Float and Double

Hi Im also new to C++.

What is the difference in the following?

float number = 1.5;

float number = 1.5f;

double number = 1.5;

Are any of them the same? Does the first example default to a double because there is no suffix?
Last edited on
Floats have 8 significant figures of precision, while doubles have 16.

Does the first example default to a double because there is no suffix?


No, it's type is float.

I always use doubles, and would only use floats if I wasn't worried about the lesser precision and I had a million of them so memory space would be an issue.
I see. So does that mean you dont need to put a suffix f if you are using a float?

float number = 1.5; works? No need to put float number = 1.5f; ?
float number = 1.5;

is fine.

You would use the f in expressions with whole number, so the compiler doesn't think a number is an int:

answer = number * 10f; Edit: For this example the conversion to float is automatic anyway

I am inclined to always use 10.0 instead of 10f - however that is user preference.
Last edited on
I think I understand now. Thanks very much for your prompt answers!!
1.5 without f is always a double.

float number = 1.5;
number is a float and is initialized with the value of 1.5 converted into a float.

I am inclined to always use 10.0 instead of 10f - however that is user preference.

I think it's more to it than user preference. Floats has less precision so the expression could give you a less precise answer. The conversion from float to double is also an unnecessary cost. I would recommend not mixing float and double.
So if 1.5 without f always being a double, does that mean there is no difference between the following values? ie. They are both doubles:

float number = 1.5;

double number = 1.5;



Also why would you use these following strings?

1
2
float number = 1.5;
double number = 1.5f; 


Or would you never use it?
Last edited on
The difference is the type of the variable number.

float number = 1.5;
Here the number variable is of type float.

double number = 1.5;
Here the number variable is of type double.

Also why would you use these following strings?
It makes more sense to use float literal 1.5f for the float variable and the double literal 1.5 for the double variable.
1
2
float number = 1.5f;
double number = 1.5;
Here's an example of why it's best not to mix float and double:
1
2
3
4
5
6
7
    double d1 = 0.1f;
    double d2 = 0.1;

    cout.precision(16);

    cout << " d1: " << d1 << endl;
    cout << " d2: " << d2 << endl;

Output:
 d1: 0.1000000014901161
 d2: 0.1

I regard float as a relic from the days of 16-bit computers. I'd be rather less likely to use float than short int. In the case of integer values, the shorter value may be perfectly adequate.

However, in the case of floating point, the loss of precision may gradually increase during repeated calculations, so the final result will tend to be less accurate than the maximum which can be held by that data type.
Topic archived. No new replies allowed.