Question

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??!!

Was it helpful?

Solution

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

OTHER TIPS

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.

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).

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top