Do not perform not meaningful operations on ImpreciseFraction

This commit is contained in:
DBotThePony 2022-09-04 20:12:59 +07:00
parent 8c09f72726
commit e5da98cba1
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -129,6 +129,8 @@ private const val MEANINGFUL_BITS_DOUBLE = MEANINGFUL_BITS_LONG.toDouble()
private val MEANINGFUL_BITS_CONTEXT = MathContext(MEANINGFUL_BITS)
private val MEANINGFUL_BITS_BI = BigInteger.valueOf(MEANINGFUL_BITS_LONG)
private val BI_MINUS_ONE = -BigInteger.ONE
/**
* Imprecise Fraction class for dealing with huge numbers that need fractional part to some extent.
*
@ -312,30 +314,205 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
return ImpreciseFraction(div[0].toBigInteger(), div[1].toDouble() / bD)
}
operator fun plus(other: Float): ImpreciseFraction = plus(ImpreciseFraction(other))
operator fun minus(other: Float): ImpreciseFraction = minus(ImpreciseFraction(other))
operator fun times(other: Float): ImpreciseFraction = times(ImpreciseFraction(other))
operator fun div(other: Float): ImpreciseFraction = div(ImpreciseFraction(other))
operator fun plus(other: Float): ImpreciseFraction {
if (other == 0f) {
return this
}
operator fun plus(other: Double): ImpreciseFraction = plus(ImpreciseFraction(other))
operator fun minus(other: Double): ImpreciseFraction = minus(ImpreciseFraction(other))
operator fun times(other: Double): ImpreciseFraction = times(ImpreciseFraction(other))
operator fun div(other: Double): ImpreciseFraction = div(ImpreciseFraction(other))
return plus(ImpreciseFraction(other))
}
operator fun plus(other: Int): ImpreciseFraction = plus(ImpreciseFraction(other))
operator fun minus(other: Int): ImpreciseFraction = minus(ImpreciseFraction(other))
operator fun times(other: Int): ImpreciseFraction = times(ImpreciseFraction(other))
operator fun div(other: Int): ImpreciseFraction = div(ImpreciseFraction(other))
operator fun minus(other: Float): ImpreciseFraction {
if (other == 0f) {
return this
}
operator fun plus(other: Long): ImpreciseFraction = plus(ImpreciseFraction(other))
operator fun minus(other: Long): ImpreciseFraction = minus(ImpreciseFraction(other))
operator fun times(other: Long): ImpreciseFraction = times(ImpreciseFraction(other))
operator fun div(other: Long): ImpreciseFraction = div(ImpreciseFraction(other))
return minus(ImpreciseFraction(other))
}
operator fun plus(other: BigInteger): ImpreciseFraction = plus(ImpreciseFraction(other))
operator fun minus(other: BigInteger): ImpreciseFraction = minus(ImpreciseFraction(other))
operator fun times(other: BigInteger): ImpreciseFraction = times(ImpreciseFraction(other))
operator fun div(other: BigInteger): ImpreciseFraction = div(ImpreciseFraction(other))
operator fun times(other: Float): ImpreciseFraction {
if (other == 1f) {
return this
} else if (other == 0f) {
return ZERO
} else if (other == -1f) {
return -this
}
return times(ImpreciseFraction(other))
}
operator fun div(other: Float): ImpreciseFraction {
if (other == 0f) {
return NaN
} else if (other == 1f) {
return this
} else if (other == -1f) {
return -this
}
return div(ImpreciseFraction(other))
}
operator fun plus(other: Double): ImpreciseFraction {
if (other == 0.0) {
return this
}
return plus(ImpreciseFraction(other))
}
operator fun minus(other: Double): ImpreciseFraction {
if (other == 0.0) {
return this
}
return minus(ImpreciseFraction(other))
}
operator fun times(other: Double): ImpreciseFraction {
if (other == 1.0) {
return this
} else if (other == 0.0) {
return ZERO
} else if (other == -1.0) {
return -this
}
return times(ImpreciseFraction(other))
}
operator fun div(other: Double): ImpreciseFraction {
if (other == 0.0) {
return NaN
} else if (other == 1.0) {
return this
} else if (other == -1.0) {
return -this
}
return div(ImpreciseFraction(other))
}
operator fun plus(other: Int): ImpreciseFraction {
if (other == 0) {
return this
}
return plus(ImpreciseFraction(other))
}
operator fun minus(other: Int): ImpreciseFraction {
if (other == 0) {
return this
}
return minus(ImpreciseFraction(other))
}
operator fun times(other: Int): ImpreciseFraction {
if (other == 1) {
return this
} else if (other == 0) {
return ZERO
} else if (other == -1) {
return -this
}
return times(ImpreciseFraction(other))
}
operator fun div(other: Int): ImpreciseFraction {
if (other == 0) {
return NaN
} else if (other == 1) {
return this
} else if (other == -1) {
return -this
}
return div(ImpreciseFraction(other))
}
operator fun plus(other: Long): ImpreciseFraction {
if (other == 0L) {
return this
}
return plus(ImpreciseFraction(other))
}
operator fun minus(other: Long): ImpreciseFraction {
if (other == 0L) {
return this
}
return minus(ImpreciseFraction(other))
}
operator fun times(other: Long): ImpreciseFraction {
if (other == 1L) {
return this
} else if (other == 0L) {
return ZERO
} else if (other == -1L) {
return -this
}
return times(ImpreciseFraction(other))
}
operator fun div(other: Long): ImpreciseFraction {
if (other == 0L) {
return NaN
} else if (other == 1L) {
return this
} else if (other == -1L) {
return -this
}
return div(ImpreciseFraction(other))
}
operator fun plus(other: BigInteger): ImpreciseFraction {
if (other == BigInteger.ZERO) {
return this
}
return plus(ImpreciseFraction(other))
}
operator fun minus(other: BigInteger): ImpreciseFraction {
if (other == BigInteger.ZERO) {
return this
}
return minus(ImpreciseFraction(other))
}
operator fun times(other: BigInteger): ImpreciseFraction {
if (other == BigInteger.ONE) {
return this
} else if (other == BigInteger.ZERO) {
return ZERO
} else if (other == BI_MINUS_ONE) {
return -this
}
return times(ImpreciseFraction(other))
}
operator fun div(other: BigInteger): ImpreciseFraction {
if (other == BigInteger.ZERO) {
return NaN
} else if (other == BigInteger.ONE) {
return this
} else if (other == BI_MINUS_ONE) {
return -this
}
return div(ImpreciseFraction(other))
}
operator fun unaryMinus(): ImpreciseFraction {
return ImpreciseFraction(-whole, -decimal)