scoop:scoop.math:0.1.0
src/internal/decimal/scale.scoop
Selected production source · src/internal/decimal/scale.scoop
1
package scoop.math3
// Signed two-word intermediates keep scale formulas mathematical until the final representation.4
internal 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 0L8
internal fun plus(other: DecimalScale): DecimalScale {9
val sum = low + other.low10
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)25
}27
internal fun decimalScale(value: Long): DecimalScale = DecimalScale(if (value < 0L) -1L else 0L, value.toULong())28
internal fun decimalScaleProduct(a: Long, b: Long): DecimalScale = DecimalScale(multiplyHigh(a, b), a.toULong() * b.toULong())29
internal fun decimalScaleMax(a: DecimalScale, b: DecimalScale): DecimalScale = if (a.compare(b) >= 0L) a else b30
internal fun decimalScaleMin(a: DecimalScale, b: DecimalScale): DecimalScale = if (a.compare(b) <= 0L) a else b32
internal struct DecimalValue(val coefficient: IntegerStorage, val scale: DecimalScale)34
internal 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 = 1L39
while (rest >= 10uL) { rest /= 10uL; digits += 1L }40
digits41
}42
case Large(_, _) -> integerString(integerAbs(value), 10).byteLength43
}44
internal fun decimalAdjusted(value: DecimalValue): DecimalScale = decimalScale(decimalPrecision(value.coefficient) - 1L).minus(value.scale)45
internal fun decimalNegate(value: DecimalValue): DecimalValue = DecimalValue(integerNegate(value.coefficient), value.scale)46
internal fun decimalAbs(value: DecimalValue): DecimalValue = DecimalValue(integerAbs(value.coefficient), value.scale)48
internal fun decimalPowerTen(exponent: DecimalScale): IntegerStorage {49
val count = exponent.checked()50
if (count < 0L) mathOverflow()51
return integerPow(IntegerStorage.Small(10L), count)52
}54
internal fun decimalScaleCoefficient(value: IntegerStorage, distance: DecimalScale): IntegerStorage {55
if (integerSign(value) == 0) return value56
return integerMultiply(value, decimalPowerTen(distance))57
}