The PI Function

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1823
Joined: Sat Jan 21, 2012 4:58 am
Contact:

The PI Function

Post by JimKnicely » Thu Mar 14, 2013 12:40 pm

Pi Day 2013 is today (Thursday, March 14, 2013)!

We all know that the number π is a mathematical constant that is the ratio of a circle's circumference to its diameter.

Most databases provide a built in function that can be used to display a representative value of PI. I thought it would be fun to check a few databases to see which one serves up the most accurate juiciest slice of PI by default!

MySQL

MySQL is accurate to 6 digits **:

Code: Select all

MySQL> select pi();
+----------+
| pi()     |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
SQL Server

MS SQL Server is accurate to 14 digits

Image

Oracle

Yikes, Oracle does not have a PI function, but we can simulate it using one of the following two techniques where the first one is more accurate to 8 digits:[/b]

Code: Select all

SQL> select asin(1)*2 from dual;

ASIN(1)*2
----------
3.14159265

Code: Select all

SQL> select 22/7 from dual;

      22/7
----------
3.14285714
Vertica

Vertica is accurate to 50 digits:

Code: Select all

dbadmin=> select pi();
                          pi
------------------------------------------------------
3.14159265358979323846264338327950288419716939937510
(1 row)
The winner: Vertica… Delicious!

** To be fair, MySQL does store the full double-precision value internally:

Code: Select all

dbadmin=> SELECT pi() + 0.00000000000000000000000000000000000000000000000000 bigger_pi;
                      bigger_pi
------------------------------------------------------
3.14159265358979323846264338327950288419716939937510
(1 row)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica Tips, Lessons and Examples”