如何在不使用科学记数法的情况下在Lua中打印大量数字?

时间:2022-01-14 09:21:36

I'm dealing with timestamps in Lua showing the number of microseconds since the Epoch (e.g. "1247687475123456").

我正在处理Lua中的时间戳,显示自Epoch以来的微秒数(例如“1247687475123456”)。

I would really like to be able to print that number in all its terrible glory, but Lua insists on printing it in scientific notation. I've scoured the available documentation about printing a formatted string, but the only available commands are "Print in scientific notation (%e/%E)" and "Automatically print in scientific notation if the number is very long (%g)". No options seem to be available to print the number in its normal form.

我真的希望能够将这个数字印在所有可怕的荣耀中,但Lua坚持用科学记谱法打印它。我已经搜索了有关打印格式化字符串的可用文档,但唯一可用的命令是“以科学记数法打印(%e /%E)”和“如果数字很长(%g),则以科学记数法自动打印” 。似乎没有选项以正常形式打印数字。

I realize that I could write a function that will take the original number, do some dividing by 10 and print the digits in a loop but that seems like an inelegant hassle. Surely there's some way of doing this that's built in to the language?

我意识到我可以编写一个函数来获取原始数字,做一些除以10并在循环中打印数字但这似乎是一个不优雅的麻烦。当然有一些方法可以做到这种内置于语言中的方法吗?

2 个解决方案

#1


> print(string.format("%18.0f",1247687475123456))

1247687475123456

#2


Lua as usually configured uses your platform's usual double-precision floating point format to store all numbers. For most desktop platforms today, that will be the 64-bit IEEE-754 format. The conventional wisdom is that integers in the range -1E15 to +1E15 can be safely assumed to be represented exactly.

通常配置的Lua使用平台通常的双精度浮点格式来存储所有数字。对于今天的大多数桌面平台,这将是64位IEEE-754格式。传统观点认为,可以安全地假设-1E15到+ 1E15范围内的整数被精确表示。

In any case, the string.format() function passes its arguments through (with some minor tweaks) to the platform's implementation of printf(). The format string understood by printf() includes %e and %E to force "scientific" notation, and %f to force plain decimal notation. In addition, %g and %G choose the shortest notation.

无论如何,string.format()函数将其参数(通过一些小的调整)传递给平台的printf()实现。 printf()理解的格式字符串包括强制“科学”表示法的%e和%E,以及强制纯十进制表示法的%f。此外,%g和%G选择最短的符号。

For example:

E:\...>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = 1e17/3
> print(string.format("%f",a))
33333333333333332.000000
> print(string.format("%e",a))
3.333333e+016
> print(string.format("%.0f",a))
33333333333333332

Note that if the value fits within a 32-bit signed integer range, you can also use the %d format. However, results are not well defined if the value exceeds that range. System timestamps in microseconds are likely to exceed the 32-bit range.

请注意,如果值适合32位有符号整数范围,您还可以使用%d格式。但是,如果值超出该范围,则结果不明确。系统时间戳(以微秒为单位)可能超过32位范围。

If 16 decimal digits is not enough precision, there are several choices available for increased precision.

如果16个十进制数字的精度不够,可以选择几种方法来提高精度。

First, it would not be difficult to package a true 64-bit integer in a userdata along with a suitable set of arithmetic metamethods. This gets discussed occasionally on the Lua mailing list, but I don't recall seeing a completed module released by anyone.

首先,在用户数据中打包一个真正的64位整数以及一组合适的算术元方法并不困难。这在Lua邮件列表上偶尔会讨论,但我不记得看到任何人发布完整的模块。

Second, one of the Lua authors has released two modules supporting arbitrary precision arithmetic: lbc and lmapm. Both are found at that page.

其次,Lua的一位作者发布了两个支持任意精度算术的模块:lbc和lmapm。两者都在该页面上找到。

Third, casual searching in Google readily turns up several other math library wrappers.

第三,随意搜索谷歌很容易找到其他几个数学库包装。

#1


> print(string.format("%18.0f",1247687475123456))

1247687475123456

#2


Lua as usually configured uses your platform's usual double-precision floating point format to store all numbers. For most desktop platforms today, that will be the 64-bit IEEE-754 format. The conventional wisdom is that integers in the range -1E15 to +1E15 can be safely assumed to be represented exactly.

通常配置的Lua使用平台通常的双精度浮点格式来存储所有数字。对于今天的大多数桌面平台,这将是64位IEEE-754格式。传统观点认为,可以安全地假设-1E15到+ 1E15范围内的整数被精确表示。

In any case, the string.format() function passes its arguments through (with some minor tweaks) to the platform's implementation of printf(). The format string understood by printf() includes %e and %E to force "scientific" notation, and %f to force plain decimal notation. In addition, %g and %G choose the shortest notation.

无论如何,string.format()函数将其参数(通过一些小的调整)传递给平台的printf()实现。 printf()理解的格式字符串包括强制“科学”表示法的%e和%E,以及强制纯十进制表示法的%f。此外,%g和%G选择最短的符号。

For example:

E:\...>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = 1e17/3
> print(string.format("%f",a))
33333333333333332.000000
> print(string.format("%e",a))
3.333333e+016
> print(string.format("%.0f",a))
33333333333333332

Note that if the value fits within a 32-bit signed integer range, you can also use the %d format. However, results are not well defined if the value exceeds that range. System timestamps in microseconds are likely to exceed the 32-bit range.

请注意,如果值适合32位有符号整数范围,您还可以使用%d格式。但是,如果值超出该范围,则结果不明确。系统时间戳(以微秒为单位)可能超过32位范围。

If 16 decimal digits is not enough precision, there are several choices available for increased precision.

如果16个十进制数字的精度不够,可以选择几种方法来提高精度。

First, it would not be difficult to package a true 64-bit integer in a userdata along with a suitable set of arithmetic metamethods. This gets discussed occasionally on the Lua mailing list, but I don't recall seeing a completed module released by anyone.

首先,在用户数据中打包一个真正的64位整数以及一组合适的算术元方法并不困难。这在Lua邮件列表上偶尔会讨论,但我不记得看到任何人发布完整的模块。

Second, one of the Lua authors has released two modules supporting arbitrary precision arithmetic: lbc and lmapm. Both are found at that page.

其次,Lua的一位作者发布了两个支持任意精度算术的模块:lbc和lmapm。两者都在该页面上找到。

Third, casual searching in Google readily turns up several other math library wrappers.

第三,随意搜索谷歌很容易找到其他几个数学库包装。