加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么我可以在Delphi中将Single替换为Variant,它的用途是什么?

发布时间:2020-12-15 09:21:38 所属栏目:大数据 来源:网络整理
导读:我惊讶地发现,浮点值可以用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
我惊讶地发现,浮点值可以用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;

在执行div / mod操作之前,看起来Delphi会对左部分和右部分进行舍入.

我希望上面的代码在编译时在所有4行中产生错误,因为我的理解是div / mod不适用于浮点值,无论如何.显然情况并非如此.

为什么我可以在Delphi中将Single by Variant分开,它需要什么?

解决方法

从 Variants in Expressions开始:

If an expression combines variants with statically-typed values,the statically-typed values are automatically converted to variants.

这意味着浮点型文字首先转换为变量.

然后在执行div / mod操作之前,将两个变量隐式转换为整数值以匹配运算符.
Variant Type Conversions

..and what is it needed for?

只是为了尽可能多才多艺并完全支持OLE.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读