为什么Delphi和Free Pascal通常更喜欢一个有符号整数的数据类型来使用无符号数据类型? [英] Why do Delphi and Free Pascal usually prefer a signed-integer data type to unsigned one?

查看:319
本文介绍了为什么Delphi和Free Pascal通常更喜欢一个有符号整数的数据类型来使用无符号数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个Pascal的新手,但直到现在还不知道为什么 Delphi 免费Pascal 通常将参数和返回值声明为有符号整数,而我看到它们应始终为正。例如:

I'm not a Pascal newbie, but I still don't know until now why Delphi and Free Pascal usually declares parameters and returned values as signed integers whereas I see them should always be positive. For example:


  • Pos()返回Integer类型。是否可以是否定的?

  • SetLength()声明 NewLength 参数作为整型的一种类型。字符串有负数吗?

  • System.THandle 声明为Longint。句柄是否有负数?

  • Pos() returns type of Integer. Is it possible to be a negative?
  • SetLength() declares the NewLength parameter as a type of Integer. Is there a negative length for string?
  • System.THandle declared as Longint. Is there a negative number for handles?

有很多像Delphi和Free Pascal这样的决定。背后有什么注意事项?

There are many decisions like those in Delphi and Free Pascal. What considerations were behind this?

推荐答案

在Pascal中,整数(已签名)是基本类型。所有其他整数类型是整数的子范围。 (这在Borland方言中并不完全正确,在德尔福的TP和int64中给出了长度,但足够接近)。

In Pascal, Integer (signed) is the base type. All other integer number types are a subranges of integer. (this is not entirely true in Borland dialects, given longint in TP and int64 in Delp but close enough).

如果计算的中间结果,一个重要的原因得到负数,并且您以无符号整数计算,范围检查错误将触发,并且由于大多数较旧的编程语言不会假定为2补数整数,因此结果(范围检查关闭)甚至可能已损坏。

An important reason for that if the intermediate result of calculations gets negative, and you calculate with unsigned integers, range check errors will trigger, and since most older programming languages DON'T assume 2-complement integers, the result (with range checks off) might even be corrupt.

THandle案例简单得多。 Delphi没有一个正确的32位无符号到D4,但只有一个31位的基数。 (由于32位无符号整数不是整数的子范围,后来的unsigned int是int64的一个子集,它将问题转移到仅在D2010左右添加的uint64)

The THandle case is much simpler. Delphi didn't have a proper 32-bit unsigned till D4, but only a 31-bit cardinal. (since 32-bit unsigned integer is not a subrange of integer, the later unsigned ints are a subset of int64, which moved the problem to uint64 which was only added in D2010 or so)

所以在许多地方,标头的类型被用于winapi使用无符号类型,可能是为了避免在这些版本中第32位被破坏,并且定制卡住了。

So in many places in the headers signed types are used where the winapi uses unsigned types, probably to avoid the 32th bit getting corrupt in those versions, and the custom stuck.

但是,winapi案例与一般情况不同。

But the winapi case is different from the general case.

稍后添加 一些Pascal(和Modula2 / 3)实现通过将整数设置为大于词语大小的整数来规避此陷阱,并要求所有数字类型声明适当的子范围,如下面的程序。

Added later Some Pascal (and Modula2/3) implementations circumvent this trap by setting the integer at a size larger than the wordsize, and require all numeric types to declare a proper subrange, like in the below program.

第一个主要假设是一切都是整数的一个子集,第二个允许编译器再次缩放几乎所有的东西以适应寄存器,特别是如果CPU有一些操作大于字操作。 (如x86,其中32位* 32位mul提供64位结果,或者可以使用状态位检测词溢出溢出(例如,为添加而生成范围异常,而不进行完整的2 * wordize添加)

The first holds the primary assumption that everything is a subset of integer, and the second allows the compiler to scale nearly everything down again to fit in registers, specially if the CPU has some operations for larger than word operations. (like x86 where 32-bit * 32-bit mul gives a 64-bit result, or can detect wordsize overflows using status bits (e.g. to generate range exceptions for adds without doing a full 2*wordsize add)

   var x : 0..20;
       y : -10..10;

   begin
     // any expression of x and y has a range -10..20

这篇关于为什么Delphi和Free Pascal通常更喜欢一个有符号整数的数据类型来使用无符号数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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