parent
780f3b3814
commit
0245af79f8
@ -293,7 +293,7 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
return -this
|
return -this
|
||||||
|
|
||||||
if (isZero && other.isZero)
|
if (isZero && other.isZero)
|
||||||
return this
|
return NaN
|
||||||
|
|
||||||
if (!isZero && other.isZero)
|
if (!isZero && other.isZero)
|
||||||
throw ArithmeticException("Divide by zero")
|
throw ArithmeticException("Divide by zero")
|
||||||
@ -344,7 +344,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
operator fun div(other: Float): ImpreciseFraction {
|
operator fun div(other: Float): ImpreciseFraction {
|
||||||
if (other == 0f) {
|
if (other == 0f) {
|
||||||
|
if (isZero)
|
||||||
return NaN
|
return NaN
|
||||||
|
else
|
||||||
|
throw ArithmeticException("Divide by zero")
|
||||||
} else if (other == 1f) {
|
} else if (other == 1f) {
|
||||||
return this
|
return this
|
||||||
} else if (other == -1f) {
|
} else if (other == -1f) {
|
||||||
@ -384,7 +387,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
operator fun div(other: Double): ImpreciseFraction {
|
operator fun div(other: Double): ImpreciseFraction {
|
||||||
if (other == 0.0) {
|
if (other == 0.0) {
|
||||||
|
if (isZero)
|
||||||
return NaN
|
return NaN
|
||||||
|
else
|
||||||
|
throw ArithmeticException("Divide by zero")
|
||||||
} else if (other == 1.0) {
|
} else if (other == 1.0) {
|
||||||
return this
|
return this
|
||||||
} else if (other == -1.0) {
|
} else if (other == -1.0) {
|
||||||
@ -424,7 +430,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
operator fun div(other: Int): ImpreciseFraction {
|
operator fun div(other: Int): ImpreciseFraction {
|
||||||
if (other == 0) {
|
if (other == 0) {
|
||||||
|
if (isZero)
|
||||||
return NaN
|
return NaN
|
||||||
|
else
|
||||||
|
throw ArithmeticException("Divide by zero")
|
||||||
} else if (other == 1) {
|
} else if (other == 1) {
|
||||||
return this
|
return this
|
||||||
} else if (other == -1) {
|
} else if (other == -1) {
|
||||||
@ -464,7 +473,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
operator fun div(other: Long): ImpreciseFraction {
|
operator fun div(other: Long): ImpreciseFraction {
|
||||||
if (other == 0L) {
|
if (other == 0L) {
|
||||||
|
if (isZero)
|
||||||
return NaN
|
return NaN
|
||||||
|
else
|
||||||
|
throw ArithmeticException("Divide by zero")
|
||||||
} else if (other == 1L) {
|
} else if (other == 1L) {
|
||||||
return this
|
return this
|
||||||
} else if (other == -1L) {
|
} 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
|
* Returns pooled value if present, otherwise constructs new object
|
||||||
*/
|
*/
|
||||||
fun valueOf(value: Int): ImpreciseFraction {
|
fun valueOf(value: Int): ImpreciseFraction {
|
||||||
if (value in -1024 .. 1024) {
|
if (value in -1024 .. 1023) {
|
||||||
return cache[value + 1024]
|
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
|
* Returns pooled value if present, otherwise constructs new object
|
||||||
*/
|
*/
|
||||||
fun valueOf(value: Long): ImpreciseFraction {
|
fun valueOf(value: Long): ImpreciseFraction {
|
||||||
if (value in -1024L .. 1024L) {
|
if (value in -1024L .. 1023L) {
|
||||||
return cache[value.toInt() + 1024]
|
return cache[value.toInt() + 1024]
|
||||||
}
|
}
|
||||||
|
|
||||||
return ImpreciseFraction(value)
|
return ImpreciseFraction(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmField val NaN = ImpreciseFraction(0, Double.NaN)
|
@JvmField
|
||||||
|
val NaN = ImpreciseFraction(0, Double.NaN)
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun fromByteArray(input: ByteArray): ImpreciseFraction {
|
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())
|
fun CompoundTag.putImpreciseFraction(key: String, value: ImpreciseFraction) = put(key, value.serializeNBT())
|
||||||
|
|
||||||
operator fun CompoundTag.set(key: String, value: ImpreciseFraction) = putImpreciseFraction(key, value)
|
operator fun CompoundTag.set(key: String, value: ImpreciseFraction) = putImpreciseFraction(key, value)
|
||||||
|
|
||||||
fun Float.toImpreciseFraction() = ImpreciseFraction(this)
|
fun Float.toImpreciseFraction() = ImpreciseFraction(this)
|
||||||
fun Double.toImpreciseFraction() = ImpreciseFraction(this)
|
fun Double.toImpreciseFraction() = ImpreciseFraction(this)
|
||||||
fun Int.toImpreciseFraction() = ImpreciseFraction(this)
|
fun Int.toImpreciseFraction() = ImpreciseFraction(this)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.mc.otm.tests
|
package ru.dbotthepony.mc.otm.tests
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
||||||
@ -48,4 +49,16 @@ object ImpreciseFractionTests {
|
|||||||
check(f == loaded) { "$f != $loaded" }
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user