Computes a logarithmic interpolation between two values. - Java java.lang

Java examples for java.lang:Math Calculation

Description

Computes a logarithmic interpolation between two values.

Demo Code


//package com.java2s;

public class Main {
    /**// w  ww .j  av a 2 s  .  co  m
     * Computes a logarithmic interpolation between two values. Uses a
     * zero-symmetric logarithm calculation (see <code>symLog</code>).
     * @param f the interpolation fraction (typically between 0 and 1)
     * @param min the minimum value (corresponds to f==0)
     * @param max the maximum value (corresponds to f==1)
     * @param b the base of the logarithm
     * @return the interpolated value
     */
    public static double logInterp(double f, double min, double max,
            double b) {
        min = symLog(min, b);
        max = symLog(max, b);
        f = min + f * (max - min);

        return f < 0 ? -Math.pow(b, -f) : Math.pow(b, f);
    }

    /**
     * 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