# float

Printable View

• 12-28-2002
laasunde
float
Good day

A simple question, whats the difference between :

Code:

float number = 12.34f;

and

Code:

float number = 12.34;

• 12-28-2002
Prelude
>float number = 12.34f;
This assigns a single precision value to a single precision variable.

>float number = 12.34;
This assigns a double precision value to a single precision variable, your compiler may warn about this.

-Prelude
• 12-28-2002
elad
that then begs the question, What's the difference between a single precision variable and a double precision variable?
• 12-28-2002
Crimpy
A double precision variable has twice the precision of a single precision variable. Isn't that kind of obvious?
• 12-28-2002
Polymorphic OOP
Actually, double doesn't necissarily mean twice the precission.
• 12-28-2002
Crimpy
Yea, but you can't quantify exactly what the difference is, so twice as much as as good as any for a "Duh" question like that.
• 12-29-2002
vasanth
consider a number 3.14285

If you store it in a variable do you want to save it as 3.14 or 3.142.
• 12-30-2002
laasunde
According to my c++ book float uses 7 digits precision and a total of 4 bytes. Does that mean if I write
Code:

float number = 12.34;
it uses 7 digits precision and if I write
Code:

float number = 12.34f;
it uses 14 digits precision ?
• 12-30-2002
Polymorphic OOP
Quote:

Originally posted by laasunde
According to my c++ book float uses 7 digits precision and a total of 4 bytes. Does that mean if I write
Code:

float number = 12.34;
it uses 7 digits precision and if I write
Code:

float number = 12.34f;
it uses 14 digits precision ?

No, the former has more precision than the latter.
• 12-30-2002
laasunde
Quote:

Originally posted by Polymorphic OOP
No, the former has more precision than the latter.
So this is correct then

float number = 12.34; // 14 digits precision

float number = 12.34f; // 7 digits precision

Both using 4 bytes of memory.
• 12-30-2002
Polymorphic OOP
Nono, float is usually 4 bytes and double is usually 8 bytes. Notice it's usually -- the standard only says that double has to be greater than or equal to the size and precission of float.

4-byte float is generally accurate to 23 binary digits and 8-byte double is generally accurate to 52 binary digits.