From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96076335021359.tru64-unix-managers.marc@archive.invalid> From: chu () musp0 ! Jpl ! Nasa ! Gov (Eugene Chu) Date: Wed, 10 Jul 1996 18:18:28 +0000 To: tru64-unix-managers Subject: SUMMARY: Floating point problem revisited X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96076335021359 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 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