Java Binary Search binarySearch(final double[] array, final double val)

Here you can find the source of binarySearch(final double[] array, final double val)

Description

binary Search

License

Open Source License

Declaration

static int binarySearch(final double[] array, final double val) 

Method Source Code

//package com.java2s;
/*/*from w w w  . j a v a2 s.c o m*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2005-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

public class Main {

    static int binarySearch(final double[] array, final double val) {
        int low = 0;
        int high = array.length - 1;
        final boolean keyIsNaN = Double.isNaN(val);
        while (low <= high) {
            final int mid = (low + high) >> 1;
            final double midVal = array[mid];
            if (midVal < val) { // Neither val is NaN, midVal is smaller
                low = mid + 1;
                continue;
            }
            if (midVal > val) { // Neither val is NaN, midVal is larger
                high = mid - 1;
                continue;
            }
            /*
             * The following is an adaptation of evaluator's comments for bug
             * #4471414
             * (http://developer.java.sun.com/developer/bugParade/bugs/4471414.html).
             * Extract from evaluator's comment:
             * 
             * [This] code is not guaranteed to give the desired results because
             * of laxity in IEEE 754 regarding NaN values. There are actually
             * two types of NaNs, signaling NaNs and quiet NaNs. Java doesn't
             * support the features necessary to reliably distinguish the two.
             * However, the relevant point is that copying a signaling NaN may
             * (or may not, at the implementors discretion) yield a quiet NaN --
             * a NaN with a different bit pattern (IEEE 754 6.2). Therefore, on
             * IEEE 754 compliant platforms it may be impossible to find a
             * signaling NaN stored in an array since a signaling NaN passed as
             * an argument to binarySearch may get replaced by a quiet NaN.
             */
            final long midRawBits = Double.doubleToRawLongBits(midVal);
            final long keyRawBits = Double.doubleToRawLongBits(val);
            if (midRawBits == keyRawBits) {
                return mid; // key found
            }
            final boolean midIsNaN = Double.isNaN(midVal);
            final boolean adjustLow;
            if (keyIsNaN) {
                // If (mid,key)==(!NaN, NaN): mid is lower.
                // If two NaN arguments, compare NaN bits.
                adjustLow = (!midIsNaN || midRawBits < keyRawBits);
            } else {
                // If (mid,key)==(NaN, !NaN): mid is greater.
                // Otherwise, case for (-0.0, 0.0) and (0.0, -0.0).
                adjustLow = (!midIsNaN && midRawBits < keyRawBits);
            }
            if (adjustLow)
                low = mid + 1;
            else
                high = mid - 1;
        }
        return -(low + 1); // key not found.
    }
}

Related

  1. binarySearch(char[] arr, int key)
  2. binarySearch(double experience, int min, int max)
  3. binarySearch(double values[], double search)
  4. binarySearch(double[] a, double key)
  5. binarySearch(double[] arr, double value, int p, int q)
  6. binarySearch(final Enum[] full, final int[] partial, final char key, final int index)
  7. binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key)
  8. binarySearch(int arr[], int low, int high, int key)
  9. binarySearch(int codePoint)