[comp.lang.c] Problem with Turbo C and floating point

brb@akgua.ATT.COM (Brian R. Bainter) (08/15/88)

I hope someone can tell me what I am doing wrong or at least
(heaven forbid) what the compiler is doing strange.

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.

If anyone could enlighten my mind as to what I am doing wrong, or
what the compiler is doing wrong and possibly give me a work around,
I would be most greatful.

BTW: my compiler is Turbo C 1.5.

Thanks in advance for any help

-- 
	Brian R. Bainter   KA7TXA

 AT&T Technologies Atlanta Works
 {cbosgd, gatech, ihnp4, moss, mtune, ulysses}akgua!brb

andru@rhialto.SGI.COM (Andrew Myers) (08/17/88)

In article <1817@akgua.ATT.COM>, brb@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

jgk@speech2.cs.cmu.edu (Joe Keane) (08/17/88)

In article <1817@akgua.ATT.COM> brb@akgua.ATT.COM (Brian R. Bainter) writes:
>When I load this "floating point"
>string into a float with a sscanf, I do have a sort of problem.

Did you use `%f' instead of `%F' or `%lf'?

--Joe
--
-- 

rob@kaa.eng.ohio-state.edu (Rob Carriere) (08/17/88)

In article <1817@akgua.ATT.COM> brb@akgua.ATT.COM (Brian R. Bainter) writes:
> [...]
>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.
> [...]

Doing a quick digit count, it looks as if you are up against the
precision limit of float.  If this is true (check your Turbo docs for
the float format TC uses, I don't know), then the solution is simply
to use doubles, and change the sscanf conversion character
correspondingly.  You will eventually have to do this anyway, as ANSI
seems to have decided, in its' infinite wisdom, that reading floats
(as opposed to doubles) is a no-no.

Rob Carriere

karl@haddock.ima.isc.com (Karl Heuer) (08/18/88)

In article <477@accelerator.eng.ohio-state.edu> rob@kaa.eng.ohio-state.edu (Rob Carriere) writes:
>ANSI seems to have decided, in its' infinite wisdom, that reading floats
>(as opposed to doubles) is a no-no.

Wrong.  ANSI scanf accepts %f (float), %lf (double), and %Lf (long double).
This is compatible with pre-ANSI usage, of course.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

rob@kaa.eng.ohio-state.edu (Rob Carriere) (08/18/88)

In article <6216@haddock.ima.isc.com> karl@haddock.isc.com (Karl Heuer) writes:
> [ I wrote that ]
>>ANSI seems to have decided, in its' infinite wisdom, that reading floats
>>(as opposed to doubles) is a no-no.
>Wrong.  [...]

Thank you for pointing out my mistake, and apologies to anyone who
felt offended by what turned out to be a totally unfounded remark; I
should have checked my facts more carefully.

Rob Carriere
"Can I take my foot out of my mouth now?"