Say if i have two floating point numbers
double a = 0.1
double b = 0.1
Then a, and b, after going through the EXACT SAME set of complicated arithmetic operations, on the same computer, then compare. will a==b always return true in this case?
Say if i have two floating point numbers
double a = 0.1
double b = 0.1
Then a, and b, after going through the EXACT SAME set of complicated arithmetic operations, on the same computer, then compare. will a==b always return true in this case?
It depends on your compiler, and what exactly you mean by "the exact same operations".
For example, if the compiler is allowed to use fused multiply-add (FMA) then the result of a*b + c*d
is not defined - it could be fma (a, b, c*d)
or fma (c, d, a*b)
, or a*b + c*d
— all three are equally valid and possibly different.
And one line in your source code can be compiled in different ways. Say this multiplication a*b + c*d
is contained in a function that is inlined, then several calls from several places could produce different results. In C, C++, Objective-C that's perfectly legal.
And note that FMA is faster and has only one rounding error instead of two, so it is absolutely preferable. It can make a dramatic difference to the speed of floating-point heavy code. And note that since you start with two different variables, you cannot go through the same operations.
Yes, a==b will return true*. People are sometimes surprised by the results of floating point operations and think that there is some element of randomness involved. There's not, unless you call a random number generator. Finite precision floating point arithmetic is completely deterministic, it's just that it doesn't match our expectations for arithmetic with infinite precision integers or reals.
*unless a and b have become NAN (not a number). A value of NAN is not equal to any other value including another NAN. This is one of those elements of floating point math that is completely deterministic, but violates casual expectations.
Effectively, yes.
I can imagine some more esoteric hardware might dispatch the operations differently (one to a dedicated floating point processor, one to a general purpose processor which produce slightly different results) but that is so unlikely that I probably wouldn't design for it.
That said, I'd still do some sort of epsilon check rather than the raw equality. Just because they're going through the exact same set of operations today doesn't mean they always will.
Yes, if you mean machine instructions.
Getting from your preferred source-language to machine instructions is an arbitrarily involved process balancing time to compile, size of the code, and expected speed of execution, where allowed transformations depend on language semantics and requested/banned optimisations.
Inlining, fast-math, when to store (and potentially discard excess precision), fusing instructions (mostly fused multiply-add) with different context can result in different machine code.
As an answer to your question, then yes the floats will effectively be the same. As mentioned by Telastyn you might want check the equality because the future is always unsure and changes happen quite often.
To give some perspective/reference on the matter of floating point you can check: