• Advertisement
Sign in to follow this  

float and the differences between definitions

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Thirthe
float 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 this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement