delphi中的数字很大
我正在编写一个程序,并将数字乘以 5... 例如:
var
i:integer;
k:int64;
begin
k:=1;
for i:=1 to 200000000 do
begin
k:=5*(k+2);
end;
end;
end.
但是当我编译并启动程序时,出现溢出整数错误。我该如何解决这个问题?
I'm writing a program and I multiply numbers by 5... For example:
var
i:integer;
k:int64;
begin
k:=1;
for i:=1 to 200000000 do
begin
k:=5*(k+2);
end;
end;
end.
But when I compıle and start my program I get an overflow integer error. How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
k 的正确值至少为 5^20,000,000 或 2^48,000,000。计算机上没有任何整数类型能够存储它;这是 48,000,000 位,大声喊叫。即使您以二进制形式存储它,也需要 6,000,000 字节(5.7 MB)来存储它。您唯一的希望是任意精度的库,祝您好运。
你想计算什么?您现在正在做的是计算一个数字序列 (k),其中第 i 个元素至少与 5^i 一样大。这在 i = 20,000,000 之前不起作用,除非您使用其他类型的变量......
The correct value of k will be at least 5^20,000,000, or 2^48,000,000. No integer type on a computer is going to be able to store that; that's 48,000,000 bits, for crying out loud. Even if you were to store it in binary, it would take 6,000,000 bytes - 5.7 MB - to store it. Your only hope is arbitary-precision libraries, and good luck with that.
What are you trying to compute? What you are doing right now is computing a sequence of numbers (k) where the ith element is at least as big as 5^i. This won't work up to i = 20,000,000, unless you use other types of variables...
@Patrick87 是对的;计算机上没有任何整数类型可以容纳这样的数字。
@AlexanderMP 也是对的;你必须等待很长时间才能完成。
忽略所有这些,我认为您正在寻求一种方法来处理不适合整数变量的极大数字。
几年前我遇到过类似的问题,这就是我的处理方法...
回到基础知识并以与使用铅笔和纸相同的方式计算答案。使用字符串变量来保存数字的文本表示形式并创建将添加 & 的函数。将这些字符串相乘。你已经知道这些算法了,你小时候就学过。
如果您有两个函数是 MultiplyNumStrings(Str1, Str2) & AddNumStrings(Str1, Str2) 您的示例代码看起来类似,只是 K 现在是一个字符串而不是 int64:
此函数将添加由其字符串数字表示的两个数字:
此函数将乘以由其字符串表示的两个数字数字:
示例:我们知道 int64 的最大值是 9223372036854775807。
如果我们使用上面的例程将 9223372036854775807 x 9223372036854775807 相乘,我们会得到 85070591730234615847396907784232501249。
很酷,对吧?
@Patrick87 is right; no integer type on a computer can hold such a number.
@AlexanderMP is also right; you would have to wait for a very long time for this to finish.
Ignoring all that, I think you’re asking for a way to handle extremely large number that won’t fit in an integer variable.
I had a similar problem years ago and here's how I handled it...
Go back to the basics and calculate the answer the same way you would if you were doing it with pencil and paper. Use string variables to hold the text representation of your numbers and create functions that will add & multiply those strings. You already know the algorithms, you learned it as a kid.
If your have two functions are MultiplyNumStrings(Str1, Str2) & AddNumStrings(Str1, Str2) you sample code would look similar except that K is now a string and not an int64:
This function will add two numbers that are represented by their string digits:
This function will multiply two numbers that are represented by their string digits:
Example: We know the max value for an int64 is 9223372036854775807.
If we multiply 9223372036854775807 x 9223372036854775807 using the above routine we get 85070591730234615847396907784232501249.
Pretty cool, huh?
在一个线程中对大量数字执行 20 亿次乘法?
除非您拥有最先进的液氦冷却超频 CPU,否则您必须等待很长时间才能完成。但如果有的话,你就需要等待很长时间。
看看搜索引擎给出了什么:
如果你幸运的话,其中一个就足够了这种暴行。如果没有 - 祝你好运找到一些东西。
Performing 2 billion multiplications on huge numbers, in one single thread?
Unless you've got a state-of-the-art overclocked CPU cooled with liquid helium, you'd have to wait a whole lot for this to complete. However if you do have, you'd just have to wait for a very long time.
Look what search engines gave out:
if you're lucky, one of them should be enough for this atrocity. If not - good luck finding something.