diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/ImpreciseFraction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ImpreciseFraction.kt index 295f662e1..8f6831cea 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/ImpreciseFraction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ImpreciseFraction.kt @@ -293,7 +293,7 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do return -this if (isZero && other.isZero) - return this + return NaN if (!isZero && other.isZero) throw ArithmeticException("Divide by zero") @@ -344,7 +344,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do operator fun div(other: Float): ImpreciseFraction { if (other == 0f) { - return NaN + if (isZero) + return NaN + else + throw ArithmeticException("Divide by zero") } else if (other == 1f) { return this } else if (other == -1f) { @@ -384,7 +387,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do operator fun div(other: Double): ImpreciseFraction { if (other == 0.0) { - return NaN + if (isZero) + return NaN + else + throw ArithmeticException("Divide by zero") } else if (other == 1.0) { return this } else if (other == -1.0) { @@ -424,7 +430,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do operator fun div(other: Int): ImpreciseFraction { if (other == 0) { - return NaN + if (isZero) + return NaN + else + throw ArithmeticException("Divide by zero") } else if (other == 1) { return this } else if (other == -1) { @@ -464,7 +473,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do operator fun div(other: Long): ImpreciseFraction { if (other == 0L) { - return NaN + if (isZero) + return NaN + else + throw ArithmeticException("Divide by zero") } else if (other == 1L) { return this } else if (other == -1L) { @@ -763,7 +775,7 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do * Returns pooled value if present, otherwise constructs new object */ fun valueOf(value: Int): ImpreciseFraction { - if (value in -1024 .. 1024) { + if (value in -1024 .. 1023) { return cache[value + 1024] } @@ -774,14 +786,15 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do * Returns pooled value if present, otherwise constructs new object */ fun valueOf(value: Long): ImpreciseFraction { - if (value in -1024L .. 1024L) { + if (value in -1024L .. 1023L) { return cache[value.toInt() + 1024] } return ImpreciseFraction(value) } - @JvmField val NaN = ImpreciseFraction(0, Double.NaN) + @JvmField + val NaN = ImpreciseFraction(0, Double.NaN) @JvmStatic fun fromByteArray(input: ByteArray): ImpreciseFraction { @@ -839,10 +852,11 @@ fun CompoundTag.getImpreciseFraction(key: String) = ImpreciseFraction.deserializ fun CompoundTag.putImpreciseFraction(key: String, value: ImpreciseFraction) = put(key, value.serializeNBT()) operator fun CompoundTag.set(key: String, value: ImpreciseFraction) = putImpreciseFraction(key, value) + fun Float.toImpreciseFraction() = ImpreciseFraction(this) fun Double.toImpreciseFraction() = ImpreciseFraction(this) fun Int.toImpreciseFraction() = ImpreciseFraction(this) fun Byte.toImpreciseFraction() = ImpreciseFraction(this) fun Short.toImpreciseFraction() = ImpreciseFraction(this) fun Long.toImpreciseFraction() = ImpreciseFraction(this) -fun ImpreciseFraction.toImpreciseFraction() = this \ No newline at end of file +fun ImpreciseFraction.toImpreciseFraction() = this diff --git a/src/test/kotlin/ru/dbotthepony/mc/otm/tests/ImpreciseFractionTests.kt b/src/test/kotlin/ru/dbotthepony/mc/otm/tests/ImpreciseFractionTests.kt index 9eb21c3fd..c75dfa9bc 100644 --- a/src/test/kotlin/ru/dbotthepony/mc/otm/tests/ImpreciseFractionTests.kt +++ b/src/test/kotlin/ru/dbotthepony/mc/otm/tests/ImpreciseFractionTests.kt @@ -1,5 +1,6 @@ package ru.dbotthepony.mc.otm.tests +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import ru.dbotthepony.mc.otm.core.ImpreciseFraction @@ -48,4 +49,16 @@ object ImpreciseFractionTests { check(f == loaded) { "$f != $loaded" } } } + + @Test + @DisplayName("ImpreciseFraction.valueOf") + fun valueOf() { + for (i in -2100 .. 2100) { + assertEquals(ImpreciseFraction(i), ImpreciseFraction.valueOf(i)) + } + + for (i in -2100 .. 2100) { + assertEquals(ImpreciseFraction(i.toLong()), ImpreciseFraction.valueOf(i.toLong())) + } + } }