Watch, Follow, &
Connect with Us

Developer Tools Community.

Welcome, Guest
Guest Settings

Replies: 6 - Last Post: Sep 26, 2017 7:16 AM Last Post By: Goossens Guy
 Posts: 2 Registered: 11/3/00
 Floating point problem Posted: Sep 25, 2017 6:20 AM I have strange result with the sample code below ! var total: double; begin total:=2.0; total:=total - 0.1; total:=total - 1.5; total:=total - 0.2; total:=total - 0.2; showmessage(floattostr(total)); end; Can you try it and tell me the result ... My computer display a message box : -9,99227827216953E-17 Someone have explanations ?
 Posts: 1,063 Registered: 8/7/01
 Re: Floating point problem Posted: Sep 25, 2017 6:26 AM   in response to: Goossens Guy Goossens This is due to the way floating point is stored and gets asked about every so often. Have a look at Roy Lambert
 Posts: 20 Registered: 12/26/06
 Re: Floating point problem Posted: Sep 25, 2017 1:43 PM   in response to: Goossens Guy Someone have explanations ? From my experience, to avoid this results, use only currency type. Assign the "double" type variable to a "currency" one and use it for calculations. And for rounding the result or intermediate results with, say, two decimals, do the rounding manually, don't use the "format" function, at least as for Delphi 7: curr2decimals := trunc((currencyvalue * 100) + 0.5) /100; strResult := floattostr(curr2decimals); That is what I concluded from a forum discussion about the floating type subject time ago. Carlos
 Posts: 161 Registered: 9/27/99
 Re: Floating point problem Posted: Sep 25, 2017 11:25 PM   in response to: Goossens Guy Goossens Guy wrote: I have strange result with the sample code below ! var total: double; begin total:=2.0; total:=total - 0.1; total:=total - 1.5; total:=total - 0.2; total:=total - 0.2; showmessage(floattostr(total)); end; Can you try it and tell me the result ... My computer display a message box : -9,99227827216953E-17 Someone have explanations ? That is why you should never test two floats/reals/doubles on equality. if (total=0) would fail. --
 Posts: 7,731 Registered: 9/22/99
 Re: Floating point problem Posted: Sep 26, 2017 6:22 AM   in response to: Goossens Guy Goossens Guy wrote: My computer display a message box : -9,99227827216953E-17 Someone have explanations ? Yes. You have a case of "catastrophic cancellation": https://en.wikipedia.org/wiki/Loss_of_significance More about floating point behaviour: http://rvelthuis.de/articles/articles-floats.html#underflow -- Rudy Velthuis http://www.rvelthuis.de "There's nothing wrong with you that reincarnation won't cure." -- Jack E. Leonard
 Posts: 7,731 Registered: 9/22/99
 Re: Floating point problem Posted: Sep 26, 2017 6:22 AM   in response to: Goossens Guy Goossens Guy wrote: I have strange result with the sample code below ! var total: double; begin total:=2.0; total:=total - 0.1; total:=total - 1.5; total:=total - 0.2; total:=total - 0.2; showmessage(floattostr(total)); end; Can you try it and tell me the result ... My computer display a message box : -9,99227827216953E-17 Someone have explanations ? As my article already explains, values like 0.1 and 0.2 cannot be represented exactly. Their real values are: 0.1 --> 0.1000000000000000055511151231257827021181583404541015625 0.2 --> 0.200000000000000011102230246251565404236316680908203125 Now just do the math. FWIW, this little tool can be used to get the exact values of floating point numbers: http://rvelthuis.de/programs/exact.html It reads the number as BigDecimal, converts it to Single, Double and Extended, then reads it back and displays the result. -- Rudy Velthuis http://www.rvelthuis.de "Any fool can make a rule, and any fool will mind it." -- Henry David Thoreau
 Posts: 2 Registered: 11/3/00
 Re: Floating point problem Posted: Sep 26, 2017 7:16 AM   in response to: Rudy Velthuis (... I read your articles with great attention. Thank you. Very informative.
Legend