JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
DoubleUtil.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.math;
29
30/**
31 * Basic Double math utility functions.
32 */
33public final class DoubleUtil {
34 //
35 // Scalar Ops
36 //
37
38 @SuppressWarnings("unused")
39 private static void calculateMachineEpsilonDouble() {
40 final long t0;
41 double machEps = 1.0;
42 int i=0;
43 do {
44 machEps /= 2.0;
45 i++;
46 } while (1.0 + (machEps / 2.0) != 1.0);
47 machEpsilon = machEps;
48 }
49 private static volatile boolean machEpsilonAvail = false;
50 private static double machEpsilon = 0f;
51
52 /**
53 * Return computed machine Epsilon value.
54 * <p>
55 * The machine Epsilon value is computed once.
56 * </p>
57 * @see #EPSILON
58 */
59 public static double getMachineEpsilon() {
60 if( !machEpsilonAvail ) {
61 synchronized(DoubleUtil.class) {
62 if( !machEpsilonAvail ) {
63 machEpsilonAvail = true;
64 calculateMachineEpsilonDouble();
65 }
66 }
67 }
68 return machEpsilon;
69 }
70
71 public static final double E = 2.7182818284590452354;
72
73 /** The value PI, i.e. 180 degrees in radians. */
74 public static final double PI = 3.14159265358979323846;
75
76 /** The value 2PI, i.e. 360 degrees in radians. */
77 public static final double TWO_PI = 2.0 * PI;
78
79 /** The value PI/2, i.e. 90 degrees in radians. */
80 public static final double HALF_PI = PI / 2.0;
81
82 /** The value PI/4, i.e. 45 degrees in radians. */
83 public static final double QUARTER_PI = PI / 4.0;
84
85 /** The value PI^2. */
86 public final static double SQUARED_PI = PI * PI;
87
88 /** Converts arc-degree to radians */
89 public static double adegToRad(final double arc_degree) {
90 return arc_degree * PI / 180.0;
91 }
92
93 /** Converts radians to arc-degree */
94 public static double radToADeg(final double rad) {
95 return rad * 180.0 / PI;
96 }
97
98 /**
99 * Epsilon for floating point {@value}, as once computed via {@link #getMachineEpsilon()} on an AMD-64 CPU.
100 * <p>
101 * Definition of machine epsilon guarantees that:
102 * <pre>
103 * 1.0 + EPSILON != 1.0
104 * </pre>
105 * In other words: <i>machEps</i> is the maximum relative error of the chosen rounding procedure.
106 * </p>
107 * <p>
108 * A number can be considered zero if it is in the range (or in the set):
109 * <pre>
110 * <b>MaybeZeroSet</b> e ]-<i>machEps</i> .. <i>machEps</i>[ <i>(exclusive)</i>
111 * </pre>
112 * While comparing floating point values, <i>machEps</i> allows to clip the relative error:
113 * <pre>
114 * boolean isZero = afloat < EPSILON;
115 * boolean isNotZero = afloat >= EPSILON;
116 *
117 * boolean isEqual = abs(bfloat - afloat) < EPSILON;
118 * boolean isNotEqual = abs(bfloat - afloat) >= EPSILON;
119 * </pre>
120 * </p>
121 * @see #isEqual(float, float, float)
122 * @see #isZero(float, float)
123 */
124 public static final double EPSILON = 2.220446049250313E-16;
125
126 /**
127 * Inversion Epsilon, used with equals method to determine if two inverted matrices are close enough to be considered equal.
128 * <p>
129 * Using {@value}, which is ~100 times {@link DoubleUtil#EPSILON}.
130 * </p>
131 */
132 public static final double INV_DEVIANCE = 1.0E-8f; // EPSILON == 1.1920929E-7f; double ALLOWED_DEVIANCE: 1.0E-8f
133
134 /**
135 * Return true if both values are equal w/o regarding an epsilon.
136 * <p>
137 * Implementation considers following corner cases:
138 * <ul>
139 * <li>NaN == NaN</li>
140 * <li>+Inf == +Inf</li>
141 * <li>-Inf == -Inf</li>
142 * </ul>
143 * </p>
144 * @see #isEqual(float, float, float)
145 */
146 public static boolean isEqualRaw(final double a, final double b) {
147 // Values are equal (Inf, Nan .. )
148 return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
149 }
150
151 /**
152 * Returns true if both values are equal, i.e. their absolute delta < {@code epsilon} if 0 != {@code epsilon},
153 * otherwise == {@code 0}.
154 * <p>
155 * {@code epsilon} is allowed to be {@code 0}.
156 * </p>
157 * <p>
158 * Implementation considers following corner cases:
159 * <ul>
160 * <li>NaN == NaN</li>
161 * <li>+Inf == +Inf</li>
162 * <li>-Inf == -Inf</li>
163 * </ul>
164 * </p>
165 * @see #EPSILON
166 */
167 public static boolean isEqual(final double a, final double b, final double epsilon) {
168 if( 0 == epsilon && Math.abs(a - b) == 0 ||
169 0 != epsilon && Math.abs(a - b) < epsilon ) {
170 return true;
171 } else {
172 // Values are equal (Inf, Nan .. )
173 return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
174 }
175 }
176
177 /**
178 * Returns true if both values are equal, i.e. their absolute delta < {@link #EPSILON}.
179 * <p>
180 * Implementation considers following corner cases:
181 * <ul>
182 * <li>NaN == NaN</li>
183 * <li>+Inf == +Inf</li>
184 * <li>-Inf == -Inf</li>
185 * </ul>
186 * </p>
187 * @see #EPSILON
188 */
189 public static boolean isEqual(final double a, final double b) {
190 if ( Math.abs(a - b) < EPSILON ) {
191 return true;
192 } else {
193 // Values are equal (Inf, Nan .. )
194 return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
195 }
196 }
197
198 /**
199 * Returns true if both values are equal, i.e. their absolute delta < {@link #EPSILON}.
200 * <p>
201 * Implementation does not consider corner cases like {@link #isEqual(float, float, float)}.
202 * </p>
203 * @see #EPSILON
204 */
205 public static boolean isEqual2(final double a, final double b) {
206 return Math.abs(a - b) < EPSILON;
207 }
208
209 /**
210 * Returns true if both values are equal w/o regarding an epsilon.
211 * <p>
212 * Implementation considers following corner cases:
213 * <ul>
214 * <li>NaN == NaN</li>
215 * <li>+Inf == +Inf</li>
216 * <li>-Inf == -Inf</li>
217 * <li>NaN > 0</li>
218 * <li>+Inf > -Inf</li>
219 * </ul>
220 * </p>
221 * @see #compare(float, float, float)
222 */
223 public static int compare(final double a, final double b) {
224 if (a < b) {
225 return -1; // Neither is NaN, a is smaller
226 }
227 if (a > b) {
228 return 1; // Neither is NaN, a is larger
229 }
230 final long aBits = Double.doubleToLongBits(a);
231 final long bBits = Double.doubleToLongBits(b);
232 if( aBits == bBits ) {
233 return 0; // Values are equal (Inf, Nan .. )
234 } else if( aBits < bBits ) {
235 return -1; // (-0.0, 0.0) or (!NaN, NaN)
236 } else {
237 return 1; // ( 0.0, -0.0) or ( NaN, !NaN)
238 }
239 }
240
241 /**
242 * Returns {@code -1}, {@code 0} or {@code 1} if {@code a} is less, equal or greater than {@code b},
243 * taking {@code epsilon} into account for equality.
244 * <p>
245 * {@code epsilon} is allowed to be {@code 0}.
246 * </p>
247 * <p>
248 * Implementation considers following corner cases:
249 * <ul>
250 * <li>NaN == NaN</li>
251 * <li>+Inf == +Inf</li>
252 * <li>-Inf == -Inf</li>
253 * <li>NaN > 0</li>
254 * <li>+Inf > -Inf</li>
255 * </ul>
256 * </p>
257 * @see #EPSILON
258 */
259 public static int compare(final double a, final double b, final double epsilon) {
260 if( 0 == epsilon && Math.abs(a - b) == 0 ||
261 0 != epsilon && Math.abs(a - b) < epsilon ) {
262 return 0;
263 } else {
264 return compare(a, b);
265 }
266 }
267
268 /**
269 * Returns true if value is zero, i.e. it's absolute value < {@code epsilon} if 0 != {@code epsilon},
270 * otherwise {@code 0 == a}.
271 * <p>
272 * {@code epsilon} is allowed to be {@code 0}.
273 * </p>
274 * <pre>
275 * return 0 == epsilon && 0 == a || 0 != epsilon && Math.abs(a) < epsilon
276 * </pre>
277 * @param a value to test
278 * @param epsilon optional positive epsilon value, maybe {@code 0}
279 * @see #EPSILON
280 */
281 public static boolean isZero(final double a, final double epsilon) {
282 return 0 == epsilon && a == 0 ||
283 0 != epsilon && Math.abs(a) < epsilon;
284 }
285
286 /**
287 * Returns true if value is zero, i.e. it's absolute value < {@link #EPSILON}.
288 * @see #EPSILON
289 */
290 public static boolean isZero(final double a) {
291 return Math.abs(a) < EPSILON;
292 }
293
294}
Basic Double math utility functions.
Definition: DoubleUtil.java:33
static boolean isZero(final double a)
Returns true if value is zero, i.e.
static final double E
Definition: DoubleUtil.java:71
static boolean isZero(final double a, final double epsilon)
Returns true if value is zero, i.e.
static double adegToRad(final double arc_degree)
Converts arc-degree to radians.
Definition: DoubleUtil.java:89
static boolean isEqual(final double a, final double b)
Returns true if both values are equal, i.e.
static final double PI
The value PI, i.e.
Definition: DoubleUtil.java:74
static final double QUARTER_PI
The value PI/4, i.e.
Definition: DoubleUtil.java:83
static double getMachineEpsilon()
Return computed machine Epsilon value.
Definition: DoubleUtil.java:59
static int compare(final double a, final double b, final double epsilon)
Returns -1, 0 or 1 if a is less, equal or greater than b, taking epsilon into account for equality.
static int compare(final double a, final double b)
Returns true if both values are equal w/o regarding an epsilon.
static double radToADeg(final double rad)
Converts radians to arc-degree.
Definition: DoubleUtil.java:94
static final double INV_DEVIANCE
Inversion Epsilon, used with equals method to determine if two inverted matrices are close enough to ...
static final double EPSILON
Epsilon for floating point {@value}, as once computed via getMachineEpsilon() on an AMD-64 CPU.
static boolean isEqual(final double a, final double b, final double epsilon)
Returns true if both values are equal, i.e.
static boolean isEqualRaw(final double a, final double b)
Return true if both values are equal w/o regarding an epsilon.
static final double SQUARED_PI
The value PI^2.
Definition: DoubleUtil.java:86
static boolean isEqual2(final double a, final double b)
Returns true if both values are equal, i.e.
static final double TWO_PI
The value 2PI, i.e.
Definition: DoubleUtil.java:77
static final double HALF_PI
The value PI/2, i.e.
Definition: DoubleUtil.java:80