All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.operator.Precision.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.operator

import kotlin.reflect.full.*
import fuookami.ospf.kotlin.utils.math.*

@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
class Precision(
    precision: T
) where T : RealNumber, T : PlusGroup, T : Abs {
    private val precision = precision.abs()

    infix fun T.eq(other: T): Boolean {
        return (this - other).abs() <= precision
    }

    infix fun T.ord(other: T): Order {
        return if (this eq other) {
            Order.Equal
        } else {
            val value = this.compareTo(other)
            if (value < 0) {
                Order.Less(value)
            } else {
                Order.Greater(value)
            }
        }
    }

    infix fun T.neq(other: T): Boolean {
        return (this - other).abs() > precision
    }

    infix fun T.ls(other: T): Boolean {
        return (other - this) >= precision
    }

    infix fun T.leq(other: T): Boolean {
        return (this - other) <= precision
    }

    infix fun T.gr(other: T): Boolean {
        return (this - other) > precision
    }

    infix fun T.geq(other: T): Boolean {
        return (other - this) <= precision
    }
}

@Suppress("UNCHECKED_CAST")
inline fun  withPrecision(precision: T = (T::class.companionObjectInstance!! as RealNumberConstants).decimalPrecision)
        where T : RealNumber, T : PlusGroup, T : Abs = Precision(precision)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy