View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.numbers.examples.jmh.core;
18  
19  import org.apache.commons.rng.UniformRandomProvider;
20  
21  /** Class containing utility methods for working with doubles.
22   */
23  final class DoubleUtils {
24  
25      /** No instantiation. */
26      private DoubleUtils() {}
27  
28      /** Create a random double value with exponent in the range {@code [minExp, maxExp]}.
29       * @param minExp minimum exponent; must be less than {@code maxExp}
30       * @param maxExp maximum exponent; must be greater than {@code minExp}
31       * @param rng random number generator
32       * @return random double
33       */
34      static double random(final int minExp, final int maxExp, final UniformRandomProvider rng) {
35          // Create random doubles using random bits in the sign bit and the mantissa.
36          final long mask = ((1L << 52) - 1) | 1L << 63;
37          final long bits = rng.nextLong() & mask;
38          // The exponent must be unsigned so + 1023 to the signed exponent
39          final long exp = rng.nextInt(maxExp - minExp + 1) + minExp + 1023;
40          return Double.longBitsToDouble(bits | (exp << 52));
41      }
42  
43      /** Create an array with the given length containing random doubles with exponents in the range
44       * {@code [minExp, maxExp]}.
45       * @param len array length
46       * @param minExp minimum exponent; must be less than {@code maxExp}
47       * @param maxExp maximum exponent; must be greater than {@code minExp}
48       * @param rng random number generator
49       * @return array of random doubles
50       */
51      static double[] randomArray(final int len, final int minExp, final int maxExp,
52              final UniformRandomProvider rng) {
53          final double[] arr = new double[len];
54          for (int i = 0; i < arr.length; ++i) {
55              arr[i] = random(minExp, maxExp, rng);
56          }
57          return arr;
58      }
59  
60      /** Return a new array containing each element of {@code v} multiplied by {@code s}.
61       * @param v input array
62       * @param s scale factor
63       * @return new array containing each element of {@code v} multiplied by {@code s}
64       */
65      static double[] scalarMultiply(final double[] v, final double s) {
66          final double[] sv = new double[v.length];
67          for (int i = 0; i < v.length; ++i) {
68              sv[i] = s * v[i];
69          }
70          return sv;
71      }
72  }