Advertisement

float number( 1/5) = 0 ? Damn you VC++!!

Started by July 18, 2001 01:10 PM
6 comments, last by Chrono999 23 years, 7 months ago
Something strange has happened to my compiler here: int main(int argc, char* argv[]) { float number; number=(float)(1/5); cout< and number turns out to be 0! is there any way I can turn back to giving me a decimal?
hmmmm......
Try doing this instead:

number=0.20f*(float)
(I''m assuming the floats a variable here)
e.g.:

number=10*0.20f
the "f" tells the compiler its a float
Advertisement
ha ... I ran into this same probem in my programming class working on a project and this cause me some time in frustration. See since both 1 and 5 are integers, the compiler casts the result to an integer and truncates whatever is after the decimal so 0.2 = 0. even though "number" can hold a float it is assigned an interger as the result of an operation on two other integers. To get the right answer you can get rid of the type cast just replace

number= (float) (1/5);
with
number=(1.0/5.0);

you will get the correct answer because you are performing the operation with floats and not integers.

Anyway, hope that explains it for you, it still seems wierd to me but thats the way it is.

Nomad
Your ( float ) cast is in the wrong place...

You''re doing an integer division 1 / 5, which equals 0, then you''re casting that int to float, which is still 0.

Try casting one of the values before the division...

number = ( float )1 / 5;
number = 1 / ( float )5;
number = 1.0f / 5;

etc...

G''luck,
-Alamar
Do:
number = float(1)/float(5); 


The difference lies in the fact that integer division returns an integer (so 1/5 returns the nearest integer, which is zero) while floating point division returns a float (so 1.0f/5.0f returns 02). It''s a language feature/restriction, depending on how you iew these things, and not a fault of VC++ (meaning it''s a fault of yours )

It can be useful when you want o divide variables instead of constants:
// assume a and b are integers with values 1 and 5 respectivelyfloat fp_result;int int_result;int_result = a/b;  // int_result = 0fp_result = float(a)/float(b); // fp_result = 0.2 


This also works in reverse, ie you can "demote" a variable from a float to an integer. And the order of casting can be relevant. Consider:
int pre_result, post_result;float a = 6.5f, b = 1.3f;pre_result = int(a)/int(b); // pre_result might be 7 or 6, not sure...post_result = int(a/b);  // post_result is 5; 
I tried doing what Anonymous was saying, but I see that was another stupid mistake. Thanks guys, StevenMarky was right
Advertisement
sorry for flaming, but what are you smoking man, what i said is right, i just took the time and typed my code in vc++ and it works fine, what do you mean, "another stupid mistake"

number=(1.0/5.0);

assigns the value 0.2 to number.
to the anon poster above:
1.0 and 5.0. will generally be treated as double values and the compiler would complain about a downsizing type coercion when the resulting value (0.2, also a double) is assigned to a float variable. To make sure the numbers are treated as floats append an ''f'' to them, hence:

number = 1.0f/5.0f;

will assign 0.2f to number.

perhaps that was what he meant bu "another stupid mistake"(?)

This topic is closed to new replies.

Advertisement