Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C++11 type inference decide between floats or doubles?

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?

like image 331
Gearoid Murphy Avatar asked Sep 23 '12 14:09

Gearoid Murphy


People also ask

What's the difference between float and double in C?

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.

What is the difference between double and float type of data?

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.

Can we compare float and double in C?

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.

What is float and double in C++?

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>


1 Answers

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.

like image 190
Kiril Kirov Avatar answered Sep 22 '22 11:09

Kiril Kirov