Implicit decimal points in floating-point reads

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Wed May 22 15:21:15 AEST 1991


In article <1991May21.200003.13471 at wpi.WPI.EDU>, fenn at wpi.WPI.EDU (Brian Fennell) writes:
> >main()
> >{  char a1[10] = "26208";
> >   float x = 0.0;
> >   int ifld = 0;
> >   ifld = sscanf(a1, "%5f", &x); 
> 
>     x /= 1000.;	/* simple, to the point, no fancy fix routines needed */

Simple, to the point, and WRONG.  The thing about the Fortran format which
the original poster wanted to emulate is that the decimal point is placed
implicitly where the format specification says *UNLESS* there is an
explicit decimal point in the input field.

	if (!strchr(a1, '.')) x /= 1.0e5;

comes closer.  Of course, I don't need to explain to a sophisticated
audience why this loses accuracy compared with bending the string, do I?
-- 
There is no such thing as a balanced ecology; ecosystems are chaotic.



More information about the Comp.lang.c mailing list