View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.geometry.core.precision;
18  
19  import java.util.Comparator;
20  
21  /** Class encapsulating the concept of comparison operations for doubles.
22   */
23  public abstract class DoublePrecisionContext implements Comparator<Double> {
24      /** Return true if the given values are considered equal to each other.
25       * @param a first value
26       * @param b second value
27       * @return true if the given values are considered equal
28       */
29      public boolean eq(final double a, final double b) {
30          return compare(a, b) == 0;
31      }
32  
33      /** Return true if the given value is considered equal to zero. This is
34       * equivalent {@code context.eq(n, 0.0)} but with a more explicit
35       * method name.
36       * @param n the number to compare to zero
37       * @return true if the argument is considered equal to zero.
38       */
39      public boolean eqZero(final double n) {
40          return eq(n, 0.0);
41      }
42  
43      /**
44       * Return true if the first argument is strictly less than the second.
45       * @param a first value
46       * @param b second value
47       * @return true if {@code a < b}
48       */
49      public boolean lt(final double a, final double b) {
50          return compare(a, b) < 0;
51      }
52  
53      /**
54       * Return true if the first argument is less than or equal to the second.
55       * @param a first value
56       * @param b second value
57       * @return true if {@code a <= b}
58       */
59      public boolean lte(final double a, final double b) {
60          return compare(a, b) <= 0;
61      }
62  
63      /**
64       * Return true if the first argument is strictly greater than the second.
65       * @param a first value
66       * @param b second value
67       * @return true if {@code a > b}
68       */
69      public boolean gt(final double a, final double b) {
70          return compare(a, b) > 0;
71      }
72  
73      /**
74       * Return true if the first argument is greater than or equal to the second.
75       * @param a first value
76       * @param b second value
77       * @return true if {@code a >= b}
78       */
79      public boolean gte(final double a, final double b) {
80          return compare(a, b) >= 0;
81      }
82  
83      /** Return the sign of the argument: 0 if the value is considered equal to
84       * zero, -1 if less than 0, and +1 if greater than 0.
85       * @param a number to determine the sign of
86       * @return 0 if the number is considered equal to 0, -1 if less than
87       *      0, and +1 if greater than 0
88       */
89      public int sign(final double a) {
90          final int cmp = compare(a, 0.0);
91          if (cmp < 0) {
92              return -1;
93          } else if (cmp > 0) {
94              return 1;
95          }
96          return 0;
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public int compare(final Double a, final Double b) {
102         return compare(a.doubleValue(), b.doubleValue());
103     }
104 
105     /** Compare two double values. The returned value is
106      * <ul>
107      *  <li>
108      *   {@code 0} if the arguments are considered equal,
109      *  </li>
110      *  <li>
111      *   {@code -1} if {@code a < b},
112      *  </li>
113      *  <li>
114      *   {@code +1} if {@code a > b} or if either value is NaN.
115      *  </li>
116      * </ul>
117      *
118      * @param a first value
119      * @param b second value
120      * @return {@code 0} if the values are considered equal, {@code -1} if the
121      *      first is smaller than the second, {@code 1} is the first is larger
122      *      than the second or either value is NaN.
123      */
124     public abstract int compare(double a, double b);
125 
126     /** Get the largest positive double value that is still considered equal
127      * to zero by this instance.
128      * @return the largest positive double value still considered equal to zero
129      */
130     public abstract double getMaxZero();
131 }