# Differences between float and double variables

• 08-07-2004
sevensuns
Differences between float and double variables
Hello!

Thantos told me the difference between:

//first command
float n = 2.59f;

and

//second command
float n = 2.59;

The first command create a float variable and the second create a double variable.

What the difference between the first one and the second one?

The precision and the lenght only?

Thank you very much!

Bye!!!

Silas Justiniano - Brazil
• 08-07-2004
hunterdude
A double is bigger and more precise. It is 8 bytes while a float is 4 bytes.
• 08-07-2004
toysoldier
what's mean "f" after 2.59, how does it work?
Quote:

float n = 2.59f;
• 08-07-2004
sean
In programming you often have to deal with different number systems. For example, if you're going to specify something in hexadecimal you must precede the number with "0x" (zero x), octals require just the zero, and in this case, if you want to specify it as a float you put an f at the end.
• 08-07-2004
Dave Evans
IEEE standard floating point numbers have about eight or nine decimal digits of precision. Double precision numbers have more.

Now, in C, (therefore, by extension C++), all internal calculations are carried out in double precision. Assignment statements evaluate the right hand side, then convert to whatever is required by the left hand side.

Therefore, the following declarations store the same value in f:

Code:

```  float f = 0.1;   float f = 0.1f;```
Why would you want to use 0.1f rather than 0.1? In this case, at least, it's a matter of style, rather than substance. I'm sure that there are sometimes reasons that people want to do this, but it doesn't affect the results.

Try the following with your favorite C++ compiler:

Code:

```#include <iostream> int main() {   float f = 0.1;   double d = 0.1;   float ff = 0.1f;   double df = 0.1f;   std::cout.precision(20);   std::cout << "f  = " << f << std::endl;   std::cout << "d  = " << d << std::endl;   std::cout << "ff = " << ff << std::endl;   std::cout << "df = " << df << std::endl;   return 0; }```
Now, I have told the program to print out 20 digits after the decimal point. This is more than the precision of float or double variables (at least for IEEE standard numbers, which are used in the compilers that I have at hand).

The number 0.1 cannot be represented exactly by a binary (or hex) floating point number. Double precision still cannot store the number exactly either.

How many correct significant digits do you see?

Is there any difference between 0.1 and 0.1f?

Regards,

Dave
• 08-08-2004
Salem
> Is there any difference between 0.1 and 0.1f?
Code:

```        float  a = 0.1f;         double  b = 0.1;         if ( a == b ) printf("boo\n");```
Overly picky compilers and code checking tools will warn you about loss of precision if you do
float a = 0.1;
since 0.1 is a double floating point constant and therefore information is lost when its assigned to a float.
• 08-08-2004
skorman00
a float will leave 23 bits for the fraction value, a double will leave 52
http://research.microsoft.com/~holla...ieeefloat.html
you can find other information about it if you google for ieee 754, which is the doc that defines how they want floating point values to be defined. Kinda scary how I know that document off hand.
• 08-08-2004
Thantos
Of course the ANSI standard doesn't specify that a computer has to use the IEEE standard
• 08-08-2004
skorman00
ANSI wants at least 6 and 10 for float and double respectively if my memory serves me well, can you verify that for me thantos?