The division function in Clarity truncates the result, as expected for integer division. However, the symbol / is used for the operator, which typically is expected to be the operator for exact division. Clarity should use another name such as div for integer division.
In Clarity, multiplication and division are not inverse operations: While 8 / 3 * 3 is 8 in arithmetics, the equivalent in Clarity evaluates to 6 as dividing integers 8 by 3 is 2. This may result in faulty calculations including when formulas are naively implemented in Clarity. For example, take this formula from a draft contract in core, supposed to implement (100 * reject-votes) / total-liquid-ustx < pox-rejection-fraction, but returning an inconsistent result due to the integer division:
Also, trivial transformations of formulas with division may affect the result in surprising ways. For example, this implements the exact formula without rounding; It may have different results than the implementation above even as they appear to implement the same:
Moving forward, Clarity could use / for fractions and exact division with rational numbers. This will make it possible to use the division operator to implement formulas with exact results.
The division function in Clarity truncates the result, as expected for integer division. However, the symbol
/
is used for the operator, which typically is expected to be the operator for exact division. Clarity should use another name such asdiv
for integer division.In Clarity, multiplication and division are not inverse operations: While
8 / 3 * 3
is8
in arithmetics, the equivalent in Clarity evaluates to6
as dividing integers8
by3
is2
. This may result in faulty calculations including when formulas are naively implemented in Clarity. For example, take this formula from a draft contract in core, supposed to implement(100 * reject-votes) / total-liquid-ustx < pox-rejection-fraction
, but returning an inconsistent result due to the integer division:Also, trivial transformations of formulas with division may affect the result in surprising ways. For example, this implements the exact formula without rounding; It may have different results than the implementation above even as they appear to implement the same:
Moving forward, Clarity could use
/
for fractions and exact division with rational numbers. This will make it possible to use the division operator to implement formulas with exact results.