Simple rand() question

Started by
11 comments, last by nobodynews 16 years, 10 months ago
It's like 10f, it tells the compiler it's a floating point number, not an int or a double. It's the same as 10.0f too just fyi which is how I normally do it (with the decimal) so I don't confuse it.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Advertisement
Quote:
It's like 10f, it tells the compiler it's a floating point number, not an int or a double. It's the same as 10.0f too just fyi which is how I normally do it (with the decimal) so I don't confuse it.


I thought adding the decimal point was just as good as adding the f. So 10.0 was the same as 10f.

- Goishin
Sure there's a difference. Consider this program:

#include <iostream>float function(float alpha, float beta){	return alpha + beta;}double function(double alpha, double beta){	return alpha * beta;}int main(){	std::cout << "If you multiply 10 and 20 you get: " << function(10.0, 20.0) << std::endl;	std::cout << "If you add 10 and 20 you get: " << function(10.0f, 20.0f) << std::endl;}


If you didn't use the f-suffix you wouldn't be able to call the float verion of function. Which is to say that by default 10.0 is a double while 10.0f is a float. See this part of the draft standard.

Quote:The type of a floating literal is double unless explicitly specified by a suffix.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement