我很惊讶地发现带点的数字在 Delphi 中可以整数除以 Variant。简单示例:
var
v: Variant;
begin
v := 2.3;
Tag := 5.1 div v; // 2
Tag := 5.1 mod v; // 1
Tag := 5.1 div 2; // [dcc32 Error] E2015 Operator not applicable to this operand type
Tag := 5.1 mod 2; // [dcc32 Error] E2015 Operator not applicable to this operand type
end;
看起来 Delphi 在执行 div/mod 语句之前四舍五入了表达式的左侧和右侧。
我认为上面的代码在编译所有 4 行时会导致错误,因为 据我了解,div/mod 运算符不适用于带有“完全”一词的点的数字。看起来好像不是。
为什么 Delphi 让我将带点的数字除以 Variant 整数,此功能有什么用?
Variant 类型引用具有以下内容
运营商帮助
由此可知,在表达式中
数字5.1已转换为变体
在运算中使用 Variant 类型时,如果原始值不适合运算,则按照一定的规则将其转换为适合的值。这些规则列在一个特殊的表格中。