是否可以使用一个变量来表示编程中无限长度的数字? [英] Is it possible to use one variable to represent numbers of unlimited length in programming?

查看:58
本文介绍了是否可以使用一个变量来表示编程中无限长度的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C#进行了三年的游戏,并且进行过各种模拟,其中数字有时会变大,而 Int32 不足以存储值.最终,即使 Int64 对于我的实验来说也变得不够用,它还是需要几个这样的字段(实际上是可变长度的数组)和一个特殊的属性来正确处理如此大的数字.所以我想知道:有没有一种方法可以声明一个具有无限(事先未知)长度的数字变量,以便我可以放松并让计算机进行数学运算?

I've been using C# for three years to make games and I've played with various simulations where numbers sometimes get big and Int32 is not enough to store the value. Eventually even Int64 became insufficient for my experiments, it took several such fields (actually an array of variable length) and a special property to correctly handle such big numbers. And so I wondered: Is there a way to declare a numeric variable with unlimited (unknown beforehand) length so I can relax and let the computer do the math?

我们可以在纸上写任何我们喜欢的数字,而无需任何特殊的纸.我们还可以在文本文件中键入很多单词,而无需进行特殊的文件系统更改以使其正确保存和加载.是否没有变量声明任何编程语言中的数字谁知道多久?

We can write any kind of number we like on paper without needing any special kind of paper. We can also type a lot of words in a text file without needing special file system alterations to make it save and load correctly. Isn't there a variable to declare a who-knows-how-long-it-will-be number in any programming languages?

推荐答案

大多数现代动态语言,例如Perl6,Tcl8和Ruby,都允许您以数字类型存储无限制(最多可用RAM)大小的数字,这又向前迈进了一步.

Most modern dynamic languages such as Perl6, Tcl8 and Ruby goes one step further by allowing you to store unlimited (up to available RAM) sized numbers in number types.

这些语言中的大多数没有单独的整数和浮点类型,而是单个数字"类型,可以自动转换为需要存储在RAM中的任何数字.某些语言(例如Perl6)甚至在其数字"类型中包含复数.

Most of these languages don't have separate integer and floating point types but rather a single "number" type that automatically gets converted to whatever it needs to be to be stored in RAM. Some, like Perl6, even includes complex numbers in its "number" type.

它在计算机级别的实现方式是默认情况下将数字假定为整数-因此int32或int64.如果需要,则将数字转换为浮点数或双精度数(如果计算或赋值的结果不是整数).如果整数增长太大,则解释器/运行时环境会将其静默转换为bigInt对象/结构(它只是一个大的,可增长的数组或int的链表).

How it's implemented at the machine level is that by default numbers are assumed to be integers - so int32 or int64. If need be numbers are converted to floats or doubles if the result of a calculation or assignment isn't an integer. If the integer grows too large then the interpreter/runtime environment silently converts it to a bigInt object/struct (which is simply a big, growable array or linked-list of ints).

在程序员看来,数字的大小是不受限制的(同样,最多可用RAM).

How it appears to the programmer is that numbers have unlimited size (again, up to available RAM).

仍然,此系统存在一些麻烦(有点像浮点数的0.1 + 0.2!= 0.3问题),因此即使您99.99%的时间都可以忽略它,您仍然需要了解底层实现

Still, there are gotchas with this system (kind of like the 0.1+0.2!=0.3 issue with floats) so you'd still need to be aware of the underlying implementation even if you can ignore it 99.99% of the time.

例如,如果在任何时间点您将超大号转换为浮点数(最有可能是硬件的双精度数),您将失去精度.因为这就是浮点数的工作方式.有时您可能会意外地这样做.例如,在某些语言中,幂函数(如C中的 pow())返回浮点结果.因此,如果将整数增大为另一个整数的幂,则可能会截断结果(如果太大).

For example, if at any point in time you super large number gets converted to a floating point number (most likely a double in hardware) you'll lose precision. Because that's just how floating point numbers work. Sometimes you can do it accidentally. In some languages for example, the power function (like pow() in C) returns a floating point result. So raising an integer to the power of another integer may truncate the result if it's too large.

在大多数情况下,它可以工作.我个人认为这是处理数字的理智方法.许多语言设计师显然已经独立提出了该解决方案.

For the most part, it works. And I personally feel that this is the sane way of dealing with numbers. Lots of language designers have apparently come up with this solution independently.

这篇关于是否可以使用一个变量来表示编程中无限长度的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆