- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很抱歉问了一个非常基本的问题。考虑以下示例:
const
c1 = 1; // Is this Byte or ShortInt?
c2 = 1234; // Is this Word or Smallint?
c3 = 123456; // Is this Cardinal or Integer?
读完这个 documentation 后,我可以得出的结论是,负值被解释为有符号,正值被解释为无符号。然而,例如123456 (根据文档将被解释为 Cardinal
)也可以在 Integer
的上下文中使用,我的意思是它用于在计算中使用常量的 Integer
变量。因此,是否保证常量始终为Cardinal
,以便有必要将类型转换为Integer
?
最佳答案
documentation(XE8 是我写这篇文章时的最新版本)告诉您真正的常量有一个类型。但是,在指定该类型的实际类型时,该文档具有误导性。当我说误导时,我是有点善意的。
如果您阅读了此官方 documentation,那么您会倾向于相信无符号类型优于有符号类型。但这个程序表明情况并非如此:
program SO32160057_overloads;
{$APPTYPE CONSOLE}
procedure foo(value: UInt8); overload;
begin
Writeln('UInt8');
end;
procedure foo(value: UInt16); overload;
begin
Writeln('UInt16');
end;
procedure foo(value: UInt32); overload;
begin
Writeln('UInt32');
end;
procedure foo(value: UInt64); overload;
begin
Writeln('UInt64');
end;
procedure foo(value: Int8); overload;
begin
Writeln('Int8');
end;
procedure foo(value: Int16); overload;
begin
Writeln('Int16');
end;
procedure foo(value: Int32); overload;
begin
Writeln('Int32');
end;
procedure foo(value: Int64); overload;
begin
Writeln('Int64');
end;
const
ZeroInt32 = Int32(0);
ZeroUInt16 = UInt16(0);
begin
foo(127);
foo(128);
foo(32767);
foo(32768);
foo(2147483647);
foo(2147483648);
foo(9223372036854775807);
foo(9223372036854775808);
foo(ZeroInt32);
foo(ZeroUInt16);
foo(UInt8(0));
end.
输出为:
Int8UInt8Int16UInt16Int32UInt32Int64UInt64Int32UInt16UInt8
Let's have a look at another program:
program SO32160057_comparisons;
var
Int8var: Int8 = 0;
Int16var: Int16 = 0;
Int32var: Int32 = 0;
begin
if Int8var < 127 then ;
if Int8var < 128 then ; // line 10
if Int8var < Int16(128) then ; // line 11
if Int16var < 32767 then ;
if Int16var < 32768 then ; // line 13
if Int16var < Int32(32768) then ; // line 14
if Int32var < 2147483647 then ;
if Int32var < 2147483648 then ; // line 16
if Int32var < Int64(2147483648) then ;
end.
编译器发出以下警告:
(10): W1022 Comparison always evaluates to True(10): W1023 Comparing signed and unsigned types - widened both operands(11): W1022 Comparison always evaluates to True(13): W1022 Comparison always evaluates to True(13): W1023 Comparing signed and unsigned types - widened both operands(14): W1022 Comparison always evaluates to True(16): W1022 Comparison always evaluates to True(16): W1023 Comparing signed and unsigned types - widened both operands
So, by my empirical analysis, the compiler looks at the value of an integral literal, and determines its type by finding the first type in the following list which can represent the value:
Int8
UInt8
Int16
UInt16
Int32
UInt32
Int64
UInt64
This rule can be overridden by specifying the type using the typecast syntax. For instance, to declare an Int32
with value 0
you would write Int32(0)
.
Now let us apply that rule to the concrete example that you give in the question, namely 123456
. According to the rules above, the first type in the list which can represent this value is Int32
. Also known as Integer
.
Now, because this is an unsigned type, you might expect that a comparison against an unsigned UInt32
variable will result in warning W1023, comparing signed and unsigned types. But that is not the case. The compiler recognises that 123456
is a positive value and that we are comparing two positive values. On the other hand, the warning is emitted with -123456
.
program SO32160057_123456;
var
UInt32var: UInt32 = 0;
begin
if UInt32var > 123456 then ; // line 7
if UInt32var > -123456 then ; // line 8
end.
编译器发出以下警告:
(8): W1022 Comparison always evaluates to True(8): W1023 Comparing signed and unsigned types - widened both operands
关于delphi - 整数真常数的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160057/
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Why can't Python handle true/false values as I expect?
我是不是遗漏了什么或者这是 ruby 中的错误? a = %w(foo bar baz) a.include? "foo" # => true a.size == 3
从 Modelica 文档来看,注释 Evaluate 似乎只对参数有影响: https://build.openmodelica.org/Documentation/ModelicaReferenc
为了避免嵌套的 if 语句并提高可读性,我想创建一个switch(true){ ... } Coldfusion 中的声明。我在 php 中经常使用这个,但是当我在 Coldfusion 中尝试这个时
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
这个问题在这里已经有了答案: How can I return pivot table output in MySQL? (10 个答案) 关闭 5 年前。 我正在尝试构建一个以唯一列值作为列名的表
我制作了一个简单的 JDialog,其中包含一个标签和一个按钮,它基本上相当于信息对话框。所以在对话框中,有一个方法 display() 我在其中调用了 setVisible(true) 五次。 据我
在 bash 4.2.8(1)-release (x86_64-pc-linux-gnu) 在 Ubuntu 11.04 上这个命令 [ $(wc -l /var/www/some.log|cut -
我正在使用 c 语言进行并发处理,我有一个进程池。为此,我让每个 child 都在一个 While (True) 循环中。为了杀死 child ,我正在使用一个全局变量和一个信号处理程序来修改它来打破
我正在尝试选择填写了字段的数据库条目。数据库有两种插入数据的方式,一种输入评论,一种不输入,我希望只选择填写了评论的行。 $requete = "SELECT * FROM daysoff WHER
如何在 JavaMail session 中setDebug(true) 捕获流并在我的日志记录框架中使用它? (缺少下载源代码,更改接受流作为参数的方法,重新编译它,...) 更一般地说,Java
我是 JavaScript 的新手,我刚刚发现了我无法理解的奇怪行为: var magicVar = Math.sin; magicVar == true; // it returns false m
对此感到困惑。 在两台服务器上运行相同版本的 MySQL。 (从完全相同的 rpm 构建)- 沿线的某个地方,一些开发人员改变了一些东西...... 服务器 1: mysql> select ( no
我在查看 OpenSSL 中使用的一些预处理器宏时,从 crypto/stack/safestack.h 中发现了以下内容: #define CHECKED_STACK_OF(type, p) \
所以我遇到了一个问题,我的正则表达式看起来像这样:/true|false/。 当我检查单词 falsee 时,我从这个正则表达式中得到一个 true,有没有办法将它限制为确切的 true 或 fals
我正在对这个恶意 JavaScript 行进行一些试验:var undefined = true; JavaScript 中每个未初始化的变量都有 undefined 的值,这只是一个保存特殊值 'u
我想将 PHP 的微时间存储为我在 MySQL 中的时间戳。 我去过told最好用 DECIMAL 存储它,但我找不到理想的大小。 有谁知道 microtime(true) 返回的最大大小是多少,所以
在 PHP 中,当您在 URL 中包含诸如“var=true”之类的内容时,URL 中的“true”和“false”是否被转换为 boolean 变量,或者它们是否等于文本“true”还是“假”?例如
我是一名优秀的程序员,十分优秀!