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

Started by
31 comments, last by Cromulent 15 years, 9 months ago
Here you go, I have taken the time to dig out K&R to show you it has always been this way and has nothing to do with OpenGL. That f is just to decorate the function for the programmer using it and for a way to overloaded the function.
Quote:
2.3 Constants ...
Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) or both; their type is double, unless suffixed. The suffixes f or F indicate a float constant; l or L indicate a long double.
Advertisement
Quote:Original post by Cromulent
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.


We ended again where we started. Definition of glRectf:

[source=c]void glRectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 );

Writting "25.0", "25.0f" or "25" in the red book won't make much difference, because the authors assume the compiler correctly converts from one type to another. They want to state how to use glRectf, not to describe detailed information about it's parameter.

You have to look at the C standard to see how the conversion is treated, not what the OGL Red Book says (unless of course, we're not talking about C language, but rather something like GLSL).

Edit: Oh, I see 'dmail' has a better answer. We replied almost at the same time
Quote:Original post by dmail
Here you go, I have taken the time to dig out K&R to show you it has always been this way and has nothing to do with OpenGL. That f is just to decorate the function for the programmer using it and for a way to overloaded the function.
Quote:
2.3 Constants ...
Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) or both; their type is double, unless suffixed. The suffixes f or F indicate a float constant; l or L indicate a long double.


Fair play. Thanks for taking the time, always useful to learn something new.

I have always just used 1.0 and almost never the f suffix when dealing with floats. Will this on modern compilers generate code that actually uses the 1.0 as a double and the casts it to a float to call the function? Or will it be cast to a float at "compile time"?
The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.
Quote:Original post by Zipster
The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.


GCC 4.1.2 doesn't warn about:
float test = 1.0;

Using -Wall.
Quote:Original post by lexs
Quote:Original post by Zipster
The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.


GCC 4.1.2 doesn't warn about:
float test = 1.0;
Using -Wall.


ditto

I've never seen these kinds of errors.
Quote:Original post by lexs
Quote:Original post by Zipster
The compiler will do the cast, and give you a warning (unless you have a low warning level). However at work we treat all warnings as errors, so if we never used the suffix we'd all be very unhappy campers.


GCC 4.1.2 doesn't warn about:
*** Source Snippet Removed ***
Using -Wall.

Well, that cast is perfectly fine because the value 1.0 is exactly representable in both float and double. Try something like this:
float a = 3.14159265358979323846;float b = 1.0 / 3.0;


The compiler should warn you about truncation from double to float, because double is of a higher precision than float.
NextWar: The Quest for Earth available now for Windows Phone 7.
[edit] Nevermind, I was way off [/edit]

[Edited by - MikeTacular on July 11, 2008 7:51:52 PM]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by MikeTacular
Actually, neither floats nor doubles can exactly represent 1.0.

IEEE 754. Read it.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

This topic is closed to new replies.

Advertisement