Ah, sorry for the brainless response.
Would you believe that the answer is (drumroll):
-- It depends on where you put your statements -- |
Yes, you read that right.
The FPU (whether integrated with the CPU or not) has its own registers, much like the CPU registers you learn about in assembly programming. On the Intel x86 architecture they are ordered as a "stack" and they are
all IEEE 80-bit double-extended-precision values.
On a PC, it is typical for the
float to be an IEEE 32-bit single-precision value and a
double to be an IEEE 64-bit double-precision value. This is not stipulated by the language, though, and so it is not guaranteed to be so by every PC compiler. On other computer architectures
float and
double are the appropriate FPU types for that machine. On
really old PCs, they might even be
emulated. :-@
NB: long float --> IEEE 48-bit single-extended-precision
long double --> IEEE 80-bit double-extended-precision
...but only if the compiler agrees that those are valid types :-)
If you make a call to a function, the state of the FPU registers cannot be guaranteed/known after the function returns (at least not in C and C++), so the
float operands are re-loaded from memory --where their precision is exactly identical, before comparing/multiplying/etc.
However, if you
don't make a call to the function, the contents of the FPU registers are known based upon the last time they were used. Hence, you may be comparing a
float (IEEE single-precision on x86 PCs) in main memory to an IEEE double-extended precision value still on the top of the FPU register stack. Hence, automatic errors.
Worse yet, even without the function calls, simply
using the floats normally causes the precision to be converted back and forth and back and fourth... each time compounding precision errors.
It gets worse, but I'll leave it at that. :-D
Here are some good links:
David Goldberg's famous
What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Comparing floating point numbers
by Bruce Dawson
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Understanding and Using Floating Point Numbers
by Jeff Bezanson
http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point.html
Whew. Have fun now!