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

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Just cast the ints to double or float.

##### Share on other sites
oops [ignore post]

Like this:

int nApples=5;
int nOranges=3;

##### Share on other sites
Quote:
 Original post by utilaeLike this:int nApples=5;int nOranges=3;float fAnswer=(float)nApples/nOranges;

##### 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.

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.

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:

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 on other sites
It works with just one (float):

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

##### Share on other sites
Quote:
 Original post by utilaeIt 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 on other sites
Quote:
 Original post by utilaeIt 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.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633674
• Total Posts
3013275
×