• Advertisement
Sign in to follow this  

Difference between 1.5 and 1.5f

This topic is 4227 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

Hi, What's the difference between float f = 1.5; and float f = 1.5f; ? Are either of them more "correct"? Cheers, Tim

Share this post


Link to post
Share on other sites
Advertisement
1.5 is a double literal, 1.5f is a float literal.

The correct method is const float f = 1.5f; or, alternatively, const double d = 1.5;

Share this post


Link to post
Share on other sites
Ok... I've written a program with maybe 200 floats in it.

Is it worth going through and changing all the 0.5's and the 2.3's into 0.5f's and 2.3f's?

Is it worth it in terms of clarity for a programmer to read the code?

Is it worth it in terms of more efficient code (not having to cast or such) - or is this just one of those unnecessary micro-optimisations that I've heard so much bad press about?

Thanks,

Tim

Share this post


Link to post
Share on other sites
It's a question of code readability. On the other hand, literals shouldn't appear in the middle of your code anyway, except for the simplest ones. To clean this up, a regex would suffice. If all your floating-point values are in the format [-](digits).(digits), then replace \.\d+ with &f in all your files.

Share this post


Link to post
Share on other sites
Ok, thank you. I think I'll go for the readability *from now on* rather than trying to go back and change what I've already done.

Cheers.

Tim

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
operations on floats are also quite a bit faster than operations on doubles. (especially if you use SSE/SSE2 SIMD instructions). functions such as sinf and cosf (works on floats) are quite alot faster than the normal double versions.

floats also use less memory, (not a big issue unless you got lots of them).

as a rule of thumb, use floats unless you need more precision.

Share this post


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

  • Advertisement