0

我正在使用 32 位 Linux 机器并使用 TCL 8.5 版本。当我想计算超过 4 字节的整数时,我得到了错误。但我可以使用“Mpexpr”包做同样的事情。我的疑问是,我的数据总线大小是 32 位,但是当我安装 Mpexpr 包时,我能够处理 8 字节整数,Mpexpr 如何工作以及 32 位数据总线如何突然扩大到 64 位。我想知道内部的东西是如何运作的。

% set tcl_platform(wordSize)

4

%
4

1 回答 1

0

I think you might be slightly confused. Tcl 8.5 internally uses its own arbitrary-precision integer arithmetic library (a fork of libtommath) and it should always behave as if integers are of infinite width; the actual representation is swapped back and forth between a C long, long long and the real arbitrary-precision type as necessary (a good thing because using native numeric types is far faster). You should never be able to write a script that observes the difference other than by using the explicit “casting” Tcl functions int() and wide().

It was much more likely that you'd observe the issue you describe when using Tcl 8.4 (or before) where there was no built-in arbitrary-precision arithmetic support and you had to use the mpexpr package.

As for how things work, that fundamentally stems from the fact that values in Tcl do not need to be represented as system integers; there are many other possible representations. For example, a long long could be used and that's a 64-bit wide integer on normal 32-bit systems. For larger types, the representation is actually typically as a sequence of “digits” where each “digit” is in the range 0–255; arithmetic operations then just follow conventional rules for long addition, long multiplication, etc. just as is taught in schools. Except with rather more than ten digits to consider. (Indeed, they could use a C unsigned short instead of an unsigned char for each digit, giving a range 0–65535.) There are quite a few specialised algorithms that let you do the more advanced operations fairly quickly; I'll let you do your own literature searches…</p>

于 2013-09-11T12:32:10.750 回答