Jump to content
  • Advertisement
Sign in to follow this  
Daan Niphuis

Result of certain floating point operations

This topic is 2223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a question regarding the result of IEEE floating point operations.

I was wondering if the following statements are always true
a == a + 0.0f * (b - a)
b == a + 1.0f * (b - a)
1.0f == (a - b) / (a - b)
0.0f == (a - a) / (a - b)
where a and b are floating point numbers.

Share this post


Link to post
Share on other sites
Advertisement
No, none of them are always true.

The easiest counterexample is to set a = NaN or b = NaN, and all of the comparisons will return false. If a = +/-Inf, then all of the examples will also return false. If b = +/-Inf, then the second and fourth example will return true, but first and third will return false.

Reasoning about NaNs and Infs is easy. The more interesting question is when you constrain a and b to be finite (possibly denormalized). For that, I don't have a complete answer, but I think that at least first and fourth case will return true, at least if the FPU is in strict mode, and compiler did not do any static compile-time expression optimizations. The second case is easily false, e.g. if b << a or a << b. The third case is slightly a mystery to me, but I'm assuming it might not hold either.

Share this post


Link to post
Share on other sites

Third and fourth cases will break if a == b.


Heh, was so fixed on the IEEE peculiarities, that didn't even consider the natural reasons :)

Share this post


Link to post
Share on other sites

[quote name='alvaro' timestamp='1340027618' post='4950243']
Third and fourth cases will break if a == b.


Heh, was so fixed on the IEEE peculiarities, that didn't even consider the natural reasons smile.png
[/quote]

Hmmm, sorry about that. I forgot to mention a != b and are both finite.

I did some tests and some equations can indeed be untrue for large values of a and b. Edited by Daan Niphuis

Share this post


Link to post
Share on other sites

Hmmm, sorry about that. I forgot to mention a != b and are both finite.

I did some tests and some equations can indeed be untrue for large values of a and b.


I expect only your second equation will be untrue in general.

EDIT: Oh, if a and b are enormous and have opposite signs, a-b might overflow and then none of them will be true. Edited by alvaro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!