什么是bash中固定宽度的整数的宽度是多少? [英] What is the width of fixed-width integers in bash?

查看:310
本文介绍了什么是bash中固定宽度的整数的宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

壳牌算术说:

评估在固定宽度的整数完成,不检查是否溢出,
  尽管除以0被捕获并标记为错误。

Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error.

例如:

$ echo $(( 1 << 32 ))
4294967296
$ echo $(( (1 << 64) - 1 ))
0

什么是壳算术整数限制在bash?

What are integer limits in shell arithmetic in bash?

@rici指出说的 POSIX外壳担保签订了整数的范围(如ISO C定义):

@rici pointed out that POSIX shell guarantees signed long integer range (as defined by ISO C):

-2**31+1 to +2**31-1

@约翰Zwinck指出说的 庆典源$ C ​​$ C表示还会将intmax_t 用于

@John Zwinck pointed out that bash source code indicates that intmax_t is used:

所有的算术作为做还会将intmax_t 整数,没有检查溢出

All arithmetic is done as intmax_t integers with no checking for overflow

请问bash的保证的说明文件中,它使用还会将intmax_t 或一些其他的C类型整数?

Does bash guarantee in its documentation that it uses intmax_t or some other C type for integers?

推荐答案

猛砸不会记录整数的precise大小和大小可能因平台不同而不同。

然而,它确实企图都符合POSIX标准,指定算术扩展采用签订长期的算术,其中必须至少有32位,包括符号位。

However, it does make an attempt to conform to Posix, which specifies that arithmetic expansion uses signed long arithmetic, which must be at least 32 bits including the sign bit.

的Posix不需要整数运算进行模2 K k的任何值 [但见注1],虽然庆典共同平台将这样做,而且特别不保证算术运算符的行为完全好像值签订多头。 POSIX甚至允许整数运算的浮点仿真,假设浮点值具有足够precision:

Posix does not require integer arithmetic to be modulo 2k for any value of k [but see Note 1], although bash on common platforms will do so, and it particularly does not guarantee that arithmetic operators will behave exactly as though the values were signed longs. Posix even allows the simulation of integer arithmetic with floating point, provided that the floating point values have sufficient precision:

作为一个扩展,外壳可识别运算前pressions超出上市。外壳可以使用符号整型比签订长期排名大的等级。壳可以使用实浮动型,而不是只要只要它不影响的情况下,结果在没有溢出签署。 (XSH和教派; 2.6.4)

As an extension, the shell may recognize arithmetic expressions beyond those listed. The shell may use a signed integer type with a rank larger than the rank of signed long. The shell may use a real-floating type instead of signed long as long as it does not affect the results in cases where there is no overflow. (XSH §2.6.4)

这将允许一个平台,让仅为32位,例如在使用IEEE-754浮点双打(precision 53位)。而庆典不这样做 - 如记录,庆典使用固定宽度的整数数据类型, - 其他shell的实现可能和便携式code不应该假设。

That would permit the use of IEEE-754 floating point doubles (53 bits of precision) on a platform where long was only 32 bits, for example. While bash does not do so -- as documented, bash uses a fixed-width integer datatype -- other shell implementations might, and portable code should not make assumptions.

注:


  1. Posix的普遍推迟到ISO C标准,但也有一些地方的Posix增加了一个额外的限制,其中一些被标记为扩展名(<大骨节病> CX ):

POSIX.1-2008部分作为ISO C标准的配置文件,它可能会选择进一步限制所允许的ISO C标准改变的行为。这种限制与其他兼容的差异不考虑冲突,即使CX标记丢失。该标记仅用于信息。

POSIX.1-2008 acts in part as a profile of the ISO C standard, and it may choose to further constrain behaviors allowed to vary by the ISO C standard. Such limitations and other compatible differences are not considered conflicts, even if a CX mark is missing. The markings are for information only.

其中的一个附加约束是精确的宽整数类型的存在。标准C要求的各类 int_ {至少,快} {} 8,16,32,64 _t 及其类似物的无符号。它不需要精确的宽度类型,如 int32_t ,除非一些整型资格。一个精确的宽度型必须恰好在其名称(即,没有填充比特)指示的比特数,并且必须具有二进制补码重新presentation。因此, INT32_MIN ,如果它被定义,必须完全-2 31 (宗; 7.20.2.1)。

One of these additional constraints is the existence of exact-width integer types. Standard C requires the types int_{least,fast}{8,16,32,64}_t and their unsigned analogues. It does not require the exact-width types, such as int32_t, unless some integer type qualifies. An exact-width type must have exactly the number of bits indicated in its name (i.e. no padding bits) and must have 2's-complement representation. So INT32_MIN, if it is defined, must be exactly -231 (§7.20.2.1).

然而,POSIX的确实的要求完全的宽度类型 INT {} 8,16,32 _t (以及无符号类似物) ,并且还的int64_t ,如果这种类型的实现提供。尤其是,的int64_t 如果实现支持 _POSIX_V7_LP64_OFF64 编程环境和应用程序正在建设中所需的 _POSIX_V7_LP64_OFF64 编程环境。 (XBD,与教派; 13, stdint.h )(。这些要求都被标记为<大骨节病> CX

However, Posix does require the exact-width types int{8,16,32}_t (as well as the unsigned analogues), and also int64_t if such a type is provided by the implementation. In particular, int64_t is required if the "implementation supports the _POSIX_V7_LP64_OFF64 programming environment and the application is being built in the _POSIX_V7_LP64_OFF64 programming environment." (XBD, §13, stdint.h) (These requirements are marked as CX.)

尽管 int32_t 必须存在,因此必须有一些可用的2的补型的事实,但仍然没有保证签订了是二进制补码,即使是这样,也不能保证整数溢出环绕,而不是,例如,诱捕。

Despite the fact that int32_t must exist, and therefore there must be some 2's complement type available, there is still no guarantee that signed long is 2's-complement, and even if it is, there is no guarantee that integer overflow wraps around rather than, for example, trapping.

最相关的原题,虽然是事实,即使签订了是同一类型的int64_t 键,即使有符号整数溢出环绕,外壳是没有任何义务实际使用签订了算术扩展。它可以使用任何数据类型,只要它不影响在那里没有溢出的情况下的结果。 (XSH,与教派; 2.6.4)

Most relevant to the original question, though, is the fact that even if signed long is the same type as int64_t and even if signed integer overflow wraps around, the shell is not under any obligation to actually use signed long for arithmetic expansion. It could use any datatype "as long as it does not affect the results in cases where there is no overflow." (XSH, §2.6.4)

这篇关于什么是bash中固定宽度的整数的宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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