Sign in to follow this  
Daan Niphuis

Result of certain floating point operations

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
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
[quote name='alvaro' timestamp='1340027618' post='4950243']
Third and fourth cases will break if a == b.
[/quote]

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='clb' timestamp='1340030371' post='4950265']
[quote name='alvaro' timestamp='1340027618' post='4950243']
Third and fourth cases will break if a == b.
[/quote]

Heh, was so fixed on the IEEE peculiarities, that didn't even consider the natural reasons [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/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
[quote name='Daan Niphuis' timestamp='1340030774' post='4950267']
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.
[/quote]

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

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