哪些脚本语言可以很好地支持长整数(64 位)?
Perl 长期以来一直是我选择的脚本语言,但我遇到了一个可怕的问题。默认情况下,不支持长(64 位)整数。大多数时候,整数只是一个字符串,它们可以在大文件中查找,但有很多地方它们不起作用,例如二进制 &
、printf
、打包
、解包
、<<
、>>
。
现在,这些确实可以在较新版本的 Perl 中工作,但前提是它是使用 64 位整数支持构建的,如果我想让可移植代码在没有此选项的 Perls 上运行,这没有帮助。而且您并不总是能够控制代码运行的系统上的 Perl。
我的问题是 Python、PHP 和 Ruby 是否存在这样的问题,或者它们是否也依赖于版本和构建选项?
Perl has long been my choice scripting language but I've run into a horrible problem. By default there is no support for long (64 bit) integers. Most of the time an integer is just a string and they work for seeking in huge files but there are plenty of places they don't work, such as binary &
, printf
, pack
, unpack
, <<
, >>
.
Now these do work in newer versions of Perl but only if it is built with 64-bit integer support, which does not help if I want to make portable code to run on Perls built without this option. And you don't always get control over the Perl on a system your code runs on.
My question is do Python, PHP, and Ruby suffer from such a problem, or do they also depend on version and build options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
高速硬件整数的大小(假设语言有它们)将始终取决于编译语言解释器(通常是 C)的编译器可用的整数大小。
如果您需要跨平台/跨版本大整数支持,Perl pragma
use bigint;
就可以解决问题。如果您需要更多控制,bigint
是模块Math::BigInt
的包装器。在加载
use bigint;
的作用域中,该作用域中的所有整数都将透明地升级为Math::BigInt
数字。最后,当使用任何类型的大数库时,请确保不要使用像9**9**9
这样的技巧来获得无穷大,因为你可能会等待一段时间:)The size of high speed hardware integers (assuming the language has them) will always be dependent on whatever size integers are available to the compiler that compiled the language interpreter (usually C).
If you need cross-platform / cross-version big integer support, the Perl pragma
use bigint;
will do the trick. If you need more control,bigint
is a wrapper around the moduleMath::BigInt
.In the scope where
use bigint;
is loaded, all of the integers in that scope will be transparently upgraded toMath::BigInt
numbers. Lastly, when using any sort of big number library, be sure to not use tricks like9**9**9
to get infinity, because you might be waiting a while :)在 Python 中,永远不会出现溢出。相反,Python 自动切换它正在使用的数字的实现。基本实现使用平台上的本机整数,但长整数使用无限长度数字实现。因此,你永远不必担心你的数字变得太大,Python 会自然地处理它。
In Python, you never get overflows. Instead, python switches the implementation of numbers it is using automatically. The basic implementation uses the native ints on the platform, but long integers use an infinite length number implementation. As a result, you never have to worry about your numbers becoming too large, python just handles it naturally.
从用户角度来看,Tcl 8.5 的长整数支持非常好。在内部,它将整数表示为保存它们所需的任何类型(最多并包括 bigint),并且消耗整数的事物将采用它们中的任何一个(尽管可能会施加它们自己的限制;您真的不想使用一个会只适合作为 Unix 文件模式的 bigint...)
您真正需要考虑它的唯一时间是当您要转入或转出某些固定宽度的二进制格式时。但这是相当明显的(毕竟它是固定宽度的)。
Tcl 8.5's long integer support is pretty good from a user perspective. Internally, it represents integers as whatever type is necessary to hold them (up to and including bigints) and things that consume integers will take any of them (though might impose their own limits; you don't really want to use a number that will only fit in a bigint as a Unix file mode...)
The only time you really need to think about it at all is when you're going to/from some fixed-width binary format. That's reasonably obvious though (it's fixed width after all).
先生,请问 bigint 和 Math::BigInt 是核心模块的一部分。只要
使用
其中之一,它就可以在任何平台上运行。Excuse me sir, bigint and Math::BigInt are part of core modules. Just friggin'
use
one of them, it will work on any platform.