-1

I'm a bit puzzled by the results of the Math.Floor function in C#.

I get a return of 91 as expected with the call below:

Math.Floor(91.0);

But if I use the call below I get a returned value of 90, while I still expect 91 in this case.

Math.Floor(9.1/0.1);

Is this just due to small rounding errors and is there a way to get consistent results?

  • That equals `90.999999` (repeating). Floor rounds it down. What is "unexpected"? – ForeverZer0 Aug 05 '18 at 06:03
  • 2
    Why would you ask this question without bothering to look an d see what `9.1/0.1` actually produces? – jmcilhinney Aug 05 '18 at 06:05
  • In the debugger the division shows an answer of 91, not 90.999999. So that's why I find this answer unexpected. My algorithm requires an answer of 91 in this case to work. But if it's the floating accuracy I guess I just need to work around this. – Arno Gerretsen Aug 05 '18 at 11:47

1 Answers1

0

Yes, this problem is related to precision, since Math.Floor() simply doesn't round mathematically and always down instead. So even 90.9999999 still results in only 90.

For accurate rounding, use Math.Round() instead or add 0.5 to the value passed.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • I do want to use floor and not round. But for something that clearly is 91, the answer of 90 surprised me. Guess I'll just have to find a workaround for 91 actually being 90.9999999. – Arno Gerretsen Aug 05 '18 at 11:49
  • @ArnoGerretsen This issue most likely isn't exclusive to 91. It may happen with other numbers as well. – Mario Aug 06 '18 at 05:26
  • I ended up adding an epsilon value before doing the math, to make sure that I also get the right value when a small precision error would give the wrong value. For my algorithm that worked best. – Arno Gerretsen Aug 16 '18 at 19:41