/**
 * Rounds a given floating-point number to the nearest value that is within epsilon.
 *
 * @param x The original floating-point number.
 * @param epsilon The tolerance value.
 * @return The rounded floating-point number.
 */
fun roundToEpsilon(x: Double, epsilon: Double): Double {

    var roundedValue = 0.0
    var count = 0

    while (roundedValue != x) {
        roundedValue = x.round(count)
        count++
    }

    return if (Math.abs(roundedValue - x) > epsilon) {
        roundedValue
    } else {
        x.round(count - 1)
    }
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()
/**
 * Rounds a given floating-point number to the nearest value that is within epsilon.
 *
 * @param x The original floating-point number.
 * @param epsilon The tolerance value.
 * @return The rounded floating-point number.
 */
fun roundToEpsilon(x: Double, epsilon: Double): Double {
/**
 * Rounds a given floating-point number to the nearest value that is within epsilon.
 *
 * @param x The original floating-point number.
 * @param epsilon The tolerance value.
 * @return The rounded floating-point number.
 */
fun roundToEpsilon(x: Double, epsilon: Double): Double {

    var roundedValue = 0.0
    var count = 0

    while (roundedValue != x) {
        roundedValue = x.round(count)
        count++
    }

    return if (Math.abs(roundedValue - x) > epsilon) {
        roundedValue
    } else {
        x.round(count - 1)
    }
}

fun Double.round(decimals: Int): Double = "%.${decimals}f".format(this).toDouble()