views:

426

answers:

4

just curious! but I spotted that the value of π held by SAS is in fact incorrect.

for instance:

data _null_;
x= constant('pi') * 1000000000000000000000000000;
put x= 32.;
run;

gives a π value of (3.)141592653589792961327005696

however - π is of course (3.)1415926535897932384626433832795 ( http://www.joyofpi.com/pi.html ) - to 31 dp.

what gives??!!

+6  A: 

PI is held as a constant in all programming languages to a set precision. It isn't calculated. Your code just exposes how accurate PI is in SAS.

Garry Shutler
+3  A: 

You got 16 digits of precision. Which means it probably uses an IEEE 754 double-precision floating point representation, which only gives about 16-17 decimal digits of precision. It is impossible for π to be represented in any finite number of digits, so any computer representation is going to be truncated at some number of digits. There are ways of doing arbitrary-precision math (Java has a BigDecimal class), but even then you'd have to truncate π somewhere. And math done that way is several orders of magnitude slower (because it is not handled by direct CPU instructions).

Kip
+6  A: 

SAS stores PI as a constant to 14 decimal places. The difference you are seeing is an artifact of floating point math when you did the multiplication step.

data _null_;
    pi=constant("PI");
    put pi= 32.30;
run;

/*On Log */

pi=3.141592653589790000000000000000
cmjohns
Great answer. In particular the point about floating point math, multiplying a small number by a relatively large number is a great way to run into trouble.
Rog
+1  A: 

As Garry Shutler said, it's held as a constant. Note that that small fractional values in the numeric types of computer languages are rarely all that accurate (in fact, their accuracy can be lower than their precision), because they're stored as very good approximations that can be manipulated quickly. If you need excellent precision (as in financial and scientific endeavors), you need to use special types like Java's BigDecimal that handle being completely accurate (at the cost of computational speed). (Sorry, don't know SAS so don't know of an analog for BigDecimal.)

T.J. Crowder