How precise is R numerically? For example I
wrote the following function for calculating
the volume of the ball inscribed in the
unit cube in m dimensions. In order to see what
happens in 40 dimensions, I created an output
of 24 digits. But how many are precise?
Thanks
Josef Eschgf?ller
Ferrara
---------------------------------------
Vol = function (m)
{if (m<=1) 1
else Vol(m-2)*pi/(m+m)}
for (m in 1:40)
{x=sprintf('%2d %.24f',m,Vol(m))
print(x)}
---------------------------------------
1 1.000000000000000000000000
2 0.785398163397448278999491
3 0.523598775598298815658893
4 0.308425137534042437259529
5 0.164493406684822623953224
6 0.080745512188280771370685
7 0.036912234143214060766436
8 0.015854344243815498421979
9 0.006442400200661534299951
10 0.002490394570192719803786
11 0.000919972597358349329817
12 0.000325991886927389960208
13 0.000111160736667881195885
14 0.000036576204182177245747
15 0.000011640725122781503579
16 0.000003590860448591509251
17 0.000001075600486123191399
18 0.000000313361689037812061
19 0.000000088923646984269168
20 0.000000024611369504941992
21 0.000000006651473240385528
22 0.000000001757247673443401
23 0.000000000454265640598788
24 0.000000000115011591279740
25 0.000000000028542351985668
26 0.000000000006948453273887
27 0.000000000001660526728044
28 0.000000000000389807317126
29 0.000000000000089943078792
30 0.000000000000020410263397
31 0.000000000000004557492187
32 0.000000000000001001886462
33 0.000000000000000216936121
34 0.000000000000000046287046
35 0.000000000000000009736070
36 0.000000000000000002019653
37 0.000000000000000000413335
38 0.000000000000000000083486
39 0.000000000000000000016648
40 0.000000000000000000003278
Josef Eschgfaeller wrote:> > How precise is R numerically? For example I > wrote the following function for calculating > the volume of the ball inscribed in the > unit cube in m dimensions. In order to see what > happens in 40 dimensions, I created an output > of 24 digits. But how many are precise?For most floating point operations R uses "double precision", which gives about 18-19 significant digit precision. Leading zeros don't count. Duncan Murdoch> > Thanks > Josef Eschgf?ller > Ferrara > --------------------------------------- > Vol = function (m) > {if (m<=1) 1 > else Vol(m-2)*pi/(m+m)} > > > for (m in 1:40) > {x=sprintf('%2d %.24f',m,Vol(m)) > print(x)} > --------------------------------------- > 1 1.000000000000000000000000 > 2 0.785398163397448278999491 > 3 0.523598775598298815658893 > 4 0.308425137534042437259529 > 5 0.164493406684822623953224 > 6 0.080745512188280771370685 > 7 0.036912234143214060766436 > 8 0.015854344243815498421979 > 9 0.006442400200661534299951 > 10 0.002490394570192719803786 > 11 0.000919972597358349329817 > 12 0.000325991886927389960208 > 13 0.000111160736667881195885 > 14 0.000036576204182177245747 > 15 0.000011640725122781503579 > 16 0.000003590860448591509251 > 17 0.000001075600486123191399 > 18 0.000000313361689037812061 > 19 0.000000088923646984269168 > 20 0.000000024611369504941992 > 21 0.000000006651473240385528 > 22 0.000000001757247673443401 > 23 0.000000000454265640598788 > 24 0.000000000115011591279740 > 25 0.000000000028542351985668 > 26 0.000000000006948453273887 > 27 0.000000000001660526728044 > 28 0.000000000000389807317126 > 29 0.000000000000089943078792 > 30 0.000000000000020410263397 > 31 0.000000000000004557492187 > 32 0.000000000000001001886462 > 33 0.000000000000000216936121 > 34 0.000000000000000046287046 > 35 0.000000000000000009736070 > 36 0.000000000000000002019653 > 37 0.000000000000000000413335 > 38 0.000000000000000000083486 > 39 0.000000000000000000016648 > 40 0.000000000000000000003278 > >------------------------------------------------------------------------ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Wed, Apr 06, 2005 at 02:00:58PM +0200, Josef Eschgfaeller wrote:> How precise is R numerically? For example I > wrote the following function for calculating > the volume of the ball inscribed in the > unit cube in m dimensions. In order to see what > happens in 40 dimensions, I created an output > of 24 digits. But how many are precise?R uses IEEE-754 double precision floating point arithmetic (see capabilities()), which has a 52 bit mantissa, roughly corresponding to 15 decimal digits. This is how much information is available, how much of it is precise in the sense that it accurately reflects the quantity you're computing depends on the computation you're doing.> Thanks > Josef Eschgf?ller > Ferrara > --------------------------------------- > Vol = function (m) > {if (m<=1) 1 > else Vol(m-2)*pi/(m+m)} > > > for (m in 1:40) > {x=sprintf('%2d %.24f',m,Vol(m)) > print(x)} > ---------------------------------------For getting an impression of "what happens", this looks ok to me. If you're concerned that precision wanes because you get less and less non-zero digits: that's not the case -- use the %e or %g format to see that the number of mantissa digits does not decrease. Best regards, Jan -- +- Jan T. Kim -------------------------------------------------------+ | *NEW* email: jtk at cmp.uea.ac.uk | | *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk | *-----=< hierarchical systems are for files, not for humans >=-----*