1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.util;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.math.BigDecimal;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.math.BigInteger;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.util.Arrays;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.Localizable;
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.NonMonotonousSequenceException;
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Some useful additions to the built-in functions in {@link Math}.
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1073472 $ $Date: 2011-02-22 20:49:07 +0100 (mar. 22 févr. 2011) $
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic final class MathUtils {
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Smallest positive number such that 1 - EPSILON is not numerically equal to 1. */
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static final double EPSILON = 0x1.0p-53;
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Safe minimum, such that 1 / SAFE_MIN does not overflow.
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>In IEEE 754 arithmetic, this is also the smallest normalized
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * number 2<sup>-1022</sup>.</p>
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static final double SAFE_MIN = 0x1.0p-1022;
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 2 &pi;.
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.1
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static final double TWO_PI = 2 * FastMath.PI;
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** -1.0 cast as a byte. */
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final byte  NB = (byte)-1;
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** -1.0 cast as a short. */
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final short NS = (short)-1;
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** 1.0 cast as a byte. */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final byte  PB = (byte)1;
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** 1.0 cast as a short. */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final short PS = (short)1;
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** 0.0 cast as a byte. */
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final byte  ZB = (byte)0;
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** 0.0 cast as a short. */
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final short ZS = (short)0;
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Gap between NaN and regular numbers. */
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final int NAN_GAP = 4 * 1024 * 1024;
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Offset to order signed double numbers lexicographically. */
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long SGN_MASK = 0x8000000000000000L;
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Offset to order signed double numbers lexicographically. */
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final int SGN_MASK_FLOAT = 0x80000000;
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** All long-representable factorials */
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long[] FACTORIALS = new long[] {
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                       1l,                  1l,                   2l,
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                       6l,                 24l,                 120l,
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                     720l,               5040l,               40320l,
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  362880l,            3628800l,            39916800l,
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond               479001600l,         6227020800l,         87178291200l,
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond           1307674368000l,     20922789888000l,     355687428096000l,
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        6402373705728000l, 121645100408832000l, 2432902008176640000l };
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Private Constructor
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private MathUtils() {
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super();
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Add two integers, checking for overflow.
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x an addend
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y an addend
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum <code>x+y</code>
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         int
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int addAndCheck(int x, int y) {
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long s = (long)x + (long)y;
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, x, y);
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (int)s;
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Add two long integers, checking for overflow.
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a an addend
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b an addend
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum <code>a+b</code>
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         long
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long addAndCheck(long a, long b) {
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION);
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Add two long integers, checking for overflow.
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a an addend
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b an addend
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param pattern the pattern to use for any thrown exception.
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum <code>a+b</code>
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         long
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static long addAndCheck(long a, long b, Localizable pattern) {
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long ret;
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (a > b) {
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // use symmetry to reduce boundary cases
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            ret = addAndCheck(b, a, pattern);
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // assert a <= b
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (a < 0) {
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (b < 0) {
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // check for negative overflow
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (Long.MIN_VALUE - b <= a) {
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ret = a + b;
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    } else {
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        throw MathRuntimeException.createArithmeticException(pattern, a, b);
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // opposite sign addition is always safe
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    ret = a + b;
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // assert a >= 0
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // assert b >= 0
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // check for positive overflow
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (a <= Long.MAX_VALUE - b) {
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    ret = a + b;
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    throw MathRuntimeException.createArithmeticException(pattern, a, b);
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return ret;
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns an exact representation of the <a
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Coefficient</a>, "<code>n choose k</code>", the number of
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>k</code>-element subsets that can be selected from an
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>n</code>-element set.
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>0 <= k <= n </code> (otherwise
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> The result is small enough to fit into a <code>long</code>. The
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * largest value of <code>n</code> for which all coefficients are
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code> < Long.MAX_VALUE</code> is 66. If the computed value exceeds
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Long.MAX_VALUE</code> an <code>ArithMeticException</code> is
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * thrown.</li>
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n the size of the set
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k the size of the subsets to be counted
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n choose k</code>
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if preconditions are not met.
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result is too large to be represented
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         by a long integer.
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long binomialCoefficient(final int n, final int k) {
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        checkBinomial(n, k);
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((n == k) || (k == 0)) {
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 1;
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((k == 1) || (k == n - 1)) {
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return n;
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Use symmetry for large k
206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (k > n / 2)
207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return binomialCoefficient(n, n - k);
208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // We use the formula
210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // (n choose k) = n! / (n-k)! / k!
211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // (n choose k) == ((n-k+1)*...*n) / (1*...*k)
212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // which could be written
213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // (n choose k) == (n-1 choose k-1) * n / k
214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long result = 1;
215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n <= 61) {
216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // For n <= 61, the naive implementation cannot overflow.
217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            int i = n - k + 1;
218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int j = 1; j <= k; j++) {
219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result = result * i / j;
220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                i++;
221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else if (n <= 66) {
223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // For n > 61 but n <= 66, the result cannot overflow,
224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // but we must take care not to overflow intermediate values.
225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            int i = n - k + 1;
226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int j = 1; j <= k; j++) {
227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // We know that (result * i) is divisible by j,
228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // but (result * i) may overflow, so we split j:
229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // Filter out the gcd, d, so j/d and i/d are integer.
230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // result is divisible by (j/d) because (j/d)
231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // is relative prime to (i/d) and is a divisor of
232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // result * (i/d).
233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final long d = gcd(i, j);
234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result = (result / (j / d)) * (i / d);
235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                i++;
236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // For n > 66, a result overflow might occur, so we check
239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // the multiplication, taking care to not overflow
240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // unnecessary.
241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            int i = n - k + 1;
242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int j = 1; j <= k; j++) {
243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                final long d = gcd(i, j);
244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result = mulAndCheck(result / (j / d), i / d);
245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                i++;
246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns a <code>double</code> representation of the <a
253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial
254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Coefficient</a>, "<code>n choose k</code>", the number of
255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>k</code>-element subsets that can be selected from an
256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>n</code>-element set.
257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>0 <= k <= n </code> (otherwise
261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> The result is small enough to fit into a <code>double</code>. The
263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * largest value of <code>n</code> for which all coefficients are <
264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE,
265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Double.POSITIVE_INFINITY is returned</li>
266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n the size of the set
269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k the size of the subsets to be counted
270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n choose k</code>
271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if preconditions are not met.
272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double binomialCoefficientDouble(final int n, final int k) {
274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        checkBinomial(n, k);
275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((n == k) || (k == 0)) {
276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 1d;
277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((k == 1) || (k == n - 1)) {
279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return n;
280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (k > n/2) {
282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return binomialCoefficientDouble(n, n - k);
283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 67) {
285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return binomialCoefficient(n,k);
286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double result = 1d;
289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i <= k; i++) {
290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             result *= (double)(n - k + i) / (double)i;
291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.floor(result + 0.5);
294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the natural <code>log</code> of the <a
298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial
299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Coefficient</a>, "<code>n choose k</code>", the number of
300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>k</code>-element subsets that can be selected from an
301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>n</code>-element set.
302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>0 <= k <= n </code> (otherwise
306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n the size of the set
310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k the size of the subsets to be counted
311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n choose k</code>
312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if preconditions are not met.
313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double binomialCoefficientLog(final int n, final int k) {
315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        checkBinomial(n, k);
316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((n == k) || (k == 0)) {
317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 0;
318dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
319dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((k == 1) || (k == n - 1)) {
320dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.log(n);
321dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
322dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
323dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /*
324dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         * For values small enough to do exact integer computation,
325dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         * return the log of the exact value
326dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         */
327dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 67) {
328dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.log(binomialCoefficient(n,k));
329dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
330dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
331dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /*
332dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         * Return the log of binomialCoefficientDouble for values that will not
333dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         * overflow binomialCoefficientDouble
334dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         */
335dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 1030) {
336dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.log(binomialCoefficientDouble(n, k));
337dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
338dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
339dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (k > n / 2) {
340dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return binomialCoefficientLog(n, n - k);
341dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
342dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
343dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /*
344dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         * Sum logs for values that could overflow
345dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         */
346dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double logSum = 0;
347dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
348dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // n!/(n-k)!
349dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = n - k + 1; i <= n; i++) {
350dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            logSum += FastMath.log(i);
351dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
352dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
353dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // divide by k!
354dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 2; i <= k; i++) {
355dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            logSum -= FastMath.log(i);
356dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
357dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
358dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return logSum;
359dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
360dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
361dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
362dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Check binomial preconditions.
363dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n the size of the set
364dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k the size of the subsets to be counted
365dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if preconditions are not met.
366dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
367dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static void checkBinomial(final int n, final int k)
368dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
369dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < k) {
370dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
371dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.BINOMIAL_INVALID_PARAMETERS_ORDER,
372dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                n, k);
373dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
374dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 0) {
375dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
376dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.BINOMIAL_NEGATIVE_PARAMETER,
377dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  n);
378dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
379dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
380dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
381dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
382dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Compares two numbers given some amount of allowed error.
383dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
384dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the first number
385dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y the second number
386dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param eps the amount of error to allow when checking for equality
387dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <ul><li>0 if  {@link #equals(double, double, double) equals(x, y, eps)}</li>
388dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *       <li>&lt; 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x &lt; y</li>
389dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *       <li>> 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x > y</li></ul>
390dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
391dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int compareTo(double x, double y, double eps) {
392dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (equals(x, y, eps)) {
393dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 0;
394dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else if (x < y) {
395dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          return -1;
396dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
397dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return 1;
398dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
399dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
400dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
401dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html">
402dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * hyperbolic cosine</a> of x.
403dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
404dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x double value for which to find the hyperbolic cosine
405dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return hyperbolic cosine of x
406dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
407dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double cosh(double x) {
408dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (FastMath.exp(x) + FastMath.exp(-x)) / 2.0;
409dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
410dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
411dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
412dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff they are strictly equal.
413dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
414dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
415dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
416dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal.
417dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.2 his method considers that {@code NaN == NaN}. In release
418dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
419dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
420dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases wher the old semantics is
421dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * useful (see e.g. {@link #equalsIncludingNaN(float,float)
422dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equalsIncludingNaN}.
423dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
424dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
425dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(float x, float y) {
426dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Float.isNaN(x) && Float.isNaN(y)) || x == y;
427dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
428dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
429dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
430dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or neither is NaN and they are
431dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equal as defined by {@link #equals(float,float,int) equals(x, y, 1)}.
432dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
433dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
434dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
435dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or both are NaN.
436dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
437dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
438dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(float x, float y) {
439dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, 1);
440dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
441dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
442dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
443dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are equal or within the range of allowed
444dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * error (inclusive).
445dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
446dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
447dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
448dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param eps the amount of absolute error to allow.
449dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or within range of each other.
450dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
451dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
452dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(float x, float y, float eps) {
453dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return equals(x, y, 1) || FastMath.abs(y - x) <= eps;
454dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
455dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
456dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
457dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or are equal or within the range
458dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * of allowed error (inclusive).
459dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
460dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
461dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
462dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param eps the amount of absolute error to allow.
463dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or within range of each other,
464dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * or both are NaN.
465dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
466dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
467dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(float x, float y, float eps) {
468dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
469dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
470dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
471dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
472dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are equal or within the range of allowed
473dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * error (inclusive).
474dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
475dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (or fewer) floating point numbers between them, i.e. two adjacent floating
476dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * point numbers are considered equal.
477dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Adapted from <a
478dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
479dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Bruce Dawson</a>
480dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
481dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
482dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
483dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
484dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * values between {@code x} and {@code y}.
485dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if there are fewer than {@code maxUlps} floating
486dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * point values between {@code x} and {@code y}.
487dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
488dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
489dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(float x, float y, int maxUlps) {
490dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Check that "maxUlps" is non-negative and small enough so that
491dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // NaN won't compare as equal to anything (except another NaN).
492dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        assert maxUlps > 0 && maxUlps < NAN_GAP;
493dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
494dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int xInt = Float.floatToIntBits(x);
495dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int yInt = Float.floatToIntBits(y);
496dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
497dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Make lexicographically ordered as a two's-complement integer.
498dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (xInt < 0) {
499dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            xInt = SGN_MASK_FLOAT - xInt;
500dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
501dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (yInt < 0) {
502dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            yInt = SGN_MASK_FLOAT - yInt;
503dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
504dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
505dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps;
506dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
507dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return isEqual && !Float.isNaN(x) && !Float.isNaN(y);
508dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
509dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
510dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
511dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or if they are equal as defined
512dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * by {@link #equals(float,float,int) equals(x, y, maxUlps)}.
513dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
514dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
515dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
516dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
517dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * values between {@code x} and {@code y}.
518dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if both arguments are NaN or if there are less than
519dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@code maxUlps} floating point values between {@code x} and {@code y}.
520dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
521dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
522dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(float x, float y, int maxUlps) {
523dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, maxUlps);
524dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
525dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
526dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
527dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff both arguments are null or have same dimensions and all
528dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * their elements are equal as defined by
529dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equals(float,float)}.
530dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
531dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first array
532dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second array
533dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return true if the values are both null or have same dimension
534dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * and equal elements.
535dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.2 this method considers that {@code NaN == NaN}. In release
536dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
537dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
538dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases where the old semantics is
539dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * useful (see e.g. {@link #equalsIncludingNaN(float[],float[])
540dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equalsIncludingNaN}.
541dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
542dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
543dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(float[] x, float[] y) {
544dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((x == null) || (y == null)) {
545dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return !((x == null) ^ (y == null));
546dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
547dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length != y.length) {
548dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return false;
549dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
550dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < x.length; ++i) {
551dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (!equals(x[i], y[i])) {
552dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return false;
553dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
554dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
555dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return true;
556dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
557dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
558dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
559dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff both arguments are null or have same dimensions and all
560dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * their elements are equal as defined by
561dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equalsIncludingNaN(float,float)}.
562dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
563dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first array
564dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second array
565dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return true if the values are both null or have same dimension and
566dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equal elements
567dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
568dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
569dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(float[] x, float[] y) {
570dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((x == null) || (y == null)) {
571dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return !((x == null) ^ (y == null));
572dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
573dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length != y.length) {
574dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return false;
575dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
576dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < x.length; ++i) {
577dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (!equalsIncludingNaN(x[i], y[i])) {
578dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return false;
579dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
580dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
581dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return true;
582dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
583dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
584dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
585dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff both arguments are NaN or neither is NaN and they are
586dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equal
587dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
588dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method considers that {@code NaN == NaN}. In release
589dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
590dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
591dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases where the old semantics
592dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (w.r.t. NaN) is useful (see e.g.
593dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equalsIncludingNaN(double,double, double) equalsIncludingNaN}.
594dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
595dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
596dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
597dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
598dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal.
599dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
600dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(double x, double y) {
601dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Double.isNaN(x) && Double.isNaN(y)) || x == y;
602dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
603dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
604dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
605dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or neither is NaN and they are
606dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equal as defined by {@link #equals(double,double,int) equals(x, y, 1)}.
607dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
608dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
609dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
610dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or both are NaN.
611dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
612dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
613dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(double x, double y) {
614dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, 1);
615dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
616dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
617dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
618dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are equal or within the range of allowed
619dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * error (inclusive).
620dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
621dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Two NaNs are considered equals, as are two infinities with same sign.
622dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
623dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method considers that {@code NaN == NaN}. In release
624dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
625dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
626dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases where the old semantics
627dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (w.r.t. NaN) is useful (see e.g.
628dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equalsIncludingNaN(double,double, double) equalsIncludingNaN}.
629dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
630dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
631dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
632dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param eps the amount of absolute error to allow.
633dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or within range of each other.
634dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
635dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(double x, double y, double eps) {
636dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return equals(x, y) || FastMath.abs(y - x) <= eps;
637dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
638dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
639dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
640dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or are equal or within the range
641dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * of allowed error (inclusive).
642dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
643dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
644dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
645dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param eps the amount of absolute error to allow.
646dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if the values are equal or within range of each other,
647dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * or both are NaN.
648dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
649dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
650dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(double x, double y, double eps) {
651dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
652dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
653dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
654dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
655dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are equal or within the range of allowed
656dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * error (inclusive).
657dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
658dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (or fewer) floating point numbers between them, i.e. two adjacent floating
659dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * point numbers are considered equal.
660dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Adapted from <a
661dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
662dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Bruce Dawson</a>
663dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
664dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method considers that {@code NaN == NaN}. In release
665dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
666dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
667dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases where the old semantics
668dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (w.r.t. NaN) is useful (see e.g.
669dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equalsIncludingNaN(double,double, int) equalsIncludingNaN}.
670dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
671dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
672dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
673dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
674dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
675dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * values between {@code x} and {@code y}.
676dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if there are fewer than {@code maxUlps} floating
677dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * point values between {@code x} and {@code y}.
678dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
679dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(double x, double y, int maxUlps) {
680dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Check that "maxUlps" is non-negative and small enough so that
681dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // NaN won't compare as equal to anything (except another NaN).
682dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        assert maxUlps > 0 && maxUlps < NAN_GAP;
683dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
684dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long xInt = Double.doubleToLongBits(x);
685dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long yInt = Double.doubleToLongBits(y);
686dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
687dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Make lexicographically ordered as a two's-complement integer.
688dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (xInt < 0) {
689dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            xInt = SGN_MASK - xInt;
690dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
691dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (yInt < 0) {
692dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            yInt = SGN_MASK - yInt;
693dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
694dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
695dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.abs(xInt - yInt) <= maxUlps;
696dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
697dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
698dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
699dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true if both arguments are NaN or if they are equal as defined
700dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * by {@link #equals(double,double,int) equals(x, y, maxUlps}.
701dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
702dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first value
703dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second value
704dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
705dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * values between {@code x} and {@code y}.
706dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return {@code true} if both arguments are NaN or if there are less than
707dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@code maxUlps} floating point values between {@code x} and {@code y}.
708dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
709dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
710dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(double x, double y, int maxUlps) {
711dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, maxUlps);
712dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
713dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
714dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
715dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff both arguments are null or have same dimensions and all
716dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * their elements are equal as defined by
717dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equals(double,double)}.
718dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
719dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method considers that {@code NaN == NaN}. In release
720dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 3.0, the semantics will change in order to comply with IEEE754 where it
721dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is specified that {@code NaN != NaN}.
722dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * New methods have been added for those cases wher the old semantics is
723dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * useful (see e.g. {@link #equalsIncludingNaN(double[],double[])
724dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equalsIncludingNaN}.
725dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
726dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first array
727dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second array
728dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return true if the values are both null or have same dimension
729dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * and equal elements.
730dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
731dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equals(double[] x, double[] y) {
732dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((x == null) || (y == null)) {
733dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return !((x == null) ^ (y == null));
734dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
735dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length != y.length) {
736dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return false;
737dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
738dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < x.length; ++i) {
739dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (!equals(x[i], y[i])) {
740dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return false;
741dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
742dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
743dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return true;
744dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
745dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
746dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
747dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns true iff both arguments are null or have same dimensions and all
748dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * their elements are equal as defined by
749dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link #equalsIncludingNaN(double,double)}.
750dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
751dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x first array
752dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y second array
753dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return true if the values are both null or have same dimension and
754dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * equal elements
755dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
756dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
757dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static boolean equalsIncludingNaN(double[] x, double[] y) {
758dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((x == null) || (y == null)) {
759dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return !((x == null) ^ (y == null));
760dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
761dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length != y.length) {
762dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return false;
763dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
764dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < x.length; ++i) {
765dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (!equalsIncludingNaN(x[i], y[i])) {
766dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return false;
767dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
768dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
769dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return true;
770dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
771dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
772dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
773dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns n!. Shorthand for <code>n</code> <a
774dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
775dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * product of the numbers <code>1,...,n</code>.
776dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
777dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
778dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
779dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>n >= 0</code> (otherwise
780dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
781dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> The result is small enough to fit into a <code>long</code>. The
782dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * largest value of <code>n</code> for which <code>n!</code> <
783dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Long.MAX_VALUE</code> is 20. If the computed value exceeds <code>Long.MAX_VALUE</code>
784dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * an <code>ArithMeticException </code> is thrown.</li>
785dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
786dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
787dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
788dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n argument
789dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n!</code>
790dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result is too large to be represented
791dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         by a long integer.
792dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if n < 0
793dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
794dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long factorial(final int n) {
795dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 0) {
796dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
797dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
798dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  n);
799dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
800dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n > 20) {
801dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw new ArithmeticException(
802dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    "factorial value is too large to fit in a long");
803dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
804dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FACTORIALS[n];
805dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
806dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
807dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
808dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns n!. Shorthand for <code>n</code> <a
809dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
810dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * product of the numbers <code>1,...,n</code> as a <code>double</code>.
811dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
812dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
813dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
814dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>n >= 0</code> (otherwise
815dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
816dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> The result is small enough to fit into a <code>double</code>. The
817dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * largest value of <code>n</code> for which <code>n!</code> <
818dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Double.MAX_VALUE</code> is 170. If the computed value exceeds
819dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li>
820dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
821dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
822dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
823dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n argument
824dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n!</code>
825dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if n < 0
826dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
827dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double factorialDouble(final int n) {
828dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 0) {
829dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
830dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
831dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  n);
832dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
833dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 21) {
834dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return factorial(n);
835dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
836dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.floor(FastMath.exp(factorialLog(n)) + 0.5);
837dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
838dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
839dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
840dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the natural logarithm of n!.
841dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
842dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <Strong>Preconditions</strong>:
843dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
844dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li> <code>n >= 0</code> (otherwise
845dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>IllegalArgumentException</code> is thrown)</li>
846dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
847dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
848dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param n argument
849dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return <code>n!</code>
850dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if preconditions are not met.
851dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
852dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double factorialLog(final int n) {
853dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 0) {
854dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
855dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER,
856dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  n);
857dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
858dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 21) {
859dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.log(factorial(n));
860dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
861dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double logSum = 0;
862dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 2; i <= n; i++) {
863dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            logSum += FastMath.log(i);
864dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
865dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return logSum;
866dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
867dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
868dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
869dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
870dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Gets the greatest common divisor of the absolute value of two numbers,
871dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * using the "binary gcd" method which avoids division and modulo
872dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef
873dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Stein (1961).
874dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
875dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Special cases:
876dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
877dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocations
878dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)</code>,
879dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(Integer.MIN_VALUE, 0)</code> and
880dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(0, Integer.MIN_VALUE)</code> throw an
881dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>ArithmeticException</code>, because the result would be 2^31, which
882dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is too large for an int value.</li>
883dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result of <code>gcd(x, x)</code>, <code>gcd(0, x)</code> and
884dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(x, 0)</code> is the absolute value of <code>x</code>, except
885dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for the special cases above.
886dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocation <code>gcd(0, 0)</code> is the only one which returns
887dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>0</code>.</li>
888dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
889dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
890dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p any number
891dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param q any number
892dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the greatest common divisor, never negative
893dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result cannot be represented as a
894dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * nonnegative int value
895dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
896dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
897dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int gcd(final int p, final int q) {
898dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int u = p;
899dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int v = q;
900dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((u == 0) || (v == 0)) {
901dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((u == Integer.MIN_VALUE) || (v == Integer.MIN_VALUE)) {
902dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw MathRuntimeException.createArithmeticException(
903dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        LocalizedFormats.GCD_OVERFLOW_32_BITS,
904dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        p, q);
905dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
906dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.abs(u) + FastMath.abs(v);
907dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
908dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // keep u and v negative, as negative integers range down to
909dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // -2^31, while positive numbers can only be as large as 2^31-1
910dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // (i.e. we can't necessarily negate a negative number without
911dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // overflow)
912dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /* assert u!=0 && v!=0; */
913dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (u > 0) {
914dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            u = -u;
915dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } // make u negative
916dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (v > 0) {
917dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            v = -v;
918dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } // make v negative
919dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // B1. [Find power of 2]
920dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int k = 0;
921dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { // while u and v are
922dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                                            // both even...
923dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            u /= 2;
924dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            v /= 2;
925dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k++; // cast out twos.
926dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
927dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (k == 31) {
928dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(
929dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.GCD_OVERFLOW_32_BITS,
930dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    p, q);
931dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
932dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // B2. Initialize: u and v have been divided by 2^k and at least
933dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // one is odd.
934dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
935dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // t negative: u was odd, v may be even (t replaces v)
936dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // t positive: u was even, v is odd (t replaces u)
937dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        do {
938dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            /* assert u<0 && v<0; */
939dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B4/B3: cast out twos from t.
940dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            while ((t & 1) == 0) { // while t is even..
941dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                t /= 2; // cast out twos
942dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
943dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B5 [reset max(u,v)]
944dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (t > 0) {
945dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                u = -t;
946dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
947dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                v = t;
948dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
949dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B6/B3. at this point both u and v should be odd.
950dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            t = (v - u) / 2;
951dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // |u| larger: t positive (replace u)
952dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // |v| larger: t negative (replace v)
953dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } while (t != 0);
954dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return -u * (1 << k); // gcd is u*2^k
955dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
956dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
957dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
958dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
959dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Gets the greatest common divisor of the absolute value of two numbers,
960dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * using the "binary gcd" method which avoids division and modulo
961dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef
962dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Stein (1961).
963dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
964dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Special cases:
965dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
966dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocations
967dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(Long.MIN_VALUE, Long.MIN_VALUE)</code>,
968dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(Long.MIN_VALUE, 0L)</code> and
969dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(0L, Long.MIN_VALUE)</code> throw an
970dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>ArithmeticException</code>, because the result would be 2^63, which
971dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is too large for a long value.</li>
972dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result of <code>gcd(x, x)</code>, <code>gcd(0L, x)</code> and
973dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>gcd(x, 0L)</code> is the absolute value of <code>x</code>, except
974dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for the special cases above.
975dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocation <code>gcd(0L, 0L)</code> is the only one which returns
976dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>0L</code>.</li>
977dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
978dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
979dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p any number
980dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param q any number
981dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the greatest common divisor, never negative
982dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result cannot be represented as a nonnegative long
983dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value
984dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.1
985dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
986dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long gcd(final long p, final long q) {
987dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long u = p;
988dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long v = q;
989dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if ((u == 0) || (v == 0)) {
990dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)){
991dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw MathRuntimeException.createArithmeticException(
992dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        LocalizedFormats.GCD_OVERFLOW_64_BITS,
993dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        p, q);
994dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
995dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.abs(u) + FastMath.abs(v);
996dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
997dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // keep u and v negative, as negative integers range down to
998dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // -2^63, while positive numbers can only be as large as 2^63-1
999dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // (i.e. we can't necessarily negate a negative number without
1000dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // overflow)
1001dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /* assert u!=0 && v!=0; */
1002dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (u > 0) {
1003dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            u = -u;
1004dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } // make u negative
1005dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (v > 0) {
1006dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            v = -v;
1007dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } // make v negative
1008dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // B1. [Find power of 2]
1009dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int k = 0;
1010dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while ((u & 1) == 0 && (v & 1) == 0 && k < 63) { // while u and v are
1011dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                                            // both even...
1012dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            u /= 2;
1013dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            v /= 2;
1014dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k++; // cast out twos.
1015dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1016dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (k == 63) {
1017dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(
1018dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.GCD_OVERFLOW_64_BITS,
1019dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    p, q);
1020dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1021dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // B2. Initialize: u and v have been divided by 2^k and at least
1022dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // one is odd.
1023dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
1024dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // t negative: u was odd, v may be even (t replaces v)
1025dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // t positive: u was even, v is odd (t replaces u)
1026dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        do {
1027dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            /* assert u<0 && v<0; */
1028dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B4/B3: cast out twos from t.
1029dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            while ((t & 1) == 0) { // while t is even..
1030dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                t /= 2; // cast out twos
1031dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1032dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B5 [reset max(u,v)]
1033dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (t > 0) {
1034dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                u = -t;
1035dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1036dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                v = t;
1037dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1038dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // B6/B3. at this point both u and v should be odd.
1039dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            t = (v - u) / 2;
1040dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // |u| larger: t positive (replace u)
1041dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // |v| larger: t negative (replace v)
1042dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } while (t != 0);
1043dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return -u * (1L << k); // gcd is u*2^k
1044dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1045dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1046dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1047dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns an integer hash code representing the given double value.
1048dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1049dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param value the value to be hashed
1050dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the hash code
1051dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1052dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int hash(double value) {
1053dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return new Double(value).hashCode();
1054dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1055dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1056dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1057dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns an integer hash code representing the given double array.
1058dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1059dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param value the value to be hashed (may be null)
1060dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the hash code
1061dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1062dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1063dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int hash(double[] value) {
1064dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return Arrays.hashCode(value);
1065dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1066dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1067dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1068dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a byte value x, this method returns (byte)(+1) if x >= 0 and
1069dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (byte)(-1) if x < 0.
1070dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1071dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a byte
1072dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return (byte)(+1) or (byte)(-1), depending on the sign of x
1073dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1074dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static byte indicator(final byte x) {
1075dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= ZB) ? PB : NB;
1076dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1077dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1078dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1079dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a double precision value x, this method returns +1.0 if x >= 0 and
1080dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * -1.0 if x < 0. Returns <code>NaN</code> if <code>x</code> is
1081dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>NaN</code>.
1082dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1083dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a double
1084dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1.0 or -1.0, depending on the sign of x
1085dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1086dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double indicator(final double x) {
1087dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (Double.isNaN(x)) {
1088dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Double.NaN;
1089dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1090dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= 0.0) ? 1.0 : -1.0;
1091dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1092dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1093dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1094dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x <
1095dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 0. Returns <code>NaN</code> if <code>x</code> is <code>NaN</code>.
1096dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1097dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a float
1098dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1.0F or -1.0F, depending on the sign of x
1099dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static float indicator(final float x) {
1101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (Float.isNaN(x)) {
1102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Float.NaN;
1103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= 0.0F) ? 1.0F : -1.0F;
1105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For an int value x, this method returns +1 if x >= 0 and -1 if x < 0.
1109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, an int
1111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1 or -1, depending on the sign of x
1112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int indicator(final int x) {
1114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= 0) ? 1 : -1;
1115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a long value x, this method returns +1L if x >= 0 and -1L if x < 0.
1119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a long
1121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1L or -1L, depending on the sign of x
1122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long indicator(final long x) {
1124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= 0L) ? 1L : -1L;
1125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a short value x, this method returns (short)(+1) if x >= 0 and
1129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (short)(-1) if x < 0.
1130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a short
1132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return (short)(+1) or (short)(-1), depending on the sign of x
1133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static short indicator(final short x) {
1135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x >= ZS) ? PS : NS;
1136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the least common multiple of the absolute value of two numbers,
1141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * using the formula <code>lcm(a,b) = (a / gcd(a,b)) * b</code>.
1142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
1143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Special cases:
1144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
1145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocations <code>lcm(Integer.MIN_VALUE, n)</code> and
1146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>lcm(n, Integer.MIN_VALUE)</code>, where <code>abs(n)</code> is a
1147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * power of 2, throw an <code>ArithmeticException</code>, because the result
1148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * would be 2^31, which is too large for an int value.</li>
1149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result of <code>lcm(0, x)</code> and <code>lcm(x, 0)</code> is
1150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>0</code> for any <code>x</code>.
1151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
1152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a any number
1154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b any number
1155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the least common multiple, never negative
1156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException
1157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *             if the result cannot be represented as a nonnegative int
1158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *             value
1159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int lcm(int a, int b) {
1162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (a==0 || b==0){
1163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 0;
1164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b));
1166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (lcm == Integer.MIN_VALUE) {
1167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(
1168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.LCM_OVERFLOW_32_BITS,
1169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                a, b);
1170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return lcm;
1172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the least common multiple of the absolute value of two numbers,
1177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * using the formula <code>lcm(a,b) = (a / gcd(a,b)) * b</code>.
1178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
1179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Special cases:
1180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
1181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The invocations <code>lcm(Long.MIN_VALUE, n)</code> and
1182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>lcm(n, Long.MIN_VALUE)</code>, where <code>abs(n)</code> is a
1183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * power of 2, throw an <code>ArithmeticException</code>, because the result
1184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * would be 2^63, which is too large for an int value.</li>
1185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result of <code>lcm(0L, x)</code> and <code>lcm(x, 0L)</code> is
1186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>0L</code> for any <code>x</code>.
1187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
1188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a any number
1190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b any number
1191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the least common multiple, never negative
1192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result cannot be represented as a nonnegative long
1193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value
1194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.1
1195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long lcm(long a, long b) {
1197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (a==0 || b==0){
1198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return 0;
1199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b));
1201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (lcm == Long.MIN_VALUE){
1202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(
1203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.LCM_OVERFLOW_64_BITS,
1204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                a, b);
1205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return lcm;
1207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Returns the
1211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <a href="http://mathworld.wolfram.com/Logarithm.html">logarithm</a>
1212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for base <code>b</code> of <code>x</code>.
1213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
1214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Returns <code>NaN<code> if either argument is negative.  If
1215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>base</code> is 0 and <code>x</code> is positive, 0 is returned.
1216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * If <code>base</code> is positive and <code>x</code> is 0,
1217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NEGATIVE_INFINITY</code> is returned.  If both arguments
1218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * are 0, the result is <code>NaN</code>.</p>
1219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param base the base of the logarithm, must be greater than 0
1221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x argument, must be greater than 0
1222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the value of the logarithm - the number y such that base^y = x.
1223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double log(double base, double x) {
1226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.log(x)/FastMath.log(base);
1227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Multiply two integers, checking for overflow.
1231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x a factor
1233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y a factor
1234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the product <code>x*y</code>
1235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
1236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         int
1237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int mulAndCheck(int x, int y) {
1240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long m = ((long)x) * ((long)y);
1241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) {
1242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw new ArithmeticException("overflow: mul");
1243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (int)m;
1245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Multiply two long integers, checking for overflow.
1249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a first value
1251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b second value
1252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the product <code>a * b</code>
1253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
1254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         long
1255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long mulAndCheck(long a, long b) {
1258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long ret;
1259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        String msg = "overflow: multiply";
1260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (a > b) {
1261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // use symmetry to reduce boundary cases
1262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            ret = mulAndCheck(b, a);
1263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
1264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (a < 0) {
1265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (b < 0) {
1266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // check for positive overflow with negative a, negative b
1267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (a >= Long.MAX_VALUE / b) {
1268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ret = a * b;
1269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    } else {
1270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        throw new ArithmeticException(msg);
1271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
1272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else if (b > 0) {
1273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // check for negative overflow with negative a, positive b
1274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (Long.MIN_VALUE / b <= a) {
1275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ret = a * b;
1276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    } else {
1277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        throw new ArithmeticException(msg);
1278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
1280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
1281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    // assert b == 0
1282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    ret = 0;
1283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
1284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else if (a > 0) {
1285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // assert a > 0
1286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // assert b > 0
1287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // check for positive overflow with positive a, positive b
1289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (a <= Long.MAX_VALUE / b) {
1290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    ret = a * b;
1291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
1292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    throw new ArithmeticException(msg);
1293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
1294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // assert a == 0
1296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                ret = 0;
1297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return ret;
1300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Get the next machine representable number after a number, moving
1304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * in the direction of another number.
1305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * If <code>direction</code> is greater than or equal to<code>d</code>,
1307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the smallest machine representable number strictly greater than
1308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>d</code> is returned; otherwise the largest representable number
1309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * strictly less than <code>d</code> is returned.</p>
1310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * If <code>d</code> is NaN or Infinite, it is returned unchanged.</p>
1312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param d base number
1314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param direction (the only important thing is whether
1315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * direction is greater or smaller than d)
1316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the next machine representable number in the specified direction
1317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1318dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.2, replaced by {@link FastMath#nextAfter(double, double)}
1319dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * which handles Infinities differently, and returns direction if d and direction compare equal.
1320dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1321dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
1322dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double nextAfter(double d, double direction) {
1323dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1324dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // handling of some important special cases
1325dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (Double.isNaN(d) || Double.isInfinite(d)) {
1326dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return d;
1327dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else if (d == 0) {
1328dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return (direction < 0) ? -Double.MIN_VALUE : Double.MIN_VALUE;
1329dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1330dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // special cases MAX_VALUE to infinity and  MIN_VALUE to 0
1331dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // are handled just as normal numbers
1332dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1333dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // split the double in raw components
1334dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long bits     = Double.doubleToLongBits(d);
1335dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long sign     = bits & 0x8000000000000000L;
1336dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long exponent = bits & 0x7ff0000000000000L;
1337dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long mantissa = bits & 0x000fffffffffffffL;
1338dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1339dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (d * (direction - d) >= 0) {
1340dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // we should increase the mantissa
1341dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (mantissa == 0x000fffffffffffffL) {
1342dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        return Double.longBitsToDouble(sign |
1343dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                        (exponent + 0x0010000000000000L));
1344dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
1345dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        return Double.longBitsToDouble(sign |
1346dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                        exponent | (mantissa + 1));
1347dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
1348dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
1349dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // we should decrease the mantissa
1350dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (mantissa == 0L) {
1351dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        return Double.longBitsToDouble(sign |
1352dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                        (exponent - 0x0010000000000000L) |
1353dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                        0x000fffffffffffffL);
1354dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
1355dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        return Double.longBitsToDouble(sign |
1356dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                        exponent | (mantissa - 1));
1357dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
1358dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1359dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1360dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1361dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1362dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Scale a number by 2<sup>scaleFactor</sup>.
1363dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>If <code>d</code> is 0 or NaN or Infinite, it is returned unchanged.</p>
1364dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1365dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param d base number
1366dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param scaleFactor power of two by which d should be multiplied
1367dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return d &times; 2<sup>scaleFactor</sup>
1368dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.0
1369dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.2, replaced by {@link FastMath#scalb(double, int)}
1370dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1371dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
1372dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double scalb(final double d, final int scaleFactor) {
1373dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.scalb(d, scaleFactor);
1374dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1375dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1376dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1377dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Normalize an angle in a 2&pi wide interval around a center value.
1378dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method has three main uses:</p>
1379dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
1380dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *   <li>normalize an angle between 0 and 2&pi;:<br/>
1381dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *       <code>a = MathUtils.normalizeAngle(a, FastMath.PI);</code></li>
1382dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *   <li>normalize an angle between -&pi; and +&pi;<br/>
1383dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *       <code>a = MathUtils.normalizeAngle(a, 0.0);</code></li>
1384dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *   <li>compute the angle between two defining angular positions:<br>
1385dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *       <code>angle = MathUtils.normalizeAngle(end, start) - start;</code></li>
1386dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul>
1387dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Note that due to numerical accuracy and since &pi; cannot be represented
1388dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * exactly, the result interval is <em>closed</em>, it cannot be half-closed
1389dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * as would be more satisfactory in a purely mathematical view.</p>
1390dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a angle to normalize
1391dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param center center of the desired 2&pi; interval for the result
1392dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return a-2k&pi; with integer k and center-&pi; &lt;= a-2k&pi; &lt;= center+&pi;
1393dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1394dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1395dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     public static double normalizeAngle(double a, double center) {
1396dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI);
1397dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     }
1398dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1399dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     /**
1400dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * <p>Normalizes an array to make it sum to a specified value.
1401dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * Returns the result of the transformation <pre>
1402dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      *    x |-> x * normalizedSum / sum
1403dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * </pre>
1404dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * applied to each non-NaN element x of the input array, where sum is the
1405dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * sum of the non-NaN entries in the input array.</p>
1406dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      *
1407dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * <p>Throws IllegalArgumentException if <code>normalizedSum</code> is infinite
1408dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * or NaN and ArithmeticException if the input array contains any infinite elements
1409dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * or sums to 0</p>
1410dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      *
1411dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
1412dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      *
1413dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @param values input array to be normalized
1414dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @param normalizedSum target sum for the normalized array
1415dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @return normalized array
1416dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @throws ArithmeticException if the input array contains infinite elements or sums to zero
1417dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @throws IllegalArgumentException if the target sum is infinite or NaN
1418dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * @since 2.1
1419dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      */
1420dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     public static double[] normalizeArray(double[] values, double normalizedSum)
1421dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond       throws ArithmeticException, IllegalArgumentException {
1422dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         if (Double.isInfinite(normalizedSum)) {
1423dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             throw MathRuntimeException.createIllegalArgumentException(
1424dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                     LocalizedFormats.NORMALIZE_INFINITE);
1425dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         }
1426dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         if (Double.isNaN(normalizedSum)) {
1427dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             throw MathRuntimeException.createIllegalArgumentException(
1428dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                     LocalizedFormats.NORMALIZE_NAN);
1429dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         }
1430dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         double sum = 0d;
1431dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         final int len = values.length;
1432dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         double[] out = new double[len];
1433dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         for (int i = 0; i < len; i++) {
1434dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             if (Double.isInfinite(values[i])) {
1435dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 throw MathRuntimeException.createArithmeticException(
1436dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                         LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
1437dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             }
1438dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             if (!Double.isNaN(values[i])) {
1439dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 sum += values[i];
1440dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             }
1441dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         }
1442dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         if (sum == 0) {
1443dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             throw MathRuntimeException.createArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
1444dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         }
1445dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         for (int i = 0; i < len; i++) {
1446dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             if (Double.isNaN(values[i])) {
1447dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 out[i] = Double.NaN;
1448dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             } else {
1449dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 out[i] = values[i] * normalizedSum / sum;
1450dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond             }
1451dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         }
1452dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         return out;
1453dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     }
1454dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1455dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1456dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Round the given value to the specified number of decimal places. The
1457dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method.
1458dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1459dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value to round.
1460dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param scale the number of digits to the right of the decimal point.
1461dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the rounded value.
1462dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1463dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1464dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double round(double x, int scale) {
1465dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return round(x, scale, BigDecimal.ROUND_HALF_UP);
1466dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1467dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1468dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1469dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Round the given value to the specified number of decimal places. The
1470dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value is rounded using the given method which is any method defined in
1471dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link BigDecimal}.
1472dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1473dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value to round.
1474dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param scale the number of digits to the right of the decimal point.
1475dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param roundingMethod the rounding method as defined in
1476dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *        {@link BigDecimal}.
1477dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the rounded value.
1478dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1479dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1480dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double round(double x, int scale, int roundingMethod) {
1481dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        try {
1482dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return (new BigDecimal
1483dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                   (Double.toString(x))
1484dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                   .setScale(scale, roundingMethod))
1485dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                   .doubleValue();
1486dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } catch (NumberFormatException ex) {
1487dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (Double.isInfinite(x)) {
1488dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return x;
1489dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1490dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return Double.NaN;
1491dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1492dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1493dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1494dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1495dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1496dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Round the given value to the specified number of decimal places. The
1497dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value is rounding using the {@link BigDecimal#ROUND_HALF_UP} method.
1498dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1499dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value to round.
1500dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param scale the number of digits to the right of the decimal point.
1501dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the rounded value.
1502dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1503dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1504dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static float round(float x, int scale) {
1505dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return round(x, scale, BigDecimal.ROUND_HALF_UP);
1506dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1507dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1508dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1509dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Round the given value to the specified number of decimal places. The
1510dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * value is rounded using the given method which is any method defined in
1511dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@link BigDecimal}.
1512dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1513dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value to round.
1514dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param scale the number of digits to the right of the decimal point.
1515dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param roundingMethod the rounding method as defined in
1516dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *        {@link BigDecimal}.
1517dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the rounded value.
1518dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1519dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1520dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static float round(float x, int scale, int roundingMethod) {
1521dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        float sign = indicator(x);
1522dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        float factor = (float)FastMath.pow(10.0f, scale) * sign;
1523dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (float)roundUnscaled(x * factor, sign, roundingMethod) / factor;
1524dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1525dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1526dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1527dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Round the given non-negative, value to the "nearest" integer. Nearest is
1528dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * determined by the rounding method specified. Rounding methods are defined
1529dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * in {@link BigDecimal}.
1530dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1531dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param unscaled the value to round.
1532dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sign the sign of the original, scaled value.
1533dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param roundingMethod the rounding method as defined in
1534dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *        {@link BigDecimal}.
1535dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the rounded value.
1536dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1537dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1538dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static double roundUnscaled(double unscaled, double sign,
1539dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int roundingMethod) {
1540dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        switch (roundingMethod) {
1541dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_CEILING :
1542dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (sign == -1) {
1543dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
1544dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1545dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.ceil(nextAfter(unscaled, Double.POSITIVE_INFINITY));
1546dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1547dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1548dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_DOWN :
1549dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            unscaled = FastMath.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
1550dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1551dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_FLOOR :
1552dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (sign == -1) {
1553dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.ceil(nextAfter(unscaled, Double.POSITIVE_INFINITY));
1554dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1555dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
1556dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1557dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1558dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_HALF_DOWN : {
1559dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            unscaled = nextAfter(unscaled, Double.NEGATIVE_INFINITY);
1560dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double fraction = unscaled - FastMath.floor(unscaled);
1561dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (fraction > 0.5) {
1562dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.ceil(unscaled);
1563dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1564dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.floor(unscaled);
1565dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1566dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1567dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1568dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_HALF_EVEN : {
1569dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double fraction = unscaled - FastMath.floor(unscaled);
1570dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (fraction > 0.5) {
1571dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.ceil(unscaled);
1572dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else if (fraction < 0.5) {
1573dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.floor(unscaled);
1574dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1575dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // The following equality test is intentional and needed for rounding purposes
1576dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math
1577dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    .floor(unscaled) / 2.0)) { // even
1578dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    unscaled = FastMath.floor(unscaled);
1579dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else { // odd
1580dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    unscaled = FastMath.ceil(unscaled);
1581dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
1582dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1583dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1584dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1585dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_HALF_UP : {
1586dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            unscaled = nextAfter(unscaled, Double.POSITIVE_INFINITY);
1587dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double fraction = unscaled - FastMath.floor(unscaled);
1588dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (fraction >= 0.5) {
1589dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.ceil(unscaled);
1590dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1591dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                unscaled = FastMath.floor(unscaled);
1592dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1593dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1594dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1595dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_UNNECESSARY :
1596dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (unscaled != FastMath.floor(unscaled)) {
1597dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw new ArithmeticException("Inexact result from rounding");
1598dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1599dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1600dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        case BigDecimal.ROUND_UP :
1601dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            unscaled = FastMath.ceil(nextAfter(unscaled,  Double.POSITIVE_INFINITY));
1602dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            break;
1603dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        default :
1604dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1605dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.INVALID_ROUNDING_METHOD,
1606dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  roundingMethod,
1607dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_CEILING",     BigDecimal.ROUND_CEILING,
1608dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_DOWN",        BigDecimal.ROUND_DOWN,
1609dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_FLOOR",       BigDecimal.ROUND_FLOOR,
1610dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_HALF_DOWN",   BigDecimal.ROUND_HALF_DOWN,
1611dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_HALF_EVEN",   BigDecimal.ROUND_HALF_EVEN,
1612dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_HALF_UP",     BigDecimal.ROUND_HALF_UP,
1613dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY,
1614dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  "ROUND_UP",          BigDecimal.ROUND_UP);
1615dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1616dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return unscaled;
1617dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1618dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1619dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1620dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1621dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for byte value <code>x</code>.
1622dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1623dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a byte value x, this method returns (byte)(+1) if x > 0, (byte)(0) if
1624dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * x = 0, and (byte)(-1) if x < 0.</p>
1625dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1626dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a byte
1627dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return (byte)(+1), (byte)(0), or (byte)(-1), depending on the sign of x
1628dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1629dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static byte sign(final byte x) {
1630dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == ZB) ? ZB : (x > ZB) ? PB : NB;
1631dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1632dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1633dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1634dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1635dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for double precision <code>x</code>.
1636dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1637dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a double value <code>x</code>, this method returns
1638dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>+1.0</code> if <code>x > 0</code>, <code>0.0</code> if
1639dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>x = 0.0</code>, and <code>-1.0</code> if <code>x < 0</code>.
1640dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns <code>NaN</code> if <code>x</code> is <code>NaN</code>.</p>
1641dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1642dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a double
1643dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1.0, 0.0, or -1.0, depending on the sign of x
1644dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1645dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sign(final double x) {
1646dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (Double.isNaN(x)) {
1647dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Double.NaN;
1648dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1649dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == 0.0) ? 0.0 : (x > 0.0) ? 1.0 : -1.0;
1650dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1651dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1652dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1653dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1654dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for float value <code>x</code>.
1655dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1656dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a float value x, this method returns +1.0F if x > 0, 0.0F if x =
1657dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 0.0F, and -1.0F if x < 0. Returns <code>NaN</code> if <code>x</code>
1658dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is <code>NaN</code>.</p>
1659dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1660dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a float
1661dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1.0F, 0.0F, or -1.0F, depending on the sign of x
1662dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1663dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static float sign(final float x) {
1664dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (Float.isNaN(x)) {
1665dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Float.NaN;
1666dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1667dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == 0.0F) ? 0.0F : (x > 0.0F) ? 1.0F : -1.0F;
1668dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1669dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1670dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1671dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1672dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for int value <code>x</code>.
1673dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1674dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For an int value x, this method returns +1 if x > 0, 0 if x = 0, and -1
1675dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * if x < 0.</p>
1676dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1677dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, an int
1678dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1, 0, or -1, depending on the sign of x
1679dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1680dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int sign(final int x) {
1681dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == 0) ? 0 : (x > 0) ? 1 : -1;
1682dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1683dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1684dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1685dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1686dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for long value <code>x</code>.
1687dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1688dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a long value x, this method returns +1L if x > 0, 0L if x = 0, and
1689dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * -1L if x < 0.</p>
1690dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1691dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a long
1692dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return +1L, 0L, or -1L, depending on the sign of x
1693dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1694dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long sign(final long x) {
1695dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == 0L) ? 0L : (x > 0L) ? 1L : -1L;
1696dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1697dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1698dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1699dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a>
1700dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for short value <code>x</code>.
1701dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
1702dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * For a short value x, this method returns (short)(+1) if x > 0, (short)(0)
1703dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * if x = 0, and (short)(-1) if x < 0.</p>
1704dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1705dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the value, a short
1706dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return (short)(+1), (short)(0), or (short)(-1), depending on the sign of
1707dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         x
1708dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1709dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static short sign(final short x) {
1710dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (x == ZS) ? ZS : (x > ZS) ? PS : NS;
1711dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1712dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1713dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1714dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the <a href="http://mathworld.wolfram.com/HyperbolicSine.html">
1715dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * hyperbolic sine</a> of x.
1716dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1717dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x double value for which to find the hyperbolic sine
1718dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return hyperbolic sine of x
1719dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1720dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sinh(double x) {
1721dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (FastMath.exp(x) - FastMath.exp(-x)) / 2.0;
1722dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1723dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1724dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1725dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Subtract two integers, checking for overflow.
1726dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1727dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the minuend
1728dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y the subtrahend
1729dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the difference <code>x-y</code>
1730dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
1731dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         int
1732dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.1
1733dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1734dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int subAndCheck(int x, int y) {
1735dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long s = (long)x - (long)y;
1736dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
1737dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y);
1738dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1739dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (int)s;
1740dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1741dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1742dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1743dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Subtract two long integers, checking for overflow.
1744dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1745dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param a first value
1746dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param b second value
1747dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the difference <code>a-b</code>
1748dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws ArithmeticException if the result can not be represented as an
1749dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *         long
1750dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 1.2
1751dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1752dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long subAndCheck(long a, long b) {
1753dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long ret;
1754dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        String msg = "overflow: subtract";
1755dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (b == Long.MIN_VALUE) {
1756dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (a < 0) {
1757dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                ret = a - b;
1758dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
1759dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw new ArithmeticException(msg);
1760dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1761dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
1762dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // use additive inverse
1763dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION);
1764dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1765dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return ret;
1766dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1767dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1768dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1769dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise an int to an int power.
1770dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1771dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1772dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1773dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1774dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1775dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int pow(final int k, int e)
1776dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1777dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1778dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1779dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1780dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1781dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1782dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1783dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1784dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int result = 1;
1785dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int k2p    = k;
1786dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (e != 0) {
1787dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((e & 0x1) != 0) {
1788dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result *= k2p;
1789dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1790dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p *= k2p;
1791dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e >> 1;
1792dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1793dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1794dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1795dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1796dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1797dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1798dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1799dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise an int to a long power.
1800dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1801dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1802dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1803dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1804dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1805dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int pow(final int k, long e)
1806dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1807dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1808dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1809dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1810dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1811dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1812dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1813dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1814dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int result = 1;
1815dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int k2p    = k;
1816dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (e != 0) {
1817dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((e & 0x1) != 0) {
1818dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result *= k2p;
1819dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1820dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p *= k2p;
1821dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e >> 1;
1822dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1823dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1824dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1825dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1826dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1827dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1828dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1829dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise a long to an int power.
1830dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1831dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1832dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1833dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1834dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1835dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long pow(final long k, int e)
1836dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1837dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1838dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1839dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1840dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1841dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1842dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1843dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1844dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long result = 1l;
1845dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long k2p    = k;
1846dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (e != 0) {
1847dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((e & 0x1) != 0) {
1848dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result *= k2p;
1849dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1850dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p *= k2p;
1851dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e >> 1;
1852dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1853dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1854dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1855dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1856dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1857dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1858dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1859dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise a long to a long power.
1860dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1861dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1862dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1863dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1864dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1865dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static long pow(final long k, long e)
1866dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1867dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1868dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1869dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1870dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1871dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1872dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1873dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1874dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long result = 1l;
1875dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        long k2p    = k;
1876dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (e != 0) {
1877dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((e & 0x1) != 0) {
1878dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result *= k2p;
1879dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1880dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p *= k2p;
1881dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e >> 1;
1882dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1883dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1884dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1885dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1886dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1887dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1888dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1889dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise a BigInteger to an int power.
1890dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1891dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1892dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1893dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1894dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1895dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static BigInteger pow(final BigInteger k, int e)
1896dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1897dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1898dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1899dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1900dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1901dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1902dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1903dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1904dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return k.pow(e);
1905dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1906dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1907dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1908dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1909dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise a BigInteger to a long power.
1910dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1911dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1912dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1913dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1914dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1915dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static BigInteger pow(final BigInteger k, long e)
1916dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1917dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1918dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e < 0) {
1919dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1920dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1921dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1922dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1923dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1924dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        BigInteger result = BigInteger.ONE;
1925dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        BigInteger k2p    = k;
1926dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (e != 0) {
1927dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((e & 0x1) != 0) {
1928dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result = result.multiply(k2p);
1929dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1930dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p = k2p.multiply(k2p);
1931dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e >> 1;
1932dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1933dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1934dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1935dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1936dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1937dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1938dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1939dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Raise a BigInteger to a BigInteger power.
1940dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param k number to raise
1941dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param e exponent (must be positive or null)
1942dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return k<sup>e</sup>
1943dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @exception IllegalArgumentException if e is negative
1944dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1945dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static BigInteger pow(final BigInteger k, BigInteger e)
1946dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
1947dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1948dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (e.compareTo(BigInteger.ZERO) < 0) {
1949dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
1950dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
1951dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                k, e);
1952dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1953dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1954dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        BigInteger result = BigInteger.ONE;
1955dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        BigInteger k2p    = k;
1956dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        while (!BigInteger.ZERO.equals(e)) {
1957dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (e.testBit(0)) {
1958dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                result = result.multiply(k2p);
1959dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
1960dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            k2p = k2p.multiply(k2p);
1961dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            e = e.shiftRight(1);
1962dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1963dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1964dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
1965dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1966dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1967dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1968dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1969dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
1970dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1971dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
1972dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
1973dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>1</sub> distance between the two points
1974dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1975dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double distance1(double[] p1, double[] p2) {
1976dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum = 0;
1977dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < p1.length; i++) {
1978dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum += FastMath.abs(p1[i] - p2[i]);
1979dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
1980dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sum;
1981dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1982dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1983dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1984dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
1985dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
1986dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
1987dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
1988dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>1</sub> distance between the two points
1989dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
1990dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int distance1(int[] p1, int[] p2) {
1991dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      int sum = 0;
1992dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      for (int i = 0; i < p1.length; i++) {
1993dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          sum += FastMath.abs(p1[i] - p2[i]);
1994dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      }
1995dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      return sum;
1996dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
1997dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
1998dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
1999dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>2</sub> (Euclidean) distance between two points.
2000dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2001dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
2002dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
2003dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>2</sub> distance between the two points
2004dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2005dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double distance(double[] p1, double[] p2) {
2006dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum = 0;
2007dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < p1.length; i++) {
2008dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double dp = p1[i] - p2[i];
2009dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum += dp * dp;
2010dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2011dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.sqrt(sum);
2012dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2013dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2014dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2015dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>2</sub> (Euclidean) distance between two points.
2016dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2017dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
2018dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
2019dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>2</sub> distance between the two points
2020dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2021dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double distance(int[] p1, int[] p2) {
2022dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      double sum = 0;
2023dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      for (int i = 0; i < p1.length; i++) {
2024dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          final double dp = p1[i] - p2[i];
2025dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          sum += dp * dp;
2026dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      }
2027dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      return FastMath.sqrt(sum);
2028dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2029dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2030dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2031dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>&infin;</sub> (max of abs) distance between two points.
2032dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2033dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
2034dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
2035dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>&infin;</sub> distance between the two points
2036dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2037dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double distanceInf(double[] p1, double[] p2) {
2038dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double max = 0;
2039dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < p1.length; i++) {
2040dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            max = FastMath.max(max, FastMath.abs(p1[i] - p2[i]));
2041dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2042dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return max;
2043dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2044dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2045dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2046dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Calculates the L<sub>&infin;</sub> (max of abs) distance between two points.
2047dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2048dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p1 the first point
2049dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p2 the second point
2050dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the L<sub>&infin;</sub> distance between the two points
2051dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2052dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static int distanceInf(int[] p1, int[] p2) {
2053dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int max = 0;
2054dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < p1.length; i++) {
2055dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            max = FastMath.max(max, FastMath.abs(p1[i] - p2[i]));
2056dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2057dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return max;
2058dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2059dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2060dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2061dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Specification of ordering direction.
2062dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2063dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static enum OrderDirection {
2064dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /** Constant for increasing direction. */
2065dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        INCREASING,
2066dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        /** Constant for decreasing direction. */
2067dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        DECREASING
2068dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2069dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2070dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2071dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Checks that the given array is sorted.
2072dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2073dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param val Values.
2074dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dir Ordering direction.
2075dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param strict Whether the order should be strict.
2076dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NonMonotonousSequenceException if the array is not sorted.
2077dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
2078dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2079dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void checkOrder(double[] val, OrderDirection dir, boolean strict) {
2080dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double previous = val[0];
2081dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        boolean ok = true;
2082dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2083dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int max = val.length;
2084dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i < max; i++) {
2085dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            switch (dir) {
2086dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            case INCREASING:
2087dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (strict) {
2088dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (val[i] <= previous) {
2089dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ok = false;
2090dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
2091dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
2092dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (val[i] < previous) {
2093dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ok = false;
2094dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
2095dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
2096dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                break;
2097dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            case DECREASING:
2098dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (strict) {
2099dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (val[i] >= previous) {
2100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ok = false;
2101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
2102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
2103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (val[i] > previous) {
2104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        ok = false;
2105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
2106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
2107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                break;
2108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            default:
2109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                // Should never happen.
2110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw new IllegalArgumentException();
2111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
2112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (!ok) {
2114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                throw new NonMonotonousSequenceException(val[i], previous, i, dir, strict);
2115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
2116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            previous = val[i];
2117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Checks that the given array is sorted in strictly increasing order.
2122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param val Values.
2124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NonMonotonousSequenceException if the array is not sorted.
2125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
2126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void checkOrder(double[] val) {
2128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        checkOrder(val, OrderDirection.INCREASING, true);
2129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Checks that the given array is sorted.
2133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param val Values
2135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dir Order direction (-1 for decreasing, 1 for increasing)
2136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param strict Whether the order should be strict
2137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NonMonotonousSequenceException if the array is not sorted.
2138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @deprecated as of 2.2 (please use the new {@link #checkOrder(double[],OrderDirection,boolean)
2139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * checkOrder} method). To be removed in 3.0.
2140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Deprecated
2142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void checkOrder(double[] val, int dir, boolean strict) {
2143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (dir > 0) {
2144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            checkOrder(val, OrderDirection.INCREASING, strict);
2145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
2146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            checkOrder(val, OrderDirection.DECREASING, strict);
2147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
2151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the Cartesian norm (2-norm), handling both overflow and underflow.
2152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Translation of the minpack enorm subroutine.
2153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The redistribution policy for MINPACK is available <a
2155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * href="http://www.netlib.org/minpack/disclaimer">here</a>, for convenience, it
2156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is reproduced below.</p>
2157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <table border="0" width="80%" cellpadding="10" align="center" bgcolor="#E0E0E0">
2159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <tr><td>
2160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *    Minpack Copyright Notice (1999) University of Chicago.
2161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *    All rights reserved
2162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </td></tr>
2163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <tr><td>
2164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Redistribution and use in source and binary forms, with or without
2165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * modification, are permitted provided that the following conditions
2166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * are met:
2167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ol>
2168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  <li>Redistributions of source code must retain the above copyright
2169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *      notice, this list of conditions and the following disclaimer.</li>
2170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Redistributions in binary form must reproduce the above
2171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     copyright notice, this list of conditions and the following
2172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     disclaimer in the documentation and/or other materials provided
2173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     with the distribution.</li>
2174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The end-user documentation included with the redistribution, if any,
2175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     must include the following acknowledgment:
2176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     <code>This product includes software developed by the University of
2177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *           Chicago, as Operator of Argonne National Laboratory.</code>
2178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     Alternately, this acknowledgment may appear in the software itself,
2179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     if and wherever such third-party acknowledgments normally appear.</li>
2180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li><strong>WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
2181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
2182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
2183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
2184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
2185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
2186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
2187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
2188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
2189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
2190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
2191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
2192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     BE CORRECTED.</strong></li>
2193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li><strong>LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
2194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
2195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
2196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
2197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
2198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
2199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
2200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
2201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
2202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *     POSSIBILITY OF SUCH LOSS OR DAMAGES.</strong></li>
2203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ol></td></tr>
2204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </table>
2205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
2206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param v vector of doubles
2207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the 2-norm of the vector
2208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
2209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
2210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double safeNorm(double[] v) {
2211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double rdwarf = 3.834e-20;
2212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double rgiant = 1.304e+19;
2213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double s1=0.0;
2214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double s2=0.0;
2215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double s3=0.0;
2216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double x1max = 0.0;
2217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double x3max = 0.0;
2218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double floatn = (double)v.length;
2219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double agiant = rgiant/floatn;
2220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    for (int i=0;i<v.length;i++) {
2221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double xabs = Math.abs(v[i]);
2222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (xabs<rdwarf || xabs>agiant) {
2223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (xabs>rdwarf) {
2224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (xabs>x1max) {
2225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    double r=x1max/xabs;
2226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    s1=1.0+s1*r*r;
2227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    x1max=xabs;
2228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
2229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    double r=xabs/x1max;
2230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    s1+=r*r;
2231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
2232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
2233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (xabs>x3max) {
2234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 double r=x3max/xabs;
2235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 s3=1.0+s3*r*r;
2236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                 x3max=xabs;
2237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
2238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    if (xabs!=0.0) {
2239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        double r=xabs/x3max;
2240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        s3+=r*r;
2241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    }
2242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
2243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
2244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
2245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond         s2+=xabs*xabs;
2246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double norm;
2249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    if (s1!=0.0) {
2250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        norm = x1max*Math.sqrt(s1+(s2/x1max)/x1max);
2251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    } else {
2252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (s2==0.0) {
2253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            norm = x3max*Math.sqrt(s3);
2254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
2255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if (s2>=x3max) {
2256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                norm = Math.sqrt(s2*(1.0+(x3max/s2)*(x3max*s3)));
2257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            } else {
2258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                norm = Math.sqrt(x3max*((s2/x3max)+(x3max*s3)));
2259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
2260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
2261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
2262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return norm;
2263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
2264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
2265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
2266