Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jack_1313

Stupid Question (.0f)

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

Hello. I wish to know exactly what adding '.0f' to a number achieves (C++). In all my programming experience I have never needed to use it, until I have recently struck this problem: This gives me a totally incorect result: vx=playerSoldier->x-(WINDOW_WIDTH/2)+(IE.MouseX-WINDOW_WIDTH/2))*(640/800); While this is correct: vx=playerSoldier->x-(WINDOW_WIDTH/2)+(IE.MouseX-WINDOW_WIDTH/2))*(640.0f/800.0f); ?? 640/800 is clearly 0.8, but the program does not seem to think so unless I add .0f onto the end of the number. What does the .0f do? Does it tell the machine to deal with these numbers as floats? Why does simply (640/800) not work? Do I need to go through my code and add .0f to all my numbers? Also, given a standard int or float, how does one tell the computer that 'whatever_number=whatever_number + .0f'? [edited by - jack_1313 on January 11, 2004 9:30:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
It is important to put .0f at the end of a number if you intend to have accurate division. If you don''t, the compiler does integer division. that is 640/800 will equal 0, because (int).8 is 0.

If you intend to have integer division, then divide by the integers without .0f, but it is likely that you intend to have floating point division most of the time, so you should go through and look for division and divide.

--Vic--

Share this post


Link to post
Share on other sites
hawflakes is correct, but to be more specific, ((float) 640)/((float) 800) evaluates to .8 while 640/800 evaluates to zero (!)

The reason is that 640 and 800 are both integer values, and your C++ compiler preserves their type during the division. Since you do not explicitely cast it (which is what you do when you precede it with another type in parentheses, e.g. (double)), the expression 640/800 evaluates to 0.8, which is truncated to an integer value of 0.

adding the .0 and appending ''f'' to the value is the same as preceding it with the word float in parentheses, and the expression 640.0f/800.0f will now evaluate to the desired decimal value (not zero).

I hope that helps...

Share this post


Link to post
Share on other sites
Thanks for the help. So, if I am reading this corectly, if both values on either side of the division are int then I get a faster, more efficiant division. However, if either one is a float or is intended to be a float (either with a typecast, stored in a float variable or a .0f trailing an int) then I get a more accurate result.
So does that mean that 640.1*800 gives me a ''float'' division? If that is the case, then I am totally missing the point of ''f''. Why not just 640.0*480.0? Does a plain .0 not tell that I am dealing with floats (or doubles etc).

Share this post


Link to post
Share on other sites
Putting just .0 should work the same but the number will be treated as a double, not a float. If you want the compiler to treat it as a float (there might be cases where you want this) then you put .0f to explicitly tell the compiler to treat it as a float.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
Thanks alot. Now all I have to do is remeber this in future situations... is there a way to simply tell the program to use float division by default? Probably not...

Share this post


Link to post
Share on other sites
quote:
Original post by jack_1313
Thanks alot. Now all I have to do is remeber this in future situations... is there a way to simply tell the program to use float division by default? Probably not...

No. The language standard dictates how types should be preserved and when they should be cast and in what order. Usually it''s best to use an explicit cast. C++ is just not loosly typed.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!