How PI() Works in MariaDB

In MariaDB, PI() is a built-in function that returns the value of the number π (pi).

The number π is a mathematical constant. It is defined as the ratio of a circle’s circumference to its diameter, and it also has various equivalent definitions. It is approximately equal to 3.141593, although its decimal representation never ends.

Syntax

The syntax goes like this:

PI()

So no arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT PI();

Result:

+----------+
| PI()     |
+----------+
| 3.141593 |
+----------+

Return More Decimal Places

The default number of decimal places displayed is six, but MariaDB uses the full double-precision value internally.

Therefore, we can use the following technique to return more decimal places:

SELECT PI()+0.000000000000000;

Result:

+------------------------+
| PI()+0.000000000000000 |
+------------------------+
|      3.141592653589793 |
+------------------------+

Expressions

You can use PI() in expressions like the following:

SELECT PI() * 1000;

Result:

+-------------+
| PI() * 1000 |
+-------------+
| 3141.592654 |
+-------------+

Compared With Radians & Degrees

The RADIANS() function converts its argument from degrees to radians.

A full circle is 2π.

Therefore, we can use RADIANS() to return π by passing in an argument that represents a half circle (i.e. 180).

SELECT RADIANS(180);

Result:

+-------------------+
| RADIANS(180)      |
+-------------------+
| 3.141592653589793 |
+-------------------+

We can go in the opposite direction by passing PI() to the DEGREES() function:

SELECT DEGREES(PI());

Result:

+---------------+
| DEGREES(PI()) |
+---------------+
|           180 |
+---------------+

Passing Arguments

As mentioned, PI() does not require or accept any arguments. Calling PI() with any arguments results in an error:

SELECT PI(3);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'PI'