Scoop API

scoop:scoop.math:0.1.0

src/big_decimal/context.scoop

Selected production source · src/big_decimal/context.scoop

1package scoop.math
3/** Selects how an inexact decimal result is rounded. */
4public enum RoundingMode : Equals<RoundingMode>, Hash, ToString {
5 /** Rounds away from zero. */
6 Up,
7 /** Rounds toward zero. */
8 Down,
9 /** Rounds toward positive infinity. */
10 Ceiling,
11 /** Rounds toward negative infinity. */
12 Floor,
13 /** Rounds to nearest, breaking ties away from zero. */
14 HalfUp,
15 /** Rounds to nearest, breaking ties toward zero. */
16 HalfDown,
17 /** Rounds to nearest, breaking ties toward an even retained digit. */
18 HalfEven,
19 /** Requires an exact result; discarding nonzero digits throws ArithmeticException. */
20 Unnecessary
22 internal fun code(): Long = when (this) {
23 case Up -> 0L; case Down -> 1L; case Ceiling -> 2L; case Floor -> 3L
24 case HalfUp -> 4L; case HalfDown -> 5L; case HalfEven -> 6L; case Unnecessary -> 7L
25 }
26 /** Compares rounding modes. */
27 public override operator fun equals(other: RoundingMode): Boolean = code() == other.code()
28 /** Hashes the rounding mode. */
29 public override fun hash(): Long = code()
30 /** Returns the declared mode name. */
31 public override fun toString(): String = when (this) {
32 case Up -> "Up"; case Down -> "Down"; case Ceiling -> "Ceiling"; case Floor -> "Floor"
33 case HalfUp -> "HalfUp"; case HalfDown -> "HalfDown"; case HalfEven -> "HalfEven"; case Unnecessary -> "Unnecessary"
34 }
37/** Decimal significant-digit precision and explicit rounding; zero precision requires exact results. */
38public struct MathContext(
39 /** The significant-digit limit; operations reject negative precision at their public boundary. */
40 val precision: Long = 0L,
41 /** The rounding policy for an inexact result. */
42 val roundingMode: RoundingMode = RoundingMode.HalfEven
43) : Equals<MathContext>, Hash, ToString {
44 /** Compares both configuration fields. */
45 public override operator fun equals(other: MathContext): Boolean = precision == other.precision && roundingMode == other.roundingMode
46 /** Hashes both configuration fields. */
47 public override fun hash(): Long = precision.hash() * 31L + roundingMode.hash()
48 /** Returns a readable explicit configuration. */
49 public override fun toString(): String = "MathContext(precision=" + precision.toString() + ", roundingMode=" + roundingMode.toString() + ")"
51 /** Named precision configurations; these do not impose IEEE decimal exponent limits. */
52 public companion object {
53 /** Unlimited exact precision, with HalfEven retained as the configured mode. */
54 public val Unlimited: MathContext = MathContext()
55 /** Seven significant digits, rounded to nearest even. */
56 public val Decimal32: MathContext = MathContext(7L)
57 /** Sixteen significant digits, rounded to nearest even. */
58 public val Decimal64: MathContext = MathContext(16L)
59 /** Thirty-four significant digits, rounded to nearest even. */
60 public val Decimal128: MathContext = MathContext(34L)
61 }
64internal fun requireMathContext(context: MathContext) {
65 if (context.precision < 0L) throw IllegalArgumentException(Some("negative decimal precision"))
68internal fun decimalRoundUp(sign: Int, odd: Boolean, half: Long, nonzero: Boolean, mode: RoundingMode): Boolean {
69 if (!nonzero) return false
70 return when (mode) {
71 case Up -> true
72 case Down -> false
73 case Ceiling -> sign > 0
74 case Floor -> sign < 0
75 case HalfUp -> half >= 0L
76 case HalfDown -> half > 0L
77 case HalfEven -> half > 0L || (half == 0L && odd)
78 case Unnecessary -> mathOverflow()
79 }