scoop:scoop.math:0.1.0
src/scalar/integer_byte.scoop
Selected production source · src/scalar/integer_byte.scoop
1
package scoop.math3
/** Returns the smaller value. */4
public fun min(a: Byte, b: Byte): Byte = if (a <= b) a else b6
/** Returns the larger value. */7
public fun max(a: Byte, b: Byte): Byte = if (a >= b) a else b9
/** Clamps to inclusive bounds; reversed bounds throw IllegalArgumentException. */10
public fun clamp(value: Byte, min: Byte, max: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {31
if (b == 0) return 032
if ((a == Byte.MIN_VALUE && b == -1) || (b == Byte.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: Byte): Byte = addExact(value, 1)41
/** Subtracts one with overflow checking. */42
public fun decrementExact(value: Byte): Byte = subtractExact(value, 1)44
/** Raises to a nonnegative power with overflow checking; zero to zero is one. */45
public fun powExact(value: Byte, exponent: Long): Byte {46
if (exponent < 0L) throw ArithmeticException()47
var result: Byte = 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: Byte): Byte = if (value < 0) -value else value61
/** Returns the absolute value, throwing for the minimum integer. */62
public fun absExact(value: Byte): Byte {63
if (value == Byte.MIN_VALUE) mathOverflow()64
return abs(value)65
}67
/** Negates with overflow checking. */68
public fun negateExact(value: Byte): Byte {69
if (value == Byte.MIN_VALUE) mathOverflow()70
return -value71
}73
/** Returns minus one, zero or one according to the sign. */74
public fun signum(value: Byte): 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: Byte, b: Byte): Byte {78
if (a == Byte.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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {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: Byte, b: Byte): Byte {118
val remainder = a % b119
return if (remainder != 0 && (a < 0) == (b < 0)) remainder - b else remainder120
}