clarity-lang / reference

The Clarity Reference
146 stars 34 forks source link

Rename `/` to `div` #28

Open njordhov opened 4 years ago

njordhov commented 4 years ago

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:

    (< (/ (* u100 reject-votes) total-liquid-ustx) (var-get pox-rejection-fraction))) 

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:

    (< (* u100 reject-votes) (* (var-get pox-rejection-fraction) total-liquid-ustx))`

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.