Consider this example of type inference:
auto var = 1.0 ;
Does this evaluate to a float or double under C++11 type inference?, can this behaviour be controlled?
float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value.
float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.
Since the precision of float is less than the double therefore after a certain point(23 in float and 52 in double) it would truncate the result. Hence, after promotion of float into double(at the time of comparison) compiler will pad the remaining bits with zeroes.
Float: The C++ float type is a primitive data type that holds floating values up to 7 digits. Double: The C++ double is also a primitive data type that is used to store floating-point values up to 15 digits. The following program illustrates the difference between C++ float and C++ double: #include<iostream>
It will be evaluated as a double
and yes, you can control it.
In the standard ISO-14882:2011, 2.14.4 Floating literals, point 1:
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With