Jump to content
  • Advertisement
Sign in to follow this  
utilae

Trying to divide two variables to get a float, but get a whole number instead

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

Hi, I am using C++. I am dividing two variables, eg int nApples=5; int nOranges=3; float fAnswer=nApples/nOranges; //fAnswer should be 1.6667 but it is 1, which is wrong The only way I know of to fix this is to do this: int nApples=5; int nOranges=3; float fAnswer=nApples/(nOranges*1.0f); //fAnswer is 1.6667 But I don't want to do that. Is there another way?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by utilae
Like this:

int nApples=5;
int nOranges=3;
float fAnswer=(float)nApples/nOranges;


float fAnswer=(float)nApples/(float)nOranges;

Share this post


Link to post
Share on other sites
A lot of people run into this. Let me explain what's happening.

int nApples=5;
int nOranges=3;

Here you have two integers. They are whole numbers, and cannot store fractional parts.

nApples/nOranges;

Here you have an expression involving two integers, the result of which is also an integer. The operation is completed using integer division, which does not produce a fractional component. The result is an integer.

float fAnswer=nApples/nOranges;

Here you are assigning this integer which resulted from the division operation to a float. The type of the expression is promoted to a float after the operation has been performed, and it is assigned to the fAnswer variable.


In order to perform floating point division on these two variables, the two variables themselves must be promoted to floats, not the result of the expression. You can do this in a number of ways. The easiest and clearest is using a type cast.

float fAnswer = (float)nApples/(float)nOranges;

The (float) addition before each variable performs a typecast to the indicated datatype. The conversion is performed first, then the division operation second, resulting in a floating point result, which is assigned to fAnswer.

The reason your other example worked:

float fAnswer=nApples/(nOranges*1.0f);

Is because when you multiplied nOranges by 1.0f, in order to complete the operation, the compiler promoted nOranges to a float automatically (an implicit conversion), then performed the multiplication. The result of the expression was a float, so when it went to do the division, it had one integer (nApples), and one float (the result of the multiplication). It now promoted nApples to a float implcitly as well, and performed a floating point division. To do these conversions explicitly, use a typecast.

Share this post


Link to post
Share on other sites
It works with just one (float):
float fAnswer=(float)nApples/nOranges;


Could a bug arise if I was only using one float?

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
It works with just one (float):
float fAnswer=(float)nApples/nOranges;


Could a bug arise if I was only using one float?


Not likely [smile]

Just don't wrap the expression in parenthesis... then you're back to the old (wrong) answer..

//don't do this!

float fAnswer = (float)(nApples/nOranges); // won't work!

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
It works with just one (float):
float fAnswer=(float)nApples/nOranges;


Could a bug arise if I was only using one float?

When you only typecast one of the variables, it is left with one operand being a floating point number, and the other being an integer, and it performs an implicit conversion on the integer and promotes it to a float, which is what happened in your multiplication example.

While you won't get errors from only casting one of the variables, don't do it. Put an explicit typecast on both of them, otherwise it's just confusing. If you modify this expression later too, you could create another type conversion problem. Qualify them both. Generally speaking, you shouldn't rely on implicit conversions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!