Java Number Min Value minDelta(double a, double b)

Here you can find the source of minDelta(double a, double b)

Description

min Delta

License

Apache License

Return

min(relDelta(a,b), absDelta(a,b)).

Declaration

public static double minDelta(double a, double b) 

Method Source Code

//package com.java2s;
/*//  w ww. j  a  v  a2  s.  c  o  m
 * Copyright 2014 Jeff Hain
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Double.MIN_NORMAL since Java 6.
     */
    private static final double DOUBLE_MIN_NORMAL = Double.longBitsToDouble(0x0010000000000000L);

    /**
     * @return min(relDelta(a,b), absDelta(a,b)).
     */
    public static double minDelta(double a, double b) {
        return Math.min(relDelta(a, b), absDelta(a, b));
    }

    /**
     * If arguments max magnitude is < Double.MIN_NORMAL, relative delta might
     * be bad due to double precision loss, so we multiply the delta by
     * maxMagnitude/Double.MIN_NORMAL (which is in [0,1]) before returning it.
     * For example this causes relDelta(0,1e-320) to be not 1 but about 4.5e-13.
     * 
     * Similarly, if relative delta is infinite due to a value being +-Infinity
     * and the other being close to +-Double.MAX_VALUE (with the same sign),
     * we return relative delta between the finite value and +-Double.MAX_VALUE
     * of ame sign.
     * 
     * @return relDelta_raw(a,b), unless betterified due to above logic.
     */
    public static double relDelta(double a, double b) {

        double delta = relDelta_raw(a, b);

        if (Double.isInfinite(delta)) {
            if ((a == Double.NEGATIVE_INFINITY) && (b < -Double.MAX_VALUE / (1L << 52))) {
                // Still +Infinity if b is -Infinity.
                return relDelta_raw(-Double.MAX_VALUE, b);
            }
            if ((a == Double.POSITIVE_INFINITY) && (b > Double.MAX_VALUE / (1L << 52))) {
                // Still +Infinity if b is +Infinity.
                return relDelta_raw(Double.MAX_VALUE, b);
            }
            if ((a < -Double.MAX_VALUE / (1L << 52)) && (b == Double.NEGATIVE_INFINITY)) {
                // Still +Infinity if a is -Infinity.
                return relDelta_raw(a, -Double.MAX_VALUE);
            }
            if ((a > Double.MAX_VALUE / (1L << 52)) && (b == Double.POSITIVE_INFINITY)) {
                // Still +Infinity if a is +Infinity.
                return relDelta_raw(a, Double.MAX_VALUE);
            }
        }

        final double maxMag = Math.max(Math.abs(a), Math.abs(b));
        if (maxMag < DOUBLE_MIN_NORMAL) {
            delta *= (maxMag / DOUBLE_MIN_NORMAL);
        }

        return delta;
    }

    /**
     * @return |a-b|, or 0 if values are both NaN,
     *         both +Infinity or both -Infinity,
     *         or +Infinity if one value is NaN
     *         and the other not.
     */
    public static double absDelta(double a, double b) {
        if (a == b) {
            return 0.0;
        }
        if (Double.isNaN(a)) {
            return Double.isNaN(b) ? 0.0 : Double.POSITIVE_INFINITY;
        } else if (Double.isNaN(b)) {
            return Double.POSITIVE_INFINITY;
        }
        return Math.abs(a - b);
    }

    /**
     * @return |a-b|/max(|a|,|b|), or 0 if values are both NaN,
     *         both +Infinity or both -Infinity, or +Infinity
     *         if (((a < 0) and (b > 0)) or ((a > 0) and (b < 0))),
     *         or if a value is NaN and the other not.
     */
    public static double relDelta_raw(double a, double b) {
        if (a == b) {
            return 0.0;
        }
        if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) {
            return Double.POSITIVE_INFINITY;
        }
        if (Double.isNaN(a)) {
            return (Double.isNaN(b)) ? 0.0 : Double.POSITIVE_INFINITY;
        } else if (Double.isNaN(b)) {
            return Double.POSITIVE_INFINITY;
        }
        if (Double.isInfinite(a)) {
            return (Double.isInfinite(b)) ? 0.0 : Double.POSITIVE_INFINITY;
        } else if (Double.isInfinite(b)) {
            return Double.POSITIVE_INFINITY;
        }
        return Math.abs(a - b) / Math.max(Math.abs(a), Math.abs(b));
    }
}

Related

  1. minComparable(T first, T second)
  2. minCoverageEstimate(short mask)
  3. minCurveSegmentLength(String segmentTypeName)
  4. minDark(final int colorValue)
  5. minDeg2Bigger(long number)
  6. minDivisibleNumber(float yourDividend, float divisor)
  7. minF(float a, float b)
  8. minFileBuf(long fileSize, int bufSize)
  9. minifiedJS(String url)