Scoop API

scoop:scoop.math:0.1.0

src/scalar/integer_short.scoop

Selected production source · src/scalar/integer_short.scoop

1package scoop.math
3/** Returns the smaller value. */
4public fun min(a: Short, b: Short): Short = if (a <= b) a else b
6/** Returns the larger value. */
7public fun max(a: Short, b: Short): Short = if (a >= b) a else b
9/** Clamps to inclusive bounds; reversed bounds throw IllegalArgumentException. */
10public fun clamp(value: Short, min: Short, max: Short): Short {
11 if (min > max) throw IllegalArgumentException(Some("reversed math bounds"))
12 return if (value < min) min else if (value > max) max else value
15/** Computes the exact result, throwing ArithmeticException on overflow. */
16public fun addExact(a: Short, b: Short): Short {
17 val result = a + b
18 if (((a xor result) and (b xor result)) < 0) mathOverflow()
19 return result
22/** Computes the exact result, throwing ArithmeticException on overflow. */
23public fun subtractExact(a: Short, b: Short): Short {
24 val result = a - b
25 if (((a xor b) and (a xor result)) < 0) mathOverflow()
26 return result
29/** Multiplies exactly, throwing ArithmeticException on overflow. */
30public fun multiplyExact(a: Short, b: Short): Short {
31 if (b == 0) return 0
32 if ((a == Short.MIN_VALUE && b == -1) || (b == Short.MIN_VALUE && a == -1)) mathOverflow()
33 val result = a * b
34 if (result / b != a) mathOverflow()
35 return result
38/** Adds one with overflow checking. */
39public fun incrementExact(value: Short): Short = addExact(value, 1)
41/** Subtracts one with overflow checking. */
42public fun decrementExact(value: Short): Short = subtractExact(value, 1)
44/** Raises to a nonnegative power with overflow checking; zero to zero is one. */
45public fun powExact(value: Short, exponent: Long): Short {
46 if (exponent < 0L) throw ArithmeticException()
47 var result: Short = 1
48 var factor = value
49 var remaining = exponent
50 while (remaining != 0L) {
51 if ((remaining and 1L) != 0L) result = multiplyExact(result, factor)
52 remaining = remaining shr 1L
53 if (remaining != 0L) factor = multiplyExact(factor, factor)
54 }
55 return result
58/** Returns the absolute value; the minimum value wraps to itself. */
59public fun abs(value: Short): Short = if (value < 0) -value else value
61/** Returns the absolute value, throwing for the minimum integer. */
62public fun absExact(value: Short): Short {
63 if (value == Short.MIN_VALUE) mathOverflow()
64 return abs(value)
67/** Negates with overflow checking. */
68public fun negateExact(value: Short): Short {
69 if (value == Short.MIN_VALUE) mathOverflow()
70 return -value
73/** Returns minus one, zero or one according to the sign. */
74public fun signum(value: Short): Int = if (value < 0) -1 else if (value == 0) 0 else 1
76/** Divides toward zero, throwing on zero divisor or overflow. */
77public fun divideExact(a: Short, b: Short): Short {
78 if (a == Short.MIN_VALUE && b == -1) mathOverflow()
79 return a / b
82/** Divides toward negative infinity; minimum divided by minus one wraps. */
83public fun floorDiv(a: Short, b: Short): Short {
84 val quotient = a / b
85 val remainder = a % b
86 return if (remainder != 0 && (a < 0) != (b < 0)) quotient - 1 else quotient
89/** Divides toward negative infinity; overflow throws. */
90public fun floorDivExact(a: Short, b: Short): Short {
91 val quotient = divideExact(a, b)
92 val remainder = a % b
93 return if (remainder != 0 && (a < 0) != (b < 0)) quotient - 1 else quotient
96/** Returns the remainder corresponding to floor division; zero divisor throws. */
97public fun floorMod(a: Short, b: Short): Short {
98 val remainder = a % b
99 return if (remainder != 0 && (a < 0) != (b < 0)) remainder + b else remainder
102/** Divides toward positive infinity; minimum divided by minus one wraps. */
103public fun ceilDiv(a: Short, b: Short): Short {
104 val quotient = a / b
105 val remainder = a % b
106 return if (remainder != 0 && (a < 0) == (b < 0)) quotient + 1 else quotient
109/** Divides toward positive infinity; overflow throws. */
110public fun ceilDivExact(a: Short, b: Short): Short {
111 val quotient = divideExact(a, b)
112 val remainder = a % b
113 return if (remainder != 0 && (a < 0) == (b < 0)) quotient + 1 else quotient
116/** Returns the remainder corresponding to ceil division; zero divisor throws. */
117public fun ceilMod(a: Short, b: Short): Short {
118 val remainder = a % b
119 return if (remainder != 0 && (a < 0) == (b < 0)) remainder - b else remainder