[net.lang.c] Using SCANF with DOUBLE precision variables...

DAVE%UWF.BITNET@WISCVM.WISC.EDU (09/09/86)

'Ello,
      I've been doing a little matrix algebra in C lately, and in the process
I have been wanting to read in some real numbers, preferably thru SCANF, but I
get nothing but 0.000 returned when i try to put the result of a SCANF in a var
declared as DOUBLE.  The only thing that I could think of was either writing my
own algorithm, or reading the number in as FLOAT and storing it in the DOUBLE
variable.  Perchance someone else has had this problem and found a better
solution without re-inventing several wheels?  I guess there really isn't any
reason why I just couldn't use FLOAT variables, but then there shouldn't be any
reason why I can't use SCANF on DOUBLE vars either.
      Thanks in advance,
                        Dave

ark@alice.UucP (Andrew Koenig) (09/10/86)

> 'Ello,
>       I've been doing a little matrix algebra in C lately, and in the process
> I have been wanting to read in some real numbers, preferably thru SCANF, but I
> get nothing but 0.000 returned when i try to put the result of a SCANF in a var
> declared as DOUBLE.  The only thing that I could think of was either writing my
> own algorithm, or reading the number in as FLOAT and storing it in the DOUBLE
> variable.  Perchance someone else has had this problem and found a better
> solution without re-inventing several wheels?  I guess there really isn't any
> reason why I just couldn't use FLOAT variables, but then there shouldn't be any
> reason why I can't use SCANF on DOUBLE vars either.
>      Thanks in advance,
>                        Dave

	float f;
	double d;

	scanf ("%f", &f);		/* should work */
	scanf ("%f", &d);		/* garbage */
	scanf ("%lf", &d);		/* should work */
	scanf ("%lf", &f);		/* garbage */

phnch@phuxc.UUCP (N.C. Hauth [Courtney]) (09/16/86)

Yes, there is a solution to your problem.  You have to use the scanf
with a long float. For example:


  sscanf(line,"%*1d %1d %6lf\n",&division_1,&dc1);

Happy Hunting.
Courtney

apn@nonvon.UUCP (apn) (09/22/86)

In article <3672@brl-smoke.ARPA>, DAVE%UWF.BITNET@WISCVM.WISC.EDU writes:
> I have been wanting to read in some real numbers, preferably thru SCANF, but I
> get nothing but 0.000 returned when i try to put the result of a SCANF in a var
> declared as DOUBLE.  The only thing that I could think of was either writing my
> own algorithm, or reading the number in as FLOAT and storing it in the DOUBLE


	What about using atof to read in the float ?

vaughan@orion.UUCP (Robert Vaughan) (09/26/86)

>> I have been wanting to read in some real numbers, preferably thru SCANF
>> ...
>> declared as DOUBLE.  (...)

Well, go ahead! K&R, page 149 gives the key to reading in double precision
number using the scanf family:
	"the conversion characters e or f may be preceeded by l (letter l)
	 to indicate that .. a pointer to double rather than float is
	 expected"
The code would look like

double foo;

	scanf("%lf",&foo);

After having my code bomb several times (unfortunately, I was not as lucky
as you - instead of zeroes, I got core dumps), I discovered this little
gem in K&R.

kneller@ucsfcgl.UUCP (Don Kneller%Langridge) (09/29/86)

In article <22@orion.UUCP> vaughan@orion.UUCP (Robert Vaughan) writes:
>>> I have been wanting to read in some real numbers, preferably thru SCANF
>>> ...
>>> declared as DOUBLE.  (...)
>
>double foo;
>
>	scanf("%lf",&foo);
>
Except, of course, in the infinite wisdom of Silicon Graphics whose
floats and doubles are the same size so one must use %f for both.  The
"justification" is that they have yet another floating point type called
long float with gets the %lf format.  It's painfully nonportable.  Sigh.
-- 
	Don Kneller
UUCP:	...ucbvax!ucsfcgl!kneller
ARPA:	kneller@cgl.ucsf.edu
BITNET:	kneller@ucsfcgl.BITNET

chris@umcp-cs.UUCP (Chris Torek) (10/01/86)

In article <9939@cgl.ucsf.edu.ucsfcgl.UUCP> kneller@cgl.ucsf.edu.UUCP
(Don Kneller%Langridge) writes:
>[In the] Silicon Graphics [compiler] floats and doubles are the same
>size so one must use %f for both.  The "justification" is that they
>have yet another floating point type called long float with gets the
>%lf format.  It's painfully nonportable.  Sigh.

Indeed.  It may be too late now, but they should have used `long
double', and perhaps `%llf'.  `long float' is an alias for `double'
(per K&R p. 193).  (The arrogation of `%lf' is to me more distressing
than that of `long float'.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu