scoop:scoop.math:0.1.0
src/internal/decimal/comparison.scoop
Selected production source · src/internal/decimal/comparison.scoop
1
package scoop.math3
internal 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 1L7
if (leftSign == 0) return 0L8
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 order13
var index = 0L14
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 += 1L20
}21
return 0L22
}24
internal fun decimalStrip(value: DecimalValue, minimum: DecimalScale?): DecimalValue {25
if (integerSign(value.coefficient) == 0) return DecimalValue(IntegerStorage.Small(0L), decimalScale(0L))26
var coefficient = value.coefficient27
var scale = value.scale28
while (minimum == None || scale.compare(minimum!!) > 0L) {29
val divided = integerDivide(coefficient, IntegerStorage.Small(10L))30
if (integerSign(divided._2) != 0) break31
coefficient = divided._132
scale = scale.minus(1L)33
}34
return DecimalValue(coefficient, scale)35
}37
internal fun decimalHash(value: DecimalValue): Long {38
if (integerSign(value.coefficient) == 0) return 0L39
val canonical = decimalStrip(value, None)40
return (integerHash(canonical.coefficient) * 31L + canonical.scale.low.toLong()) * 31L + canonical.scale.high41
}