* SUMMARY: Floating point problem revisited
@ 1996-07-10 18:18 chu
0 siblings, 0 replies; only message in thread
From: chu @ 1996-07-10 18:18 UTC (permalink / raw)
To: tru64-unix-managers
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1996-07-10 18:18 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-07-10 18:18 SUMMARY: Floating point problem revisited chu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox