带有负整数的简单数学(TIntegerHelper)导致计算错误

问题描述 投票:0回答:1

问题很简单。请问是我白痴还是Delphi有问题。我在 64 位 Windows 上有一个简单的 32 位 Windows 应用程序。单击按钮的唯一代码是:

Label1.Caption := ((1)-(-5)).ToString;

标签显示“6”。所以我想知道.. Wtf?

我什至将数字放在大括号中,这样编译器就不会误解我的意思。也试过这个好玩的:

Label1.Caption := Integer((1)-(-5)).ToString;

最后我期待'-4'。

Delphi 版本 10.4 update 2,社区版。 Windows 11.

delphi math integer helper
1个回答
0
投票

在数学中,就像在编码中一样,减去负值与该值的正数相同。 负值与减去该值的正值相同。

所以,

(1)-(-5)
(1)+(5)
相同,即
6
.

并且,

(1)+(-5)
(1)-(5)
相同,即
-4

所以,如果你想让你的

Label
显示
-4
,使用addition而不是subtraction,例如:

Label1.Caption := ((1)+(-5)).ToString;
© www.soinside.com 2019 - 2024. All rights reserved.