cql-engine icon indicating copy to clipboard operation
cql-engine copied to clipboard

Multiply/Divide with Quantity gives wrong result

Open duncand opened this issue 7 years ago • 4 comments

Per CQL spec 9.6.4 Divide: "For division operations involving quantities, the resulting quantity will have the appropriate unit."

Run the following CQL:

define x: 1'g/cm3' / 1'g/cm3'

Actual result:

 1.0 g/cm3

Expected result:

1.0

... or 1.0'' or however you spell some special neutral unit that means no units.

duncand avatar Jan 25 '18 02:01 duncand

These failing CQL Engine tests reflect this bug:

  • CqlArithmeticFunctionsTest -> Divide1Q1Q

duncand avatar Jan 25 '18 02:01 duncand

A related bug exists in the Translator; it doesn't recognize the unit conversion should occur either.

Run the following CQL:

Equivalent(1'g/cm3' / 1'g/cm3', 1.0)

Expected result:

true

Actual result:

Could not resolve call to operator Equivalent with signature (System.Quantity,System.Decimal).

This is what CQL Engine test Divide1Q1Q actually fails with.

duncand avatar Jan 25 '18 03:01 duncand

The described bug is not just with Divide but also with Multiply.

define a : 1.0 'cm' * 2.0 'cm'

This we would expect to result in 2.0 'cm2' but it instead results in 2.0 'cm'.

More generally speaking, any multiply or divide operation with 2 Quantity, the actual result has the same unit as the left operand, no matter what unit the right operand has.

In the general case, the unit of the result of a multiply or divide involving a Quantity should NEVER have the same unit as the operand, except when the second operand is a plain number.

duncand avatar Jan 25 '18 04:01 duncand

These failing CQL Engine tests also reflect this bug:

  • CqlArithmeticFunctionsTest -> Multiply1CMBy2CM

duncand avatar Jan 25 '18 04:01 duncand