Problem with Turbo C and floating point

Andrew Myers andru at rhialto.SGI.COM
Wed Aug 17 03:08:39 AEST 1988


In article <1817 at akgua.ATT.COM>, brb at akgua.ATT.COM (Brian R. Bainter) writes:
> I have an application where I have a data entry field. The field
> is labeled as a floating point field, however it is entered into
> a string variable. No problem yet. When I load this "floating point"
> string into a float with a sscanf, I do have a sort of problem.
> If the number in the string is for example 9999999.99 it ends up in
> the float as 10000000.00000 and a number entered as 1111111.11 ends
> up in the float as 1111111.12500.

I would guess that your problem is the 'float' type: it only has about 7
digits of precision on most systems, so that a number like 10000000.00
just can't be represented that accurately. The 23-bit mantissa (approximately)
doesn't have enough bits to distinguish between 9999999.99 and 10000000.00.
One possible solution is to change the relevant fields to be of type
'double', which should have enough precision for your purposes (~16 digits,
I'd guess). Alternatively, you could store the value as a string, or, if
all your values are near 10000000, as an offset from that number. This, of
course, depends on what you are using them for.

Andrew Myers



More information about the Comp.lang.c mailing list