like below fixes the issue I have some "reference" float numbers (1.5, 3 and 7.1 for example), is there any way how to get some other "random" float rounded to closest one from the brackets? (I mean if there's something like round, which can takes reference values for rounding) , A bit more optimum answers: the "for i" way, easier to read code :
private static double roundForI(double toRound, double[] numbers) {
int minIndex = 0;
double minDiff = Double.MAX_VALUE;
double diff;
for (int i=0; i<numbers.length; ++i) {
diff = Math.abs(numbers[i]toRound);
if (diff < minDiff) {
minDiff = diff;
minIndex = i;
}
}
return numbers[minIndex];
}
private static double roundForEach(double toRound, double[] numbers) {
double diff, roundValue = 0, minDiff = Double.MAX_VALUE;
for (double dob : numbers) {
if ((diff = Math.abs(dobtoRound)) < minDiff) {
minDiff = diff;
roundValue = dob;
}
}
return roundValue;
}
private static double roundLambda(double n, double[] a) {
double[] r = {0}, d = {Double.MAX_VALUE};
Arrays.stream(a).filter((i)>Math.abs(in)<d[0]).forEach(i>{d[0]=Math.abs(in);r[0]=i;});
return r[0];
}
Share :

Round to closest integer or closest .5 in Ruby
By : lalala228
Date : March 29 2020, 07:55 AM
Any of those help Maybe the title is confusing, but I will try to explain with examples: , Multiply by 2, round, divide by 2. code :
[4.12, 4.24, 4.25, 4.33, 4.53, 4.65, 4.75, 4.84].map do x
r = (x * 2).round / 2.0
r.to_i == r ? r.to_i : r
end
=> [4, 4, 4.5, 4.5, 4.5, 4.5, 5, 5]

Round a double to the closest and greater float
By : fizzyprogramming
Date : March 29 2020, 07:55 AM
around this issue Simply assigning a double to float and back should tell, if the float is larger. If it's not, one should simply increment the float by one unit. (for positive floats). If this doesn't still produce expected result, then the double is larger than supported by a float, in which case float should be assigned to Inf. code :
float next(double a) {
float b=a;
if ((double)b > a) return b;
return std::nextafter(b, std::numeric_limits<float>::infinity());
}
float next_after(float a) {
*(int*)&a += a < 0 ? 1 : 1;
return a;
}
float next_after(float a) {
union { float a; int b; } c = { .a = a };
c.b += a < 0 ? 1 : 1;
return c.a;
}

Function to round integer or float up to closest increment in Powershell
By : Hansdo
Date : March 29 2020, 07:55 AM
should help you out I want to create a function that rounds a number up to the closest increment of five in Powershell. Every number less than 1 should be rounded up to one, but every number greater than 1 should be rounded up to the closes increment of 5 (5, 10, 15 etc.). , Maybe this is more performant ( need to be tested, I've no time now): code :
Function GetIncrement([float] $value, [int] $increment=5){
if($value gt 1)
{
[Math]::Ceiling($value / $increment) * $increment;
}
else
{
[math]::Ceiling($value)
}
}

How to divide unsigned integer of 256 and round to the closer value?
By : mahsa
Date : March 29 2020, 07:55 AM
Any of those help I need to divide number of 256 and round it to closer value, for example, if input 255 I want to get 1, not 0. Now I'm using , Use this: code :
unsigned int x = 150;
unsigned int z = (x + 128) >> 8;
int x = 150;
int z = (x >= 0 ? (x + 128) : (x  128)) / 256;

How to remove all elements from sorted Ruby array which are closer to its closest neighbour than a limit?
By : Serrano Santiago
Date : March 29 2020, 07:55 AM
This might help you There seems to be some ambiguity in the question. I interpret it as I stated in a comment on the question. code :
data = [ 3.42, 5.49, 6.12, 6.48, 7.11, 8.79, 9.36,
9.54, 10.86, 10.95, 11.07, 13.08, 14.41, 14.92]
limit = 0.5
([Float::INFINITY].concat(data) << Float::INFINITY).each_cons(3).
select { a,b,c ba >= 0.5 && cb >= 0.5 }.
map { _,b,_ b }
#=> [3.42, 5.49, 7.11, 8.79, 14.41, 14.92]

