public static boolean almostEquals(double v1, double v2) {
        if (v1 == v2) {
            return true;
        }
        
        if (Double.isNaN(v1) || Double.isNaN(v2) || Double.isInfinite(v1) || Double.isInfinite(v2)) {
            return false;
        }
        
        long bits1 = Double.doubleToLongBits(v1);
        long bits2 = Double.doubleToLongBits(v2);
        if (bits1 < 0) {
            bits1 = Long.MIN_VALUE - bits1;
        }
        
        if (bits2 < 0) {
            bits2 = Long.MIN_VALUE - bits2;
        }
        
        long diff = bits1 - bits2;
        if (diff < 0) {
            diff = -diff;
        }
        
        return diff < maxUlps;
    }