Computes an inverse logarithmic interpolation, returning an interpolation fraction. - Java java.lang

Java examples for java.lang:Math Calculation

Description

Computes an inverse logarithmic interpolation, returning an interpolation fraction.

Demo Code


//package com.java2s;

public class Main {
    private static final double EPSILON = 1e-30d;

    /**//from  w  w  w. j a  v a2  s.c  o m
     * Computes an inverse logarithmic interpolation, returning an
     * interpolation fraction. Uses a zero-symmetric logarithm.
     * Returns 0.5 if the min and max values are the same.
     * @param x the interpolated value
     * @param min the minimum value (corresponds to f==0)
     * @param min the maximum value (corresponds to f==1)
     * @param b the base of the logarithm
     * @return the inferred interpolation fraction
     */
    public static double invLogInterp(double x, double min, double max,
            double b) {
        min = symLog(min, b);
        double denom = symLog(max, b) - min;
        return (denom < EPSILON && denom > -EPSILON ? 0
                : (symLog(x, b) - min) / denom);
    }

    /**
     * Computes a zero-symmetric logarithm. Computes the logarithm of the
     * absolute value of the input, and determines the sign of the output
     * according to the sign of the input value.
     * @param x the number for which to compute the logarithm
     * @param b the base of the logarithm
     * @return the symmetric log value.
     */
    public static double symLog(double x, double b) {
        return x == 0 ? 0 : x > 0 ? log(x, b) : -log(-x, b);
    }

    public static double log(double x, double b) {
        return Math.log(x) / Math.log(b);
    }
}

Related Tutorials