CompuServe Messages

Long vs. Float?

    01-Aug-93 15:18:16
Sb: #36111-Long vs. Float?
Fm: Doug Walker 71165,2274
To: JAMES MORRIS 71756,247
The real problem is that if you have float = (int1*int2)/int3; you may have precision problems. If (int1*int2) is greater than will fit into an int, you'll have a problem, for example. A bigger problem is that (int1*int2) is itself an int, so (int1*int2)/int3 is done as INTEGER division, not floating point division. This can give you funny values like (10*20)/201 = 0, for example: you probably wanted your float to be set to some floating point value near 1, but it got truncated due to the integer division. The easiest way to make sure you keep the precision correct is to cast one or more of the operands to each subexpression to float. i.e., float f; int i1, i2, i3; … f = ((float)i1*i2)/i3; Since i1 is cast to float, the multiplication is done in float; since the result of the multiplication is float, the division is also done in float. So that one cast did the whole trick for you. Note that in your particular example, this won't change the answer, since the float gets 1.0 in either integer or floating point math. –Doug