Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I divide Single by Variant in Delphi, and what is it for?

I was surprised to see, that floating-point values could be divided by a Variant in Delphi. A simple example of what can be done:

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;

It looks like Delphi rounds the left-part and right-part before doing the div/mod operation.

I would expect above code to produce errors at compile-time in all 4 lines, since my understanding is that div/mod are not applicable to floating-point values no matter what. Clearly this is not the case.

Why can I divide Single by Variant in Delphi, and what is it needed for?

like image 854
Kromster Avatar asked Mar 10 '23 13:03

Kromster


1 Answers

From Variants in Expressions:

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

This means that the float typed literal is converted to a variant first.

Then both variants are implicitly converted to integer values to match the operator before doing the div/mod operation. Variant Type Conversions

..and what is it needed for?

Just to be as versatile as possible and fully support OLE.

like image 135
LU RD Avatar answered May 03 '23 19:05

LU RD