delphi中的数字很大

发布于 2024-11-27 11:27:35 字数 212 浏览 1 评论 0原文

我正在编写一个程序,并将数字乘以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-12-04 11:27:35

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...

一场信仰旅途 2024-12-04 11:27:35

@Patrick87 是对的;计算机上没有任何整数类型可以容纳这样的数字。
@AlexanderMP 也是对的;你必须等待很长时间才能完成。

忽略所有这些,我认为您正在寻求一种方法来处理不适合整数变量的极大数字。
几年前我遇到过类似的问题,这就是我的处理方法...

回到基础知识并以与使用铅笔和纸相同的方式计算答案。使用字符串变量来保存数字的文本表示形式并创建将添加 & 的函数。将这些字符串相乘。你已经知道这些算法了,你小时候就学过。

如果您有两个函数是 MultiplyNumStrings(Str1, Str2) & AddNumStrings(Str1, Str2) 您的示例代码看起来类似,只是 K 现在是一个字符串而不是 int64:

var
  i : integer;
  k : string;
begin
  k := '1';
  for i:=1 to 200000000 do
  begin
    k := MultiplyNumStrings('5', AddNumStrings(k, '2'));
  end;
end;

此函数将添加由其字符串数字表示的两个数字:

function AddNumStrings (Str1, Str2 : string): string;
var
  i : integer;
  carryStr : string;
  worker : integer;
  workerStr : string;
begin
  Result := inttostr (length(Str1));
  Result := '';
  carryStr := '0';

  // make numbers the same length
  while length(Str1) < length(Str2) do
    Str1 := '0' + Str1;

  while length(Str1) > length(Str2) do
    Str2 := '0' + Str2;

  i := 0;
  while i < length(Str1) do
  begin
    worker := strtoint(copy(Str1, length(str1)-i, 1)) +
              strtoint(copy(Str2, length(str2)-i, 1)) +
              strtoint (carryStr);
    if worker > 9 then
    begin
      workerStr := inttostr(worker);
      carryStr := copy(workerStr, 1, 1);
      result := copy(workerStr, 2, 1) + result;
    end
    else
    begin
      result := inttostr(worker) + result;
      carryStr := '0';
    end;


    inc(i);
  end; { while }
  if carryStr <> '0' then
    result := carryStr + result;
end;

此函数将乘以由其字符串表示的两个数字数字:

function MultiplyNumStrings (Str1, Str2 : string): string;
var
  i, j : integer;
  carryStr : string;
  worker : integer;
  workerStr : string;
  tempResult : string;
begin
  Result := '';
  carryStr := '0';
  tempResult := '';

  // process each digit of str1
  for i := 0 to length(Str1) - 1 do
  begin
    while length(tempResult) < i do
      tempResult := '0' + tempResult;

    // process each digit of str2
    for j := 0 to length(Str2) - 1 do
    begin
      worker := (strtoint(copy(Str1, length(str1)-i, 1)) *
                 strtoint(copy(Str2, length(str2)-j, 1))) +
                strtoint (carryStr);
      if worker > 9 then
      begin
        workerStr := inttostr(worker);
        carryStr := copy(workerStr, 1, 1);
        tempResult := copy(workerStr, 2, 1) + tempResult;
      end
      else
      begin
        tempResult := inttostr(worker) + tempResult;
        carryStr := '0';
      end;
    end; { for }
    if carryStr <> '0' then
      tempResult := carryStr + tempResult;
    carryStr := '0';

    result := addNumStrings (tempResult, Result);
    tempResult := '';
  end; { for }

  if carryStr <> '0' then
    result := carryStr + result;
end;

示例:我们知道 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:

var
  i : integer;
  k : string;
begin
  k := '1';
  for i:=1 to 200000000 do
  begin
    k := MultiplyNumStrings('5', AddNumStrings(k, '2'));
  end;
end;

This function will add two numbers that are represented by their string digits:

function AddNumStrings (Str1, Str2 : string): string;
var
  i : integer;
  carryStr : string;
  worker : integer;
  workerStr : string;
begin
  Result := inttostr (length(Str1));
  Result := '';
  carryStr := '0';

  // make numbers the same length
  while length(Str1) < length(Str2) do
    Str1 := '0' + Str1;

  while length(Str1) > length(Str2) do
    Str2 := '0' + Str2;

  i := 0;
  while i < length(Str1) do
  begin
    worker := strtoint(copy(Str1, length(str1)-i, 1)) +
              strtoint(copy(Str2, length(str2)-i, 1)) +
              strtoint (carryStr);
    if worker > 9 then
    begin
      workerStr := inttostr(worker);
      carryStr := copy(workerStr, 1, 1);
      result := copy(workerStr, 2, 1) + result;
    end
    else
    begin
      result := inttostr(worker) + result;
      carryStr := '0';
    end;


    inc(i);
  end; { while }
  if carryStr <> '0' then
    result := carryStr + result;
end;

This function will multiply two numbers that are represented by their string digits:

function MultiplyNumStrings (Str1, Str2 : string): string;
var
  i, j : integer;
  carryStr : string;
  worker : integer;
  workerStr : string;
  tempResult : string;
begin
  Result := '';
  carryStr := '0';
  tempResult := '';

  // process each digit of str1
  for i := 0 to length(Str1) - 1 do
  begin
    while length(tempResult) < i do
      tempResult := '0' + tempResult;

    // process each digit of str2
    for j := 0 to length(Str2) - 1 do
    begin
      worker := (strtoint(copy(Str1, length(str1)-i, 1)) *
                 strtoint(copy(Str2, length(str2)-j, 1))) +
                strtoint (carryStr);
      if worker > 9 then
      begin
        workerStr := inttostr(worker);
        carryStr := copy(workerStr, 1, 1);
        tempResult := copy(workerStr, 2, 1) + tempResult;
      end
      else
      begin
        tempResult := inttostr(worker) + tempResult;
        carryStr := '0';
      end;
    end; { for }
    if carryStr <> '0' then
      tempResult := carryStr + tempResult;
    carryStr := '0';

    result := addNumStrings (tempResult, Result);
    tempResult := '';
  end; { for }

  if carryStr <> '0' then
    result := carryStr + result;
end;

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?

谁与争疯 2024-12-04 11:27:35

在一个线程中对大量数字执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文