Title: | DIGITAL UNIX (FORMERLY KNOWN AS DEC OSF/1) |
Notice: | Welcome to the Digital UNIX Conference |
Moderator: | SMURF::DENHAM |
Created: | Thu Mar 16 1995 |
Last Modified: | Fri Jun 06 1997 |
Last Successful Update: | Fri Jun 06 1997 |
Number of topics: | 10068 |
Total number of notes: | 35879 |
Hi, a customer of mine has a C program which doesn't round numbers correct if they are printed with the printf command. If the program: #include <stdio.h> main() { float f; f=0.624; printf("0.624=%2.2f\n",f); f=0.625; printf("0.625=%2.2f\n",f); f=0.626; printf("0.626=%2.2f\n",f); printf("\n"); f=0.724; printf("0.724=%2.2f\n",f); f=0.725; printf("0.725=%2.2f\n",f); f=0.726; printf("0.726=%2.2f\n",f); } is compilied with cc -o test test.c it produces the following output: 0.624=0.62 0.625=0.62 0.626=0.63 0.724=0.72 0.725=0.73 0.726=0.73 The rounding of the numbers ending with 5 is inconsistent. Rounding "0.625" results in the correct value "0.62" while "0.725" leads to the wrong value of "0.73". Are any bugs in the C compiler known which cause that problem? Can any compiler flags be used to avoid this unexpeted results? Thank you for your help. - David [Posted by WWW Notes gateway]
T.R | Title | User | Personal Name | Date | Lines |
---|---|---|---|---|---|
9377.1 | Floating point is inexact. | QUARRY::reeves | Jon Reeves, UNIX compiler group | Thu Apr 03 1997 15:02 | 7 |
.625, expressed as a binary fraction, is exact. .725, expressed in binary then converted back to decimal, is 0.72499999999999998 which rounds to .72. This is inherent in floating point; if you don't like it, do your floating point arithmetic in decimal (I think COBOL lets you do this). |