scoop:scoop.math:0.1.0
src/internal/decimal/floating.scoop
Selected production source · src/internal/decimal/floating.scoop
1
package scoop.math3
private fun decimalFromBinary(significand: ULong, exponent: Long, negative: Boolean): DecimalValue {4
if (significand == 0uL) return DecimalValue(IntegerStorage.Small(0L), decimalScale(0L))5
var coefficient = integerUnsigned(significand)6
var scale = 0L7
if (exponent >= 0L) coefficient = integerShift(coefficient, exponent, true)8
else {9
scale = -exponent10
coefficient = integerMultiply(coefficient, integerPow(IntegerStorage.Small(5L), scale))11
}12
if (negative) coefficient = integerNegate(coefficient)13
return decimalStrip(DecimalValue(coefficient, decimalScale(scale)), Some(decimalScale(0L)))14
}16
internal fun decimalFromDouble(value: Double): DecimalValue {17
if (!value.isFinite()) throw IllegalArgumentException(Some("nonfinite decimal input"))18
val bits = value.toBits().toULong()19
val rawExponent = ((bits shr 52L) and 2047uL).toLong()20
val fraction = bits and 0x000fffffffffffffuL21
return decimalFromBinary(if (rawExponent == 0L) fraction else fraction or 0x0010000000000000uL,22
if (rawExponent == 0L) -1074L else rawExponent - 1075L, value.toBits() < 0L)23
}25
internal fun decimalFromFloat(value: Float): DecimalValue {26
if (!value.isFinite()) throw IllegalArgumentException(Some("nonfinite decimal input"))27
val bits = value.toBits().toUInt()28
val rawExponent = ((bits shr 23L) and 255u).toLong()29
val fraction = bits and 0x007fffffu30
return decimalFromBinary((if (rawExponent == 0L) fraction else fraction or 0x00800000u).toULong(),31
if (rawExponent == 0L) -149L else rawExponent - 150L, value.toBits() < 0)32
}34
// The caller has already bounded the decimal exponent to the target's finite neighborhood.35
private fun decimalBinaryParts(value: DecimalValue, precision: Long, minimum: Long): (ULong, Int) {36
var numerator = integerAbs(value.coefficient)37
var denominator = IntegerStorage.Small(1L)38
if (value.scale.high < 0L) numerator = decimalScaleCoefficient(numerator, value.scale.negate())39
else denominator = decimalPowerTen(value.scale)40
var exponent = magnitudeBits(integerMagnitude(numerator)) - magnitudeBits(integerMagnitude(denominator))41
val order = if (exponent >= 0L) integerCompare(numerator, integerShift(denominator, exponent, true))42
else integerCompare(integerShift(numerator, -exponent, true), denominator)43
if (order < 0L) exponent -= 1L44
val unit = max(minimum, exponent - (precision - 1L))45
if (unit < 0L) numerator = integerShift(numerator, -unit, true)46
else denominator = integerShift(denominator, unit, true)47
val rounded = decimalRoundedQuotient(numerator, denominator, RoundingMode.HalfEven)48
return (integerLowBits(rounded), unit.toInt())49
}51
internal fun decimalDouble(value: DecimalValue): Double {52
val sign = integerSign(value.coefficient)53
if (sign == 0) return 0.054
val adjusted = decimalAdjusted(value)55
if (adjusted.compare(decimalScale(309L)) > 0L) return if (sign < 0) Double.NEGATIVE_INFINITY else Double.POSITIVE_INFINITY56
if (adjusted.compare(decimalScale(-325L)) < 0L) return if (sign < 0) -0.0 else 0.057
val parts = decimalBinaryParts(value, 53L, -1074L)58
val result = scalb(parts._1.toDouble(), parts._2)59
return if (sign < 0) -result else result60
}62
internal fun decimalFloat(value: DecimalValue): Float {63
val sign = integerSign(value.coefficient)64
if (sign == 0) return 0f65
val adjusted = decimalAdjusted(value)66
if (adjusted.compare(decimalScale(39L)) > 0L) return if (sign < 0) Float.NEGATIVE_INFINITY else Float.POSITIVE_INFINITY67
if (adjusted.compare(decimalScale(-46L)) < 0L) return if (sign < 0) -0f else 0f68
val parts = decimalBinaryParts(value, 24L, -149L)69
val result = scalb(parts._1.toFloat(), parts._2)70
return if (sign < 0) -result else result71
}