RE: [PHP-WIN] is it bug ?

From: Date: Thu, 13 Sep 2001 15:42:00 +0000
Subject: RE: [PHP-WIN] is it bug ?
Groups: php.windows 
Request: Send a blank email to php-windows+get-9343@lists.php.net to get a copy of this message
> >Hello php-windows, > >for example.. >look at this simple script.. >==================================== ><? > $old=10003.28; > $new=$old-10000; > echo $new; > >?> >==================================== >it returns >3.2800000000007 >it must return >3.28 >does anyone know what is problem.. >i know that number_format function can ne used here... >but i want to know that is it bug in php Computers uses discrete numbers so there are not able to represent an real numbers properly, because of the hardware architecture. Therefor computers need to use a fractal representation for real number. This line is not smooth, but cut with regions of irregularities considering the discrete steps between the numbers. It does not like 1.00001 1.00002 1.00003 1.00004 1.00005 1.00006 1.00007 1.00008 1.00009 1.00010 But more like 1.00001 1.00002 1.00003 1.000035 1.00004 1.00005 1.00006 1.00007 1.00008 1.000095 1.00010 If you imagine the last digit, and plot this, you will mostly have a smooth straight line, but some times small hills. Or rather small region with a little bit higher granularity and other with less granularity. So when the granularity get smaller and smaller you will start to experience jumps between the "real" numbers. This will not happen all the time, but often enough to be notable. These "bugs" might be the cause of rounding errors while big transaction is done. But there is techniques to work around it. Anyhow, one can esily test this to convinve you self about where the critical regions appears with a program simlar to the one below: void main (void) { float i = 1; while (i > 0.999) { i -= 0.0000005; cout << i << endl; } } When the program runs it look fine in the begining. 1 0.999999 0.999999 0.999998 0.999998 0.999997 0.999997 0.999996 0.999996 0.999995 0.999995 0.999994 0.999994 0.999993 0.999993 0.999992 0.999992 0.999991 0.999991 0.999990 <- 0.999990 <- 0.999990 <- 0.999989 0.999989 But check out what happens above here. 0.999988 0.999988 0.999987 0.999987 0.999986 0.999986 0.999985 0.999985 0.999984 0.999984 0.999983 0.999983 0.999982 0.999982 0.999981 0.999981 0.999980 0.999980 Then it looks nice again... But future down we find: 0.999054 0.999054 0.999053 <- 0.999053 <- 0.999053 <- 0.999052 0.999052 And a little bit futher down: 0.999044 0.999044 0.999043 <- 0.999043 <- 0.999043 <- 0.999042 0.999042 And then a bit normal again until: 0.999033 0.999033 0.999032 <- 0.999032 <- 0.999032 <- 0.999031 0.999031 And elsewhere: 0.999280 0.999280 0.999279 <- 0.999279 <- 0.999279 <- 0.999278 0.999278 And just before we quits execution we still find anomalies: 0.999075 0.999075 0.999074 <- 0.999074 <- 0.999074 <- 0.999073 0.999073 ... 0.999023 0.999023 0.999022 <- 0.999022 <- 0.999022 <- 0.999021 0.999021 ... 0.999003 0.999003 0.999002 <- 0.999002 <- 0.999002 <- 0.999001 0.999001 0.999000 0.999000

« previous php.windows (#9343) next »