From 70aaf42d8f9ac5a30c46cc3b70afebc5d20ae489 Mon Sep 17 00:00:00 2001 From: Talia-12 Date: Sun, 4 Jun 2023 16:16:11 +1000 Subject: [PATCH] add bounds checks to arccos and arcsin --- .../common/casting/arithmetic/DoubleArithmetic.kt | 7 ++++--- .../common/casting/arithmetic/operator/OperatorUtils.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/DoubleArithmetic.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/DoubleArithmetic.kt index 3e053e0a..57917c22 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/DoubleArithmetic.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/DoubleArithmetic.kt @@ -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.mishaps.MishapDivideByZero 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 java.util.function.DoubleBinaryOperator import java.util.function.DoubleUnaryOperator @@ -51,7 +52,7 @@ object DoubleArithmetic : Arithmetic { override fun getOperator(pattern: HexPattern): Operator { 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 } MUL -> make2 { a, b -> 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) } COS -> make1 { a -> cos(a) } TAN -> make1 { a -> if (cos(a) == 0.0) throw MishapDivideByZero.tan(a) else tan(a) } - ARCSIN -> make1 { a -> asin(a) } - ARCCOS -> make1 { a -> acos(a) } + ARCSIN -> make1 { a -> asin(a.asDoubleBetween(-1.0, 1.0, 0)) } + ARCCOS -> make1 { a -> acos(a.asDoubleBetween(-1.0, 1.0, 0)) } ARCTAN -> make1 { a -> atan(a) } ARCTAN2 -> make2 { a, b -> atan2(a, b) } LOG -> OperatorLog diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/operator/OperatorUtils.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/operator/OperatorUtils.kt index ffbcc042..24247286 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/operator/OperatorUtils.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/arithmetic/operator/OperatorUtils.kt @@ -57,4 +57,11 @@ fun Iterator>.nextPositiveIntUnderInclusive(max: Int, argc: I } } throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max) -} \ No newline at end of file +} + +/** + * 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) \ No newline at end of file