Bash 数量限制? [英] Bash Number Limit?

查看:25
本文介绍了Bash 数量限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过一个问题,涉及拉大从文本文件中提取素数并将它们放入另一个文件中.它应该抓取每个素数,包括 2^32 之后的第一个素数,但由于某种原因,该脚本停止工作.

I asked a question earlier that involved pulling large primes from a text file and putting them into another file. It was supposed to grab every prime up to and including the first prime after 2^32, and for some reason this script stopped working.

#!/bin/bash
n=4294967296
last=0
while read number
    do
    if [ $last -gt $n ]
    then break
    fi
    echo $number
last=$number
done < primes.txt > primes2.txt

它最终循环了这 11 个数字:

It ended up looping through these 11 numbers:

4232004449  
4232004479  
4232004493  
4232004509  
4232004527  
4232004533  
4232004559  
4232004589  
4232004593  
4232004613  
004437

原始文件中没有 004437,我的 bash 将处理超过 8999999999999999999 的数字

The original file didn't have 004437 in it, and my bash will handle numbers over 8999999999999999999

有人知道为什么会这样吗?

Does anybody have a clue as to why this happened?

64 位 Ubuntu 10.04、16GB RAM、8 核 @ 3.60 GHz
GNU bash,版本 4.1.5(1)-release (x86_64-pc-linux-gnu)

64-bit Ubuntu 10.04, 16GB RAM, 8-cores @ 3.60 GHz
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

更新:

下载并编译 jfgagne 提供的固定"bash 并在我的 bash 脚本中链接到它后,它在同一个确切位置出错.使用我最初的主要问题中明显更快的 perl 等效项,我从 ls -al 获得了一些文件大小:

After downloading and compiling the "fixed" bash provided by jfgagne and linking to it in my bash script, it errored out at the same exact spot. Using the significantly faster perl equivalent from my original prime question, I got some file sizes from ls -al:

        11  next_prime (just to make sure this was counting bytes accurately)
2147483659  primes2.txt
2147483670  one_too_many

2147483659 = 2^31 + 11

2147483659 = 2^31 + 11

下一个素数 (4232004631) 的大小为 11 个字节这将所有素数保存到 4232004613.我还意识到 004437 来自此错误循环底部的素数末尾(4232004437).似乎有什么东西在试图推进,但卡住了.

The size of the next prime (4232004631) is 11 bytes This holds all primes up to 4232004613. I also realized that the 004437 is coming from the end of the prime at the bottom of this error loop (4232004437). It seems like something is trying to advance, but stuck.

推荐答案

不,它与您的操作系统的位大小无关.这取决于 bash 源代码中的一个简单声明.

No, it is independent from the bitsize of your OS. It depends upon a simple declaration deep in the bash sources.

自 2002 年左右以来,此声明为 __int64_t.从那时起,bash 版本 >= 3.00 始终使用 64 位整数变量,并且不依赖于体系结构!它仅取决于 bash 版本.

This declaration is __int64_t since around 2002. Since then, bash versions >= 3.00 always use 64bit integer variables, and it doesn't depend on the architecture! It only depends on the bash version.

以前版本的 bash 始终使用 32 位整数,即使在 64 位操作系统上也是如此.

Previous versions of bash used always 32 bit integer, even on 64 bit OS.

这篇关于Bash 数量限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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