Fixes for imprecise fraction

#72
This commit is contained in:
DBotThePony 2022-09-08 08:00:03 +07:00
parent 780f3b3814
commit 0245af79f8
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 36 additions and 9 deletions

View File

@ -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,6 +852,7 @@ 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)

View File

@ -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()))
}
}
}