Scoop API

scoop:scoop.math:0.1.0

src/internal/decimal/scale.scoop

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

1package scoop.math
3// Signed two-word intermediates keep scale formulas mathematical until the final representation.
4internal struct DecimalScale(val high: Long, val low: ULong) {
5 internal fun compare(other: DecimalScale): Long = if (high != other.high) (if (high < other.high) -1L else 1L)
6 else if (low < other.low) -1L else if (low > other.low) 1L else 0L
8 internal fun plus(other: DecimalScale): DecimalScale {
9 val sum = low + other.low
10 return DecimalScale(high + other.high + (if (sum < low) 1L else 0L), sum)
11 }
12 internal fun negate(): DecimalScale = DecimalScale(high.inv() + (if (low == 0uL) 1L else 0L), 0uL - low)
13 internal fun minus(other: DecimalScale): DecimalScale = plus(other.negate())
14 internal fun plus(value: Long): DecimalScale = plus(decimalScale(value))
15 internal fun minus(value: Long): DecimalScale = minus(decimalScale(value))
16 internal fun halfFloor(): DecimalScale = DecimalScale(high shr 1L, (low shr 1L) or (high.toULong() shl 63L))
17 internal fun halfCeil(): DecimalScale = halfFloor().plus((low and 1uL).toLong())
18 internal fun fitsLong(): Boolean = (high == 0L && low <= Long.MAX_VALUE.toULong()) || (high == -1L && low >= 0x8000000000000000uL)
19 internal fun checked(): Long {
20 if (!fitsLong()) mathOverflow()
21 return low.toLong()
22 }
23 internal fun integer(): IntegerStorage = integerAdd(integerShift(IntegerStorage.Small(high), 64L, true), integerUnsigned(low))
24 internal fun text(): String = if (fitsLong()) low.toLong().toString() else integerString(integer(), 10)
27internal fun decimalScale(value: Long): DecimalScale = DecimalScale(if (value < 0L) -1L else 0L, value.toULong())
28internal fun decimalScaleProduct(a: Long, b: Long): DecimalScale = DecimalScale(multiplyHigh(a, b), a.toULong() * b.toULong())
29internal fun decimalScaleMax(a: DecimalScale, b: DecimalScale): DecimalScale = if (a.compare(b) >= 0L) a else b
30internal fun decimalScaleMin(a: DecimalScale, b: DecimalScale): DecimalScale = if (a.compare(b) <= 0L) a else b
32internal struct DecimalValue(val coefficient: IntegerStorage, val scale: DecimalScale)
34internal fun decimalPrecision(value: IntegerStorage): Long = when (value) {
35 case Small(val number) -> {
36 // Small coefficients stay scalar; counting digits does not allocate a magnitude or text.
37 var rest = if (number < 0L) 0uL - number.toULong() else number.toULong()
38 var digits = 1L
39 while (rest >= 10uL) { rest /= 10uL; digits += 1L }
40 digits
41 }
42 case Large(_, _) -> integerString(integerAbs(value), 10).byteLength
44internal fun decimalAdjusted(value: DecimalValue): DecimalScale = decimalScale(decimalPrecision(value.coefficient) - 1L).minus(value.scale)
45internal fun decimalNegate(value: DecimalValue): DecimalValue = DecimalValue(integerNegate(value.coefficient), value.scale)
46internal fun decimalAbs(value: DecimalValue): DecimalValue = DecimalValue(integerAbs(value.coefficient), value.scale)
48internal fun decimalPowerTen(exponent: DecimalScale): IntegerStorage {
49 val count = exponent.checked()
50 if (count < 0L) mathOverflow()
51 return integerPow(IntegerStorage.Small(10L), count)
54internal fun decimalScaleCoefficient(value: IntegerStorage, distance: DecimalScale): IntegerStorage {
55 if (integerSign(value) == 0) return value
56 return integerMultiply(value, decimalPowerTen(distance))