0

Why does this expression evaluate true?

SimpleRoundTo(93.599, -2) > 93.6

When I use the SimpleRoundTo like this

ShowMessage(FloatToStr(SimpleRoundTo(93.599, -2)));

it shows 93.6.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
sddk
  • 1,115
  • 1
  • 10
  • 20
  • This issue boils down to `93.6` not being exactly representable in a binary floating point type. And your `SimpleRoundTo` (presumably from an older Delphi) is failing to return the closest representable value to `93.6`. Anyway, the bottom line is that you have unrealistic expectations. You need to absorb this issue of representability. – David Heffernan Mar 18 '15 at 09:24
  • @DavidHeffernan i'm using like this now; if SimpleRoundTo(93.599, -2) > SimpleRoundTo(93.6, -2) then and this code returns False. Do you think that new line has problem? – sddk Mar 18 '15 at 09:34
  • You need to step back and address the issue of representability. Are you aware that `93.6` is not representable? Do you understand what that means. Check here: http://pages.cs.wisc.edu/~rkennedy/exact-float?number=93.6 and see that to double precision, the closest representable value is in fact `93.599999999999994315658113919198513031005859375`. You aren't going to make progress by trying different bits of code. What you need is understanding. Take some time. Read the answers at the dupe. Follow the links. Make sure you understand what is meant by representability. – David Heffernan Mar 18 '15 at 09:36
  • If we are going to talk about `SimpleRoundTo` though, we need to know which version of Delphi you use. Recent versions use a different implementation of `SimpleRoundTo`. I believe you are using an older version. – David Heffernan Mar 18 '15 at 09:40
  • @DavidHeffernan ok, i'm going to read about the problem by using links. I'm using Delphi 2007. – sddk Mar 18 '15 at 09:43
  • This is a really good link: http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html Start here! – David Heffernan Mar 18 '15 at 09:44

0 Answers0