Re: [C]

From: Edward Glamkowski (EGlamkowski@MATHEMATICA-MPR.COM)
Date: 10/07/97


>Just a short question here about floats with ANSI C on AIX.
>
>This is an example of something I was trying to do.
>
>float a;
>
>a = 10 / 100;
>
>'a' always comes back as 0.

And well it should, since the numbers being divided are
integers, so it divides them as per integer division.
You should use:
        a = 10.0/100.0;


> I've found a workaround for it but I was
>just curious if floats have to have a whole number part or if I was just
>doing something wrong.

For sake of clarity, floats should *alway* have an integer
part (even if it is just 0) AND a fraction part (even if
it is .0), so 0 would be '0.0'.
That way you will never have to worry about casting,
explicit or implicit.

But then, in general, you should avoid using floats unless
absolutely necessary, since there can be significant round
off errors (depending on the application, of course.  Never
write a money program with floats for money - use integers
and calculate in terms of numbers of pennies!!  (for US
and Canadian currency, anyway ;) ).


>The formula was more complex but it would have
>produced a number between 0.01 and 0.99. On paper it should have anyway.
>But as soon as I divided by 100, the var became 0. I'm also curious if
>it can't be done like this, how is it supposed to be done...
>


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST