What are the rules governing C++ single and double precision mixed calculations?
For example, these variables:
result (double)
a (double)
b (float)
c (float)
d (double)
A simple calculation:
result = a * (b + c) * d
How and when are the types co开发者_运维百科nverted and how do I figure out what precision each calculation is performed at?
All operations are done on objects of the same type (assuming normal arithmetic operations).
If you write a program that uses different types then the compiler will auto upgrade ONE parameter so that they are both the same.
In this situations floats will be upgraded to doubles:
result = a * (b + c) * d
float tmp1 = b + c; // Plus operation done on floats.
// So the result is a float
double tmp2 = a * (double)tmp1; // Multiplication done on double (as `a` is double)
// so tmp1 will be up converted to a double.
double tmp3 = tmp2 * d; // Multiplication done on doubles.
// So result is a double
result = tmp3; // No conversion as tmp3 is same type as result.
If you have:
float f;
double d;
...then an arithmetic expression like f * d
will promote both operands to the larger type, which in this case is double
.
So, the expression a * (b + c) * d
evaluates to a double
, and is then stored in result
, which is also a double
. This type promotion is done in order to avoid accidental precision loss.
For further information, read this article about the usual arithmetic conversions.
You have to differentiate between type conversion and value conversion. The C++ standard (C as well) allows floating-point calculations to be done at extended precision.
"The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby."
As types, b + c is an addition of two float(s). The result is a float. The result is then type promoted to a double and the two multiplications are done as doubles with a result of double.
However, an implementation is allowed to do all the calculations, including b + c, using doubles (or higher precision). Indeed, I tried it out using Visual C++ and it did all the calculations using the 80-bit floating-point stack available on x86.
You have parenthesis delimiting the float adition. So it would do b + c as float + float. Convert this to double for keeping largest precision, then multiply the double values.
However in such case where you want control over conversions, and not guessing:
use static_cast<>()
;
Following order of operations, each sub-expression is converted to the type of it's (not sure of the term here, dominant perhaps?) type. double is dominant over float, so:
(b + c) // this is evaluated as a float, since both b and c are floats
a * (b + c) // this is evaluated as a double, since a is a double
a * (b + c) * d // this is evaluated as a double, since both "a * (b + c)" and d are doubles
In your example, all the float
types are type-promoted to double
when the right-side formula is evaluated.
As for how they're converted: What I've read regarding floating point operations is that most contemporary hardware perform FP operations using extended-precision (80 bit) long doubles in special hardware registers (at least that's what I remember about modern Intel x86/x87 processors). As I understand it, float
and double
are type-promoted IN HARDWARE via special FP instructions (someone correct me if I'm wrong).
The floats will be upconverted to doubles. Explicitly cast the values.
ie if you want double as your result you would write:
result = a * double( b + c ) * d;
It is ALWAYS worth being explicit. It gets round misunderstandings like this and it is INSTANTLY obvious to anyone trying to use your code exactly what you mean.
精彩评论