JeanCremers 1 Posted November 23, 2022 (edited) Hello, I found a strange complier/linker bug. I'm still using bcb 6 but i thought i'd check with c++builder 10.4 and lo and behold, there's the same bug. Look at the image, it says it all, the result should be zero but it's not. You can see the result in the caption, it's always this number 2.77etc E-17. I've also seen it happen with 10 - 0.1 giving 9.911111 or something. Man i feel bad, i made my life's work with this. Please try this yourself. Edited November 23, 2022 by JeanCremers 1 Share this post Link to post
Vandrovnik 214 Posted November 23, 2022 Hello, This is not error, this is how floating point numbers work... In binary, you cannot represent exactly 0.1. Share this post Link to post
Sherlock 663 Posted November 23, 2022 For an deeper understanding read here: https://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/ And this is your solution: https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Math.IsZero if you want to check for zero values. Share this post Link to post
JeanCremers 1 Posted November 23, 2022 (edited) Hm, after reading your links i'm not so sure anymore. But i do get 0.1 - 0.1 = 0 most of the time, only sometimes not. Edited November 23, 2022 by JeanCremers Share this post Link to post
Der schöne Günther 316 Posted November 23, 2022 (edited) You really need to understand how floating point numbers work on computers. This is not a "bug" Feel free to try this out with other C++ compilers, they won't report zero either. https://onlinegdb.com/c2zTpiuZ_c Since you did redo your post completely: You need to be aware that you are mutating the variable several times comes with a loss of precision every time. It depends on the target platform, compiler optimizations and more. You will have to accept that regular floating point arithmetics are not 100 % precise. Edited November 23, 2022 by Der schöne Günther Share this post Link to post
Attila Kovacs 629 Posted November 23, 2022 10 minutes ago, Sherlock said: And this is your solution: https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Math.IsZero if you want to check for zero values. or SameValue() 1 Share this post Link to post
JeanCremers 1 Posted November 23, 2022 Duh, i thought 0.1 was representable in FP. Thanks, now i don't have to worry myself to death anymore. Share this post Link to post