Jump to content
Sign in to follow this  
JeanCremers

floating point error, [solved].

Recommended Posts

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.

2022-11-23_10-35.png

Edited by JeanCremers
  • Haha 1

Share this post


Link to post

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

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 by JeanCremers

Share this post


Link to post

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 by Der schöne Günther

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×