Hi, Christopher!

* Christopher Egner <[EMAIL PROTECTED]> [2003-06-12 05:52]:
> float i = 0.0f;
> i = i + 1.0f;
> i = i + .11f;
> 
> Compiling and running this code results in an i equal to
> 0.11000001430511474609375

The float type in C is basically a so called IEEE single precision floating
point number. These do have about 6 significant decimal digits, so your
result is to be expected.
(See: http://www.math.byu.edu/~schow/work/IEEEFloatingPoint.htm)

If you want more significant digits use the double (15 digits) or even
long double (34 digits) types.

> I know this is close, but I'm trying to do a float to char conversion.

I don't quite know what you mean by "float to char conversion"...
If you really want to convert a float to a char just do:

(char)i

which truncates the value or

(char)roundf(i)

which rounds it first.

> And I can't seem to figure out how to get an exact value from anything
> here. Is this normal (I seem to recall it is)?

It is technically impossible to compute exactly on a computer. It's basically
the same reason why you can't write 1/3 = 0.33333... exactly in decimal
notation with a limited number of digits only that in the binary system stuff
like that happens much more often :)

> And how (if I can) do I get around this?

See above.

> Thanks a lot.

HTH,
 Mika


--
[EMAIL PROTECTED] mailing list

Reply via email to