add bounds checks to arccos and arcsin
This commit is contained in:
parent
9b13e218ba
commit
70aaf42d8f
|
@ -13,6 +13,7 @@ import at.petrak.hexcasting.api.casting.iota.Iota
|
||||||
import at.petrak.hexcasting.api.casting.math.HexPattern
|
import at.petrak.hexcasting.api.casting.math.HexPattern
|
||||||
import at.petrak.hexcasting.api.casting.mishaps.MishapDivideByZero
|
import at.petrak.hexcasting.api.casting.mishaps.MishapDivideByZero
|
||||||
import at.petrak.hexcasting.common.casting.arithmetic.operator.OperatorLog
|
import at.petrak.hexcasting.common.casting.arithmetic.operator.OperatorLog
|
||||||
|
import at.petrak.hexcasting.common.casting.arithmetic.operator.asDoubleBetween
|
||||||
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
|
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
|
||||||
import java.util.function.DoubleBinaryOperator
|
import java.util.function.DoubleBinaryOperator
|
||||||
import java.util.function.DoubleUnaryOperator
|
import java.util.function.DoubleUnaryOperator
|
||||||
|
@ -51,7 +52,7 @@ object DoubleArithmetic : Arithmetic {
|
||||||
|
|
||||||
override fun getOperator(pattern: HexPattern): Operator {
|
override fun getOperator(pattern: HexPattern): Operator {
|
||||||
return when (pattern) {
|
return when (pattern) {
|
||||||
ADD -> make2 { a, b -> java.lang.Double.sum(a, b) }
|
ADD -> make2 { a, b -> a + b }
|
||||||
SUB -> make2 { a, b -> a - b }
|
SUB -> make2 { a, b -> a - b }
|
||||||
MUL -> make2 { a, b -> a * b }
|
MUL -> make2 { a, b -> a * b }
|
||||||
DIV -> make2 { a, b -> if (b == 0.0) throw MishapDivideByZero.of(a, b) else a / b }
|
DIV -> make2 { a, b -> if (b == 0.0) throw MishapDivideByZero.of(a, b) else a / b }
|
||||||
|
@ -62,8 +63,8 @@ object DoubleArithmetic : Arithmetic {
|
||||||
SIN -> make1 { a -> sin(a) }
|
SIN -> make1 { a -> sin(a) }
|
||||||
COS -> make1 { a -> cos(a) }
|
COS -> make1 { a -> cos(a) }
|
||||||
TAN -> make1 { a -> if (cos(a) == 0.0) throw MishapDivideByZero.tan(a) else tan(a) }
|
TAN -> make1 { a -> if (cos(a) == 0.0) throw MishapDivideByZero.tan(a) else tan(a) }
|
||||||
ARCSIN -> make1 { a -> asin(a) }
|
ARCSIN -> make1 { a -> asin(a.asDoubleBetween(-1.0, 1.0, 0)) }
|
||||||
ARCCOS -> make1 { a -> acos(a) }
|
ARCCOS -> make1 { a -> acos(a.asDoubleBetween(-1.0, 1.0, 0)) }
|
||||||
ARCTAN -> make1 { a -> atan(a) }
|
ARCTAN -> make1 { a -> atan(a) }
|
||||||
ARCTAN2 -> make2 { a, b -> atan2(a, b) }
|
ARCTAN2 -> make2 { a, b -> atan2(a, b) }
|
||||||
LOG -> OperatorLog
|
LOG -> OperatorLog
|
||||||
|
|
|
@ -57,4 +57,11 @@ fun Iterator<IndexedValue<Iota>>.nextPositiveIntUnderInclusive(max: Int, argc: I
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max)
|
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the double if it is between [min] and [max] (inclusive), and throws a mishap otherwise. [idx] should be
|
||||||
|
* the double's index from the top of the stack (i.e. top iota has [idx]=0, second from the top has [idx]=1, etc.).
|
||||||
|
*/
|
||||||
|
fun Double.asDoubleBetween(min: Double, max: Double, idx: Int) = if (this in min .. max) this
|
||||||
|
else throw MishapInvalidIota.of(DoubleIota(this), idx, "double.between", min, max)
|
Loading…
Reference in a new issue