Java Longitude Check longitudeCloseTo(Double first, Double second, double rtol, double atol)

Here you can find the source of longitudeCloseTo(Double first, Double second, double rtol, double atol)

Description

Determines if two longitudes are close to the same value modulo 360.0.

License

Open Source License

Parameter

Parameter Description
first value to compare
second value to compare
rtol relative tolerance of the difference
atol absolute tolerance of the difference

Return

true is first and second are both NaN, both Infinite (regardless of whether positive or negative), or have values whose difference is "negligible".

Declaration

public static boolean longitudeCloseTo(Double first, Double second, double rtol, double atol) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from ww  w .ja v  a2  s  .  c om
     * Determines if two longitudes are close to the same value
     * modulo 360.0.  The absolute of the average value, absAver, 
     * and the absolute value in the difference in values, absDiff,
     * of first and second are determined.
     *  
     * The difference between is considered negligible if: 
     *     absDiff < absAver * rtol + atol 
     * 
     * This comparison is made to the values as given as well as for
     * each value with 360.0 added to it.  
     * (So not a complete modulo 360 check.)
     * 
     * @param first 
     *       value to compare
     * @param second 
     *       value to compare
     * @param rtol
     *       relative tolerance of the difference
     * @param atol
     *       absolute tolerance of the difference
     * @return 
     *       true is first and second are both NaN, both Infinite
     *       (regardless of whether positive or negative), or 
     *       have values whose difference is "negligible".
     */
    public static boolean longitudeCloseTo(Double first, Double second, double rtol, double atol) {
        // Longitudes have modulo 360.0, so 359.999999 is close to 0.0
        if (closeTo(first, second, rtol, atol))
            return true;
        if (closeTo(first + 360.0, second, rtol, atol))
            return true;
        if (closeTo(first, second + 360.0, rtol, atol))
            return true;
        return false;
    }

    /**
     * Determines if two Doubles are close to the same value.
     * The absolute of the average value, absAver, and the 
     * absolute value in the difference in values, absDiff,
     * of first and second are determined.
     *  
     * The difference between is considered negligible if: 
     *     absDiff < absAver * rtol + atol 
     * 
     * @param first 
     *       value to compare
     * @param second 
     *       value to compare
     * @param rtol
     *       relative tolerance of the difference
     * @param atol
     *       absolute tolerance of the difference
     * @return 
     *       true is first and second are both NaN, both Infinite
     *       (regardless of whether positive or negative), or 
     *       have values whose difference is "negligible".
     */
    public static boolean closeTo(Double first, Double second, double rtol, double atol) {

        if (first == null || second == null) {
            return false;
        }

        // NaN (only) matches NaN
        if (first.isNaN()) {
            return second.isNaN();
        }
        if (second.isNaN()) {
            return false;
        }

        // Positive or negative infinity (only) matches 
        // positive or negative infinity
        if (first.isInfinite()) {
            return second.isInfinite();
        }
        if (second.isInfinite()) {
            return false;
        }

        // Check if they are the same value
        if (first.equals(second))
            return true;

        // Check if values are close
        double absDiff = Math.abs(first - second);
        double absAver = Math.abs((first + second) * 0.5);
        return (absDiff < absAver * rtol + atol);
    }
}