Class IntervalSize

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.index.quadtree;
14  
15 /**
16  * Provides a test for whether an interval is
17  * so small it should be considered as zero for the purposes of
18  * inserting it into a binary tree.
19  * The reason this check is necessary is that round-off error can
20  * cause the algorithm used to subdivide an interval to fail, by
21  * computing a midpoint value which does not lie strictly between the
22  * endpoints.
23  *
24  * @version 1.7
25  */
26 public class IntervalSize {
27  
28   /**
29    * This value is chosen to be a few powers of 2 less than the
30    * number of bits available in the double representation (i.e. 53).
31    * This should allow enough extra precision for simple computations to be correct,
32    * at least for comparison purposes.
33    */
34   public static final int MIN_BINARY_EXPONENT = -50;
35  
36   /**
37    * Computes whether the interval [min, max] is effectively zero width.
38    * I.e. the width of the interval is so much less than the
39    * location of the interval that the midpoint of the interval cannot be
40    * represented precisely.
41    */
42   public static boolean isZeroWidth(double min, double max)
43   {
44     double width = max - min;
45     if (width == 0.0return true;
46  
47     double maxAbs = Math.max(Math.abs(min), Math.abs(max));
48     double scaledInterval = width / maxAbs;
49     int level = DoubleBits.exponent(scaledInterval);
50     return level <= MIN_BINARY_EXPONENT;
51   }
52 }
53