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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With