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

commonMain.it.unibo.tuprolog.solve.stdlib.function.FloatFractionalPart.kt Maven / Gradle / Ivy

package it.unibo.tuprolog.solve.stdlib.function

import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.Numeric
import it.unibo.tuprolog.core.Real
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.function.UnaryMathFunction
import org.gciatto.kt.math.BigDecimal
import org.gciatto.kt.math.RoundingMode

/**
 * Implementation of `float_fractional_part/1` arithmetic functor
 *
 * @author Enrico
 */
object FloatFractionalPart : UnaryMathFunction("float_fractional_part") {

    override fun mathFunction(integer: Integer, context: ExecutionContext): Numeric =
        commonBehaviour(integer.decimalValue)

    override fun mathFunction(real: Real, context: ExecutionContext): Numeric =
        commonBehaviour(real.decimalValue)

    /** Implements common behaviour for Integer and Real */
    private fun commonBehaviour(decimal: BigDecimal): Numeric = Numeric.of(
        when {
            decimal >= BigDecimal.ZERO -> decimal - decimal.setScale(0, RoundingMode.FLOOR)
            else -> -commonBehaviour(-decimal).decimalValue
        }
    )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy