Date: Sun, 7 Mar 1999 21:50:22 -0500 (EST) From: Daniel Reed To: Victor Senderov cc: djgpp AT delorie DOT com Subject: Re: Problem with floats In-Reply-To: <36E32739.E6E9AFA0@usa.net> Message-ID: Precedence: first-class MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Sun, 7 Mar 1999, Victor Senderov wrote: ) cout << setprecision(25); ) double test = 37000.95000; ) cout << test; [...] ) Output : 37000.94999999999708961695 A similar concern was raised on a Visual C++ mailing list I'm on (I only reply to questions relating to pure C problems). I think my reply adequately explains the problem (see below). The poster had a question as to why printf("%.2f")ing a variable containing the value 1.305 showed up as 1.30, while printf("%.2f")ing the same variable reset to 1.605 showed up as 1.61. -- Daniel Reed When you take stuff from one writer it's plagiarism; but when you take it from many writers, it's research. -- Wilson Mizner --------------------------------------------------------------------------- Date: Wed, 10 Feb 1999 18:29:05 -0500 (EST) From: Daniel Reed To: vcpp AT earth DOT ml DOT org Cc: windev AT rinolam DOT com DOT hk Subject: Re: [VCPP-2953] Pls Help : Question about printf & precision rounding .. On Wed, 10 Feb 1999 DavPatrick AT aol DOT com wrote: ) printf( " x = %.2f ", x ); Take a look at printf("%1.10f\n", x); when x = 1.105 and when x = 1.305 and you can answer your own question. ) 1.105 1.11 ) 1.205 1.21 ) 1.305 1.30 << why did it round down here ? ) 1.405 1.41 ) 1.505 1.50 ) 1.605 1.61 ) 1.705 1.71 ) 1.805 1.80 << and here (18:26)root AT narnia:~# cat davpatrick.c int main(void) { float f = 0.0; f = 1.105; printf("%1.10f\n", f); f = 1.205; printf("%1.10f\n", f); f = 1.305; printf("%1.10f\n", f); f = 1.405; printf("%1.10f\n", f); f = 1.505; printf("%1.10f\n", f); f = 1.605; printf("%1.10f\n", f); f = 1.705; printf("%1.10f\n", f); f = 1.805; printf("%1.10f\n", f); } (18:26)root AT narnia:~# gcc davpatrick.c -o davpatrick (18:26)root AT narnia:~# ./davpatrick 1.1050000191 1.2050000429 1.3049999475 1.4049999714 1.5049999952 1.6050000191 1.7050000429 1.8049999475 (18:26)root AT narnia:~# Numbers are stored in binary form in computers, not as strings (which is how humans store numbers). For fractional parts, there must be a set precision used, which causes floating numbers to be stored in inexact forms. 1.3049999475 does, in truth, round to 1.30 when rounding to two decimal places, while 1.6050000191 rounds to 1.61. -- Daniel Reed Who controls the past controls the future. Who controls the present controls the past. -- George Orwell