Full_Name: Gostan Thierry Version: 2.6.1 (2007-11-26) OS: Windows XP Submission from: (NULL) (193.49.190.42) I cannot explain why R seems to have problems adding two big numbers. sprintf("%f",10^4+10^19) gives "10000000000000010240.000000" instead of "10000000000000010000.000000" problems seems to arrive when i'm trying to add a big and a small number...
Gostan, This is not a bug. You're asking for 20 decimal digits of precision, which is impossible with double-precision floating point arithmetic. http://fr.wikipedia.org/wiki/Virgule_flottante Best, Josh -- http://www.fosstrading.com On Wed, May 13, 2009 at 7:35 AM, <gostan at igmm.cnrs.fr> wrote:> Full_Name: Gostan Thierry > Version: 2.6.1 (2007-11-26) > OS: Windows XP > Submission from: (NULL) (193.49.190.42) > > > I cannot explain why R seems to have problems adding two big numbers. > > sprintf("%f",10^4+10^19) gives "10000000000000010240.000000" > ? ? ? ? ? ? ? ? ? ?instead of "10000000000000010000.000000" > > problems seems to arrive when i'm trying to add a big and a small number... > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
gostan wrote:> > Full_Name: Gostan Thierry > Version: 2.6.1 (2007-11-26) > OS: Windows XP > Submission from: (NULL) (193.49.190.42) > > I cannot explain why R seems to have problems adding two big numbers. > > sprintf("%f",10^4+10^19) gives "10000000000000010240.000000" > instead of "10000000000000010000.000000" > > problems seems to arrive when i'm trying to add a big and a small > number... >I can't give you the exact answer here -- someone else may chime in with more details -- but the basic problem is that you are reaching the limits of double precision arithmetic -- see ?.Machine , which gives double.eps: the smallest positive floating-point number 'x' such that '1 + x != 1'. It equals 'base^ulp.digits' if either 'base' is 2 or 'rounding' is 0; otherwise, it is '(base^ulp.digits) / 2'. Normally '2.220446e-16'. I can't say exactly why things get wonky at 10^4+10^18 (I would have guessed you would be good for another order of magnitude), but that's just my ignorance/unwilling to think about numerical computation more carefully at the moment. Check out various threads on the mailing lists about bc and yacas (although I'm not sure if yacas runs on Windows or not). Ben Bolker -- View this message in context: http://www.nabble.com/simple-add-error-%28PR-13699%29-tp23530242p23531057.html Sent from the R devel mailing list archive at Nabble.com.
On Wed, May 13, 2009 at 02:35:12PM +0200, gostan at igmm.cnrs.fr wrote:> I cannot explain why R seems to have problems adding two big numbers. > > sprintf("%f",10^4+10^19) gives "10000000000000010240.000000" > instead of "10000000000000010000.000000" > > problems seems to arrive when i'm trying to add a big and a small number...There are already two correct answers to your problem. If you are interested in more detail, it is as follows. The number 10^4+10^19 is in binary 1000101011000111001000110000010010001001111010000010011100010000 It has 60 significant binary digits. Machine representation (double precision) rounds numbers to 53 significant digits, so in binary representation, it becomes 1000101011000111001000110000010010001001111010000010100000000000 which is 10000000000000010240 in decimal. So, the problem is not specific to R, but to floating point numbers in general. Floating point numbers with 53 digits are used for efficiency. If you need more accurate arithmetic on the cost of a remarkable slow down, use a computer algebra system. Some of them are even accessible from R, see http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy Petr.