Scoop API

scoop:scoop.math:0.1.0

src/internal/decimal/comparison.scoop

Selected production source · src/internal/decimal/comparison.scoop

1package scoop.math
3internal fun decimalCompare(a: DecimalValue, b: DecimalValue): Long {
4 val leftSign = integerSign(a.coefficient)
5 val rightSign = integerSign(b.coefficient)
6 if (leftSign != rightSign) return if (leftSign < rightSign) -1L else 1L
7 if (leftSign == 0) return 0L
8 if (a.scale.compare(b.scale) == 0L) return integerCompare(a.coefficient, b.coefficient)
9 val left = integerString(integerAbs(a.coefficient), 10).toByteArray()
10 val right = integerString(integerAbs(b.coefficient), 10).toByteArray()
11 val order = decimalScale(left.size).minus(a.scale).compare(decimalScale(right.size).minus(b.scale))
12 if (order != 0L) return if (leftSign < 0) -order else order
13 var index = 0L
14 val count = max(left.size, right.size)
15 while (index < count) {
16 val x = if (index < left.size) left[index] else 48.toByte()
17 val y = if (index < right.size) right[index] else 48.toByte()
18 if (x != y) return (if (x < y) -1L else 1L) * leftSign.toLong()
19 index += 1L
20 }
21 return 0L
24internal fun decimalStrip(value: DecimalValue, minimum: DecimalScale?): DecimalValue {
25 if (integerSign(value.coefficient) == 0) return DecimalValue(IntegerStorage.Small(0L), decimalScale(0L))
26 var coefficient = value.coefficient
27 var scale = value.scale
28 while (minimum == None || scale.compare(minimum!!) > 0L) {
29 val divided = integerDivide(coefficient, IntegerStorage.Small(10L))
30 if (integerSign(divided._2) != 0) break
31 coefficient = divided._1
32 scale = scale.minus(1L)
33 }
34 return DecimalValue(coefficient, scale)
37internal fun decimalHash(value: DecimalValue): Long {
38 if (integerSign(value.coefficient) == 0) return 0L
39 val canonical = decimalStrip(value, None)
40 return (integerHash(canonical.coefficient) * 31L + canonical.scale.low.toLong()) * 31L + canonical.scale.high