"1" vs "1.0f" - makes a difference?

Started by
31 comments, last by Cromulent 15 years, 9 months ago
In working with OpenGL (in C++), I gave a few functions that take floats some input like so: glFunction( 1, 7, 3 ); well, it seems to behave differently than when i specify it like this: glFunction( 1.0f, 7.0f, 3.0f ); So my question is: When assigning floating point values, does the x.xf notation make any difference than the regular "integer style" round numbers? (assuming i want a round number like "7.0") I thought the compiler implicitly converted those things! Also, is there any difference in adding the "f" at the end? (say, "0.5" vs "0.5f" ?) Thanks!
Advertisement
5 is an integer literal. 5.0 is a double literal. 5.0f is a float literal. It's possible that they will get converted for you by the compiler as necessary. The following code won't have any implicit casts:

int i = 5;double d = 5.0;float f = 5.0f;
Mike Popoloski | Journal | SlimDX
3 has type int
3.0 has type double
3.0f has type float

Exactly how some numeric types get converted to other numeric types in C++ is a complicated matter. If you are calling a function that can only accept floats, for instance, you can pass an int or a double and they will get automatically converted. However, if the function name is overloaded to accept other types, you might be calling different functions depending on exactly what types you provide.

There are some obscure bugs that can be introduced when you use the "integer style", so I recommend using 3.0 if you want a double and 3.0f if you want a float.

EDIT: Here's some code that doesn't do what you expect, because a `0' should have been `0.0':
#include <iostream>class WrappedDouble {  double x;public:  explicit WrappedDouble(double x) : x(x) {  }  operator double() const {    return x;  }};WrappedDouble five_and_a_half() {  return WrappedDouble(5.5);}int main() {  double x = false ? 0 : five_and_a_half();  std::cout << x << '\n';}
Quote:There are some obscure bugs that can be introduced when you use the "integer style", so I recommend using 3.0 if you want a double and 3.0f if you want a float.


For example:

int windowWidth = 800;int windowHeight = 600;float aspectRatio = windowWidth / windowHeight;         // aspectRatio now equals 1float aspectRatio = (float)windowWidth / windowHeight;  // aspectRatio now equals 1.3333float aspectRatio = 800 / 600;float aspectRatio = 800.0f / 600.0f;
Mike Popoloski | Journal | SlimDX
Everything said is true.

But the last time I checked, OpenGL doesn't have overloaded functions (does it?). Are you sure everything else is working fine?
What compiler are you using? MSVC 6.0, 2003, 2005, 2008? Something else?

Debug & Release versions throw consistent results?

Cheers
Dark Sylinc
Quote:Original post by Matias Goldberg
But the last time I checked, OpenGL doesn't have overloaded functions (does it?).

No because it uses C and that language can not overload functions, C++ does it with name mangling which is not available to C.

On a personal note I never bother with the zero in a float with no decimal. ie 5.f instead of 5.0f .
Quote:Original post by alvaro
3 has type int
3.0 has type double
3.0f has type float


As far as I am aware 5.0f and 5.0 are both floats. A double is for double precision floating point numbers. The f is just there to ease readability and to suggest that you are using the GL floating point type and not the GL double type as it is rather ambiguous. Although saying that it is suggested in the particular function name, so it seems rather superfluous.
Quote:Original post by Cromulent
Quote:Original post by alvaro
3 has type int
3.0 has type double
3.0f has type float


As far as I am aware 5.0f and 5.0 are both floats. A double is for double precision floating point numbers. The f is just there to ease readability and to suggest that you are using the GL floating point type and not the GL double type as it is rather ambiguous. Although saying that it is suggested in the particular function name, so it seems rather superfluous.



No, alvaro is correct - 3.0 and 3.0f are different types. See MSDN.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
No, alvaro is correct - 3.0 and 3.0f are different types. See MSDN.


Not according to the OpenGL Red Book which has code looking like this:

glRectf(-25.0, -25.0, 25.0, 25.0);

which is quite obviously the floating point version of the function.

Edit : Obviously I'm talking from a C perspective.
It's already been stated in this thread that there will be an implicit cast (aka a type coercion) made by the compiler. If you crank the warning level up, you should get a warning when coercing a really large/really precise double constant to a float. For example, try passing the constant 1e50 to a function accepting a float.

Don't believe me? Try this code:
void foo(float x){  std::cout << "X is " << x << "\n";}int main(){  foo(1);  foo(1.0);  foo(1.0f);  foo(1e50);   // Warning!  std::cout << sizeof(1.0) << "\n";  std::cout << sizeof(1.0f) << std::endl;}


All three foo calls will output the same results (the fourth has undefined behaviour), while the sizeof calls will return 8 and 4 respectively (depending on the platform).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement