The expression print(math.pi)
gives values of pi
as 3.1415926535898
up to 13 decimal places on my CPU with lua 5.3
. Is it possible to get value up to more number of decimal places, for example value of math.pi
upto 50 decimal places. If so, what would be the highest limit to which it can be calculated in lua?
Asked
Active
Viewed 430 times
2

Maths89
- 476
- 3
- 15
2 Answers
2
print(string.format("%0.50f", math.pi))
prints 3.14159265358979311599796346854418516159057617187500
From https://www.lua.org/source/5.3/lmathlib.c.html
#define PI (l_mathop(3.141592653589793238462643383279502884))
Why both numbers differ I don't know. But if you want to print with higher precision than print or tonumber respectively does you have to format the number yourself.

Piglet
- 27,501
- 3
- 20
- 43
-
2Despite of format `%0.50f` you have only first 52 bits correct in `math.pi`. – Egor Skriptunoff Apr 23 '20 at 08:43
2
Lua uses double precision floating point; the mantissa has 52 bits and so numbers are limited to about 16 decimals.
If you need more precision, use a multiple precision library, such as lqd, which uses quad-double precision with 212 mantissa bits and gives about 64 decimals. Then qd.pi
gives 3.1415926535897932384626433832795028841971693993751058209749445923.

lhf
- 70,581
- 9
- 108
- 149