From: chu () musp0 ! Jpl ! Nasa ! Gov (Eugene Chu)
To: tru64-unix-managers
Subject: SUMMARY: Floating point problem revisited
Date: Wed, 10 Jul 1996 18:18:28 +0000 [thread overview]
Message-ID: <96076335021359.tru64-unix-managers.marc@archive.invalid> (raw)
I asked about the appearant wierd behaviour of a C program in printing out
floating point values, which was discussed here a long time ago, but I
missed the final resolution of it. Thanks to the following for their
responses:
pas@unh.edu
dsr@lns598.lns.cornell.edu
szgyula@skysrv.Pha.Jhu.EDU
BISSON@BATES.MIT.EDU
The problem wasn't some much the C compiler as the example program itself:
#include <stdio.h>
main()
{
float fff = 4.333;
float ggg = 1/65535;
printf("ggg = %f\n",ggg);
printf("1/65535 = %f\n",1/65535);
printf("fff = %f\n",fff);
printf("1/65535 = %f\n",1/65535);
printf("ggg = %f\n",ggg);
}
The lines
printf("1/65535 = %f\n",1/65535);
is passing an int to printf, which when specified with a %f is expecting
a double precision floating point value. This, according to ANSI C,
produces unpredictable results, so the output can be anything. It's by
chance that all the other systems I tested it on produced results that
we "think we expected" when just looking at the code.
Again, the expression "1/65535", if assigned to another variable, will
produce the correct results (0 or 0.0), but can be unpredicatble if
passed as an argument to a subroutine expecting a specific input type.
eyc
reply other threads:[~1996-07-10 18:18 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox