scoop:scoop.math:0.1.0
src/scalar/integer_short.scoop
Selected production source · src/scalar/integer_short.scoop
1
package scoop.math3
/** Returns the smaller value. */4
public fun min(a: Short, b: Short): Short = if (a <= b) a else b6
/** Returns the larger value. */7
public fun max(a: Short, b: Short): Short = if (a >= b) a else b9
/** Clamps to inclusive bounds; reversed bounds throw IllegalArgumentException. */10
public 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 value13
}15
/** Computes the exact result, throwing ArithmeticException on overflow. */16
public fun addExact(a: Short, b: Short): Short {17
val result = a + b18
if (((a xor result) and (b xor result)) < 0) mathOverflow()19
return result20
}22
/** Computes the exact result, throwing ArithmeticException on overflow. */23
public fun subtractExact(a: Short, b: Short): Short {24
val result = a - b25
if (((a xor b) and (a xor result)) < 0) mathOverflow()26
return result27
}29
/** Multiplies exactly, throwing ArithmeticException on overflow. */30
public fun multiplyExact(a: Short, b: Short): Short {31
if (b == 0) return 032
if ((a == Short.MIN_VALUE && b == -1) || (b == Short.MIN_VALUE && a == -1)) mathOverflow()33
val result = a * b34
if (result / b != a) mathOverflow()35
return result36
}38
/** Adds one with overflow checking. */39
public fun incrementExact(value: Short): Short = addExact(value, 1)41
/** Subtracts one with overflow checking. */42
public fun decrementExact(value: Short): Short = subtractExact(value, 1)44
/** Raises to a nonnegative power with overflow checking; zero to zero is one. */45
public fun powExact(value: Short, exponent: Long): Short {46
if (exponent < 0L) throw ArithmeticException()47
var result: Short = 148
var factor = value49
var remaining = exponent50
while (remaining != 0L) {51
if ((remaining and 1L) != 0L) result = multiplyExact(result, factor)52
remaining = remaining shr 1L53
if (remaining != 0L) factor = multiplyExact(factor, factor)54
}55
return result56
}58
/** Returns the absolute value; the minimum value wraps to itself. */59
public fun abs(value: Short): Short = if (value < 0) -value else value61
/** Returns the absolute value, throwing for the minimum integer. */62
public fun absExact(value: Short): Short {63
if (value == Short.MIN_VALUE) mathOverflow()64
return abs(value)65
}67
/** Negates with overflow checking. */68
public fun negateExact(value: Short): Short {69
if (value == Short.MIN_VALUE) mathOverflow()70
return -value71
}73
/** Returns minus one, zero or one according to the sign. */74
public fun signum(value: Short): Int = if (value < 0) -1 else if (value == 0) 0 else 176
/** Divides toward zero, throwing on zero divisor or overflow. */77
public fun divideExact(a: Short, b: Short): Short {78
if (a == Short.MIN_VALUE && b == -1) mathOverflow()79
return a / b80
}82
/** Divides toward negative infinity; minimum divided by minus one wraps. */83
public fun floorDiv(a: Short, b: Short): Short {84
val quotient = a / b85
val remainder = a % b86
return if (remainder != 0 && (a < 0) != (b < 0)) quotient - 1 else quotient87
}89
/** Divides toward negative infinity; overflow throws. */90
public fun floorDivExact(a: Short, b: Short): Short {91
val quotient = divideExact(a, b)92
val remainder = a % b93
return if (remainder != 0 && (a < 0) != (b < 0)) quotient - 1 else quotient94
}96
/** Returns the remainder corresponding to floor division; zero divisor throws. */97
public fun floorMod(a: Short, b: Short): Short {98
val remainder = a % b99
return if (remainder != 0 && (a < 0) != (b < 0)) remainder + b else remainder100
}102
/** Divides toward positive infinity; minimum divided by minus one wraps. */103
public fun ceilDiv(a: Short, b: Short): Short {104
val quotient = a / b105
val remainder = a % b106
return if (remainder != 0 && (a < 0) == (b < 0)) quotient + 1 else quotient107
}109
/** Divides toward positive infinity; overflow throws. */110
public fun ceilDivExact(a: Short, b: Short): Short {111
val quotient = divideExact(a, b)112
val remainder = a % b113
return if (remainder != 0 && (a < 0) == (b < 0)) quotient + 1 else quotient114
}116
/** Returns the remainder corresponding to ceil division; zero divisor throws. */117
public fun ceilMod(a: Short, b: Short): Short {118
val remainder = a % b119
return if (remainder != 0 && (a < 0) == (b < 0)) remainder - b else remainder120
}