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

Started by
7 comments, last by Nemesis2k2 18 years, 9 months ago
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?

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Advertisement
Just cast the ints to double or float.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
oops [ignore post]

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Like this:

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

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Quote:Original post by utilae
Like this:

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


float fAnswer=(float)nApples/(float)nOranges;
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
It works with just one (float):
float fAnswer=(float)nApples/nOranges;


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

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

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!
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.

This topic is closed to new replies.

Advertisement