PDA

View Full Version : 0.0f vs 0



joeyTR
03-18-2003, 09:23 AM
Hi http://www.opengl.org/discussion_boards/ubb/smile.gif

Please help a newbie here http://www.opengl.org/discussion_boards/ubb/wink.gif
Is 0.0f any different from 0, when it comes to GLfloat variables? I mean do I have to put that f at the end of every number? Does it prevent casting or something?

Thanks.

[This message has been edited by joeyTR (edited 03-18-2003).]

nexusone
03-18-2003, 11:28 AM
No diffrence in value, I think most compilers will take both without a problem.

Some compilers may complain about it, with a warning about mixed types.

Just that if you are working with diffrent types of numbering systems (float, int, etc), it helps keep track of what type you are using for that function.

int i = 1;
float a = 1.0f;

// like this will give compiler warning, but will still run, but can cause problems.
example:
i = a; // problem is that if a = 1.1 only 1 will be transfered to i. and I think that it will also rounds off the numbers. so 1.5f would become a 2 int.



Originally posted by joeyTR:
Hi http://www.opengl.org/discussion_boards/ubb/smile.gif

Please help a newbie here http://www.opengl.org/discussion_boards/ubb/wink.gif
Is 0.0f any different from 0, when it comes to GLfloat variables? I mean do I have to put that f at the end of every number? Does it prevent casting or something?

Thanks.

[This message has been edited by joeyTR (edited 03-18-2003).]

HalcyonBlaze
03-18-2003, 06:43 PM
Well, the reason you see the f lingering around the end of float numbers is because of the following reason. If you have just 0, it is treated as an int. If you have 0.0, it is treated as a double (which requires 2 times the memory of an int). So the f tells the compiler that it is a float and not an integer or double. With 0, it really doesn't matter. BUT with normal numbers it does matter.

For example:

3/4 = 0
3.0/4.0 = 0.75
3.0/4 = 0.75
3/4.0 = 0.75

As you can see, if both numbers are integers, you're may be getting a result that you don't want.

- Halcyon

joeyTR
03-19-2003, 04:48 AM
Thanks a lot for your answers http://www.opengl.org/discussion_boards/ubb/smile.gif

So we have to declare a number float by using f (let's say 0.75f) to save memory (since 0.75 is double-32bit) or to prevent casting?

I am asking this because I was under the impression that a function with an argument that is float, "sees" 0.75 always as float without the need of the f at the end.

(It just seems a little silly to me to see 0.0f, 1.0f etc all the time in OpenGL programs) http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

03-19-2003, 04:51 AM
"problem is that if a = 1.1 only 1 will be transfered to i. and I think that it will also rounds off the numbers. so 1.5f would become a 2 int."

Whether it rounds or truncates depends on the compiler... I think VC++ truncates, so that 1.5f would turn into a 1 as an integer.

Tzupy
03-19-2003, 01:27 PM
The standard rounding mode in C is truncate, meaning 1.6f will become 1 (integer), but the system default rounding mode is round to nearest, meaning 1.6f becomes 2 (integer). Since conversion involves switching the FPU state, doing the conversion and then switching back, floating point to integer wastes a lot of processor cycles. If I need round to nearest, am using in-line assembly to do it faster, with
fld x-double;
fistp i-integer;
There is also a trickier way to do the conversion without fistp, but on my AthlonXP it didn't work faster.