Public Group

float and the differences between definitions

This topic is 4009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello! I'd like to know why do some people define their floats like this: float a= 1.f; float b= 1.0f; float c= 1.0; is there a difference between these 3 definitions(maybe on a lower level?) and why/when should i use the 'f'. thanks!

Share on other sites
Quote:
 Original post by Thirthefloat a= 1.f;float b= 1.0f;float c= 1.0;

The last one is poor practice; 1.0 is of type double as opposed to float: the compiler should warn you of the truncation (double provides greater precision than float and so you run a real risk of losing information); if it doesn't, then you need to turn up the warning level. The first two are style only and are down to you.

Share on other sites
I agree with TheUnbeliever, the 3º is the worst, but it can be used, nonetheless.

The trailing 'f' is meant to tell the compiler that the immediate (literal number) must be treated as a float, and is used not only in float type declarations, but also in floating point arithmetic operations:

float x=y*0.23f;

the traling 0 (zero) is meant to tell the compiler the size of the mantissa. Remember that a float usually uses 1 bit as sign, 23 bits as the mantissa and 8 bits for the exponent. so, a float that is written like this:

1.0e0

is the same as 1, but the rest is part of the float representation. Also Remember that 'e' means "x10^" (by ten to the...th power).

I hope this better clarifies your question.

Don't forget to rate me! (if you want to).

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• Forum Statistics

• Total Topics
632960
• Total Posts
3009481
• Who's Online (See full list)

There are no registered users currently online

×