I'm comparing some currency values in DUnit but it is not working at all on my machine (work on others, but not on mine).
An example:
CheckEquals(16.65, SomeCurrencyFieldValue);
Raises:
expected: <16,65> but was: <16,65>
if do the following the comparison then works:
var
Temp: Currency;
begin
Temp := 16.65;
CheckEquals(Temp, SomeCurrencyFieldValue);
The question is: Why the comparison doesn't work when I pass the value direct to the CheckEquals method?
The issue has to do with how Currency
values get converted to Extended
values at run time versus how floating-point literals get converted to Extended
values at compile time. If the conversion isn't the same in both cases, then the values passed to CheckEquals
may not compare equal.
It would be worth checking in the debugger's CPU window whether either of the values goes through an intermediate Double
value on its way to Extended
in preparation for the function call. An extra conversion would affect the exact value of the result.
Another thing to consider is that 16.65 is not representable exactly as an Extended
value, but it is representable exactly as a Currency
value. Although Currency
is classified as a floating-point type, it is really a fixed-point scaled 64-bit integer. That's probably grounds for requesting an additional CheckEquals
overload in DUnit that takes that into account.