Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!gatech!bloom-beacon!oberon!sargas.usc.edu!tli From: tli@sargas.usc.edu (Tony Li) Newsgroups: comp.os.vms Subject: Re: C help Message-ID: <3332@oberon.USC.EDU> Date: Sun, 12-Jul-87 01:39:15 EDT Article-I.D.: oberon.3332 Posted: Sun Jul 12 01:39:15 1987 Date-Received: Sun, 12-Jul-87 18:04:45 EDT References: <8707100533.AA24900@super.upenn.edu> Sender: nobody@oberon.USC.EDU Reply-To: tli@sargas.usc.edu.UUCP (Tony Li) Distribution: world Organization: University of Southern California, Los Angeles Lines: 32 In article <8707100533.AA24900@super.upenn.edu> YATES@A.CHEM.UPENN.EDU ("Yates, John H.") writes: C makes me nervous! Below is an example of the dangers involved with floating point square roots. double x1 ,x2, y1, y2, x3, y3; ... printf("Enter 9.0 for this test: "); scanf("%f", &x1); ... printf("\n\nsqrt of input number %e = %e\n",x1, y1); printf("sqrt of hardwired number %e = %e\n",x2, y2); printf("sqrt of d->g input number %e = %e\n",x3, y3); Is it any wonder I keep putting away my K and R? John Please get out your K&R and your Vax C manual. Floats and doubles and G floating are *NOT* interchangeable. When you called scanf, you asked it to read a float. It did, and stored it in x1. Scanf has no idea that x1 is a g-float, so it just read an f-float and stored the binary representation into x1. Since the binary representation for f-float and g-float is radically different, you would seem to get garbage. The trick here is to tell the runtimes to expect a double precision value. This can easily be done by specifying "%lf" and "%le". ;-) Tony Li - USC University Computing Services "Fene mele kiki bobo" Uucp: oberon!tli -- Joe Isuzu Bitnet: tli@uscvaxq, tli@ramoth Internet: tli@sargas.usc.edu