+ 1
int x=10, float y=3.3, int ans = x * y, ans is 32, why not 33 ?
4 Answers
+ 2
real numbers in computers aren't always exact
x*y can be 32.9999999996
after casting to int it removes fractional part and result can be 32
+ 1
i don't think there is any way
you should use some round function to get correct result
0
thank you very much. similar thing was happening with y = 3.1,3.6 (ans was 30 and 35) but not with 3.2, 3.4.(ans was 32 and 34). Is there is a way to figure out the product without using the computer ? I mean is there is a set of rules that govern the casting (like the rules for rounding off a fractional number with more decimal digits to a fractional number with less decimal digits as in mathematics )?
thank you for listening the question.
0
okay, thank you.