Whats the difference between a double and a float?

6
Contributors
10
Replies
11
Views
8 Years
Discussion Span
Last Post by siddhant3s
Featured Replies
• 1

[QUOTE=tux4life;903139] The reason for using parentheses is because it's defined like that in the standard:[/QUOTE] That's kind of what he said: [QUOTE=Tom Gunn;903128] When the operand for sizeof is a type, you have to use parentheses [/QUOTE] He only explained further [i]why[/i] it might be in the standard. [QUOTE=tux4life;903139]Who cares …

• 1

>On the downside: no more time for office-sword-fighting This reminds me about the cover of the Dragon Book [img]http://www.stewart.cs.sdsu.edu/cs524/spr08/bookCover_51XtGJ64tZL._SS500_.jpg[/img]

Double is (on most systems) 8 bytes, where float is only 4. This means that double is more precise then float. This also means that doubles have less problems with rounding errors.

My personal rule of thumb is:
Use float if speed and memory-use are a (big) issue and always be aware of it's restrictions. In other cases use doubles.

Here's something to read on floats

And a little addition to niek_e 's post:
To get the amount of bytes a float and a double occupy in memory (on your implementation), you can use the sizeof operator:

``````cout << "Float: " << sizeof(float) << endl;
cout << "Double: " << sizeof(double) << endl;``````

this is of topic but, how come the size of thing is called an operator if it takes a parameter?

this is of topic but, how come the size of thing is called an operator if it takes a parameter?

Because it's defined like that in the standard :P
BTW, It's not a parameter, it's called an operand.
Here the operands are: `(double)` and `(float)` .
With variables parentheses are optional, with type names (e.g: double, int, float, etc.) parentheses are needed.

Also check out this :)

Just to add to the general information flow here, the numeric_limits class inside of <limits> can be used to see stuff like how much decimal digits your implementation can represent for a specified type before loss of precision.

Example...

``````#include <iostream>
#include <limits>

int main()
{
std::cout << "double Precision:\t" << std::numeric_limits<double>::digits10 << "\n";
std::cout << "float Precision:\t" << std::numeric_limits<float>::digits10 << "\n";
}``````

this is of topic but, how come the size of thing is called an operator if it takes a parameter?

It doesn't take a parameter, it takes an expression as the operand. Expressions can be surrounded with redundant parentheses:

``````double d;
float f;

sizeof d; // no redundant parentheses
sizeof(f); // redundant parentheses``````

It's the same way with the return operator. You see people do stuff like `return 1;` or `return(1);` , but both are right because expressions can have parentheses.

When the operand for sizeof is a type, you have to use parentheses because types can be made up of more than one token and the parentheses make it easier for the compiler to parse:

``sizeof(unsigned long int);``

When the operand for sizeof is a type, you have to use parentheses because types can be made up of more than one token and the parentheses make it easier for the compiler to parse.

the parentheses make it easier for the compiler to parse.

Who cares about the fact that the compiler can easier parse your code?
That's not the real reason.
The reason for using parentheses is because it's defined like that in the standard:

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

The reason for using parentheses is because it's defined like that in the standard:

That's kind of what he said:

When the operand for sizeof is a type, you have to use parentheses

He only explained further why it might be in the standard.

Who cares about the fact that the compiler can easier parse your code?

Actually; I do. Make it easier for the compiler and it'll compile code faster. On the downside: no more time for office-sword-fighting :)