以更高的精度在Lua中计算值

时间:2020-04-23 03:25:29

标签: lua evaluation calculation evaluate

表达式print(math.pi)pi给出3.1415926535898的值lua 5.3作为math.pi在我的CPU上最多13个小数位。是否有可能获得最多小数位数的值,例如 databaseReference .child('orders') .orderByChild('date_slug') .limitToFirst(pageSize) .onChildAdded .forEach((event) => { print(event.snapshot.value) }); 的值最多50个小数位数。如果是这样,在lua中可以计算出的最高限制是多少?

2 个答案:

答案 0 :(得分:2)

print(string.format("%0.50f", math.pi))

打印3.14159265358979311599796346854418516159057617187500

来自https://www.lua.org/source/5.3/lmathlib.c.html

#define PI      (l_mathop(3.141592653589793238462643383279502884))

为什么我不知道两个数字为何不同。但是,如果要打印的精度比分别打印或tonumber的精度高,则必须自己格式化数字。

答案 1 :(得分:2)

Lua使用双精度浮点;尾数有52位,因此数字限制为大约16位小数。

如果需要更高的精度,请使用多精度库,例如lqd,该库使用具有212个尾数位的四重双精度,并提供约64个小数。然后qd.pi给出3.1415926535897932384626433832795028841971693993751058209749445923。