toIntSafe for BigInteger

This commit is contained in:
DBotThePony 2022-07-03 23:10:56 +07:00
parent 843da5c007
commit 627e35b065
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 22 additions and 15 deletions

View File

@ -118,16 +118,6 @@ fun weakGreaterThan(a: Double, b: Double) = weakCompareDoubles(a, b) > 0
fun weakLessOrEqual(a: Double, b: Double) = weakCompareDoubles(a, b) <= 0 fun weakLessOrEqual(a: Double, b: Double) = weakCompareDoubles(a, b) <= 0
fun weakGreaterOrEqual(a: Double, b: Double) = weakCompareDoubles(a, b) >= 0 fun weakGreaterOrEqual(a: Double, b: Double) = weakCompareDoubles(a, b) >= 0
private val BI_INT_MAX = BigInteger.valueOf(Int.MAX_VALUE.toLong())
private val BI_INT_MIN = BigInteger.valueOf(Int.MIN_VALUE.toLong())
private val BI_LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE)
private val BI_LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE)
private val BI_FLOAT_MAX = BigDecimal(Float.MAX_VALUE.toString()).toBigInteger()
private val BI_FLOAT_MIN = BigDecimal(Float.MIN_VALUE.toString()).toBigInteger()
private val BI_DOUBLE_MAX = BigDecimal(Double.MAX_VALUE.toString()).toBigInteger()
private val BI_DOUBLE_MIN = BigDecimal(Double.MIN_VALUE.toString()).toBigInteger()
private val PERCENTAGE_CONTEXT = MathContext(6) private val PERCENTAGE_CONTEXT = MathContext(6)
private const val MEANINGFUL_BITS = 16 private const val MEANINGFUL_BITS = 16

View File

@ -11,6 +11,26 @@ inline val BigDecimal.isZero get() = this == BigDecimal.ZERO
inline val BigDecimal.isPositive get() = this > BigDecimal.ZERO inline val BigDecimal.isPositive get() = this > BigDecimal.ZERO
inline val BigDecimal.isNegative get() = this < BigDecimal.ZERO inline val BigDecimal.isNegative get() = this < BigDecimal.ZERO
val BI_INT_MAX: BigInteger = BigInteger.valueOf(Int.MAX_VALUE.toLong())
val BI_INT_MIN: BigInteger = BigInteger.valueOf(Int.MIN_VALUE.toLong())
val BI_LONG_MAX: BigInteger = BigInteger.valueOf(Long.MAX_VALUE)
val BI_LONG_MIN: BigInteger = BigInteger.valueOf(Long.MIN_VALUE)
val BI_FLOAT_MAX: BigInteger = BigDecimal(Float.MAX_VALUE.toString()).toBigInteger()
val BI_FLOAT_MIN: BigInteger = BigDecimal(Float.MIN_VALUE.toString()).toBigInteger()
val BI_DOUBLE_MAX: BigInteger = BigDecimal(Double.MAX_VALUE.toString()).toBigInteger()
val BI_DOUBLE_MIN: BigInteger = BigDecimal(Double.MIN_VALUE.toString()).toBigInteger()
fun BigInteger.toIntSafe(): Int {
if (this > BI_INT_MAX) {
return Int.MAX_VALUE
} else if (this < BI_INT_MIN) {
return Int.MIN_VALUE
}
return toInt()
}
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
fun equalDownDivision(a: Int, b: Int): Int { fun equalDownDivision(a: Int, b: Int): Int {
if (a % b == 0) { if (a % b == 0) {

View File

@ -6,10 +6,7 @@ import net.minecraft.world.item.ItemStack
import net.minecraftforge.registries.ForgeRegistries import net.minecraftforge.registries.ForgeRegistries
import net.minecraftforge.registries.ForgeRegistry import net.minecraftforge.registries.ForgeRegistry
import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.ApiStatus
import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.core.*
import ru.dbotthepony.mc.otm.core.isPositive
import ru.dbotthepony.mc.otm.core.readBigInteger
import ru.dbotthepony.mc.otm.core.writeBigInteger
import java.math.BigInteger import java.math.BigInteger
/** /**
@ -83,7 +80,7 @@ class ItemStackWrapper : IStorageStack {
/** /**
* [ItemStack] with valid amount and which can be modified after * [ItemStack] with valid amount and which can be modified after
*/ */
val stack: ItemStack get() = item.copy().also { it.count = count.toInt() } val stack: ItemStack get() = item.copy().also { it.count = count.toIntSafe() }
override fun toString(): String { override fun toString(): String {
return "ItemStackWrapper[$count $registryName]" return "ItemStackWrapper[$count $registryName]"