0

Im trying to round a number to an epsilon, but sometimes it works, sometimes it doesn't. Any explanations to this, and how to make it work?. The following code is in java, but suspect it is something common among other languages.

float roundToEpsilon(float number, float epsilon){
  float makeInt = number / epsilon;
   int bigNumber = (int) (number / epsilon) ;
   float ret = bigNumber * epsilon;
  return ret;
}

float ep = .01;
println(roundToEpsilon(random(0,1),ep));  

Results

0.76
0.42
0.51
0.58
0.35999998
0.53999996
0.35999998
0.03

I dont know why I am getting this 0.35999998??

  • 2
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – QBrute Feb 18 '20 at 12:50
  • This question will answer your question as well https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java – TreffnonX Feb 18 '20 at 12:52

0 Answers0