Java floor floory(double a, double precision)

Here you can find the source of floory(double a, double precision)

Description

Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal for at least a precision value.

License

Open Source License

Return

⌊a/precision⌋*precision = Math.floor(a/precision)*precision.

Declaration

public static double floory(double a, double precision) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww  w.  j  av  a 2  s. c o  m
     * The default tolerance for two numbers to be treated equal.
     * (experimental)
     * @see #setDefaultTolerance(double)
     * @invariants DefaultTolerance ≥ 0
     */
    private static double DefaultTolerance = .00000000001;

    /**
     * Returns the largest (closest to positive infinity)
     * <code>double</code> value that is not greater than the argument and
     * is equal for at least a precision value.
     * <code>MathUtilities.floory(a, .01)</code> will result in <code>a</code> rounded down
     * to a number formated as <code>.##</code>,
     * while <code>MathUtilities.floory(a, 100)</code> will result in <code>a</code> rounded down
     * to a number formated as <code><i>several #s preceding</i>######<b>00</b>.</code>.
     * @return &lfloor;a/precision&rfloor;*precision = <code>Math.floor(a/precision)*precision</code>.
     * @see #ceily(double, double)
     */
    public static double floory(double a, double precision) {
        return Math.floor(a / precision) * precision;
    }

    public static double floory(double a) {
        return floory(a, precisionFor(a));
    }

    /**
     * Get the precision for a given a specified tolerance relative to the magnitude of a.
     * <p>
     * Roughly gives the precision for tolerance percent of a, but adjusted to decimal digits.
     * </p>
     * @return tolerance * 10<sup>&lceil;&#13266;<sub>10</sub> a&rceil;</sup>.
     */
    public static double precisionFor(double a, double tolerance) {
        return tolerance * Math.pow(10, Math.ceil(Math.log(a) / Math.log(10)));
    }

    /**
     * Get the precision for a default tolerance relative to the magnitude of a.
     */
    public static double precisionFor(double a) {
        return precisionFor(a, DefaultTolerance);
    }
}

Related

  1. floorSqrt(long i)
  2. floorToLong(float pX)
  3. floorToNearestNumberDivisibleByFour(long number)
  4. floorToPowerofTwo(int i)
  5. floorTs(long timestamp, int aggregationWindow)