aherbert commented on a change in pull request #92: URL: https://github.com/apache/commons-numbers/pull/92#discussion_r646514277
########## File path: commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/DoubleTestUtils.java ########## @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.numbers.arrays; + +import org.apache.commons.rng.UniformRandomProvider; + +/** Class providing test utilities related to doubles. + */ +final class DoubleTestUtils { + + /** Utility class; no instantiation. */ + private DoubleTestUtils() {} + + /** Compute the difference in ULP between two arguments of the same sign. + * @param a first argument + * @param b second argument + * @return ULP difference between the arguments + */ + public static int computeUlpDifference(final double a, final double b) { + return (int) (Double.doubleToLongBits(a) - Double.doubleToLongBits(b)); + } + + /** Construct an array of length {@code len} containing random double values with exponents between + * {@code minExp} and {@code maxExp}. + * @param len vector length + * @param minExp minimum element exponent value + * @param maxExp maximum element exponent value + * @param rng random number generator + * @return random vector array + */ + public static double[] randomArray(final int len, final int minExp, final int maxExp, + final UniformRandomProvider rng) { + final double[] v = new double[len]; + for (int i = 0; i < v.length; ++i) { + v[i] = randomDouble(minExp, maxExp, rng); + } + return v; + } + + /** Construct a random double with an exponent in the range {@code [minExp, maxExp]}. + * @param minExp minimum exponent + * @param maxExp maximum exponent + * @param rng random number generator + * @return random double value with an exponent in the specified range + */ + public static double randomDouble(final int minExp, final int maxExp, final UniformRandomProvider rng) { + // Create random doubles using random bits in the sign bit and the mantissa. + final long mask = ((1L << 52) - 1) | 1L << 63; + final long bits = rng.nextLong() & mask; + // The exponent must be unsigned so + 1023 to the signed exponent + final int expRange = Math.abs(maxExp - minExp) + 1; Review comment: Same again. No need for abs. ########## File path: commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/arrays/DoubleUtils.java ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.numbers.examples.jmh.arrays; + +import org.apache.commons.rng.UniformRandomProvider; + +/** Class containing utility methods for working with doubles. + */ +final class DoubleUtils { + + /** No instantiation. */ + private DoubleUtils() {} + + /** Create a random double value with exponent in the range {@code [minExp, maxExp]}. + * @param minExp minimum exponent value + * @param maxExp maximum exponent value + * @param rng random number generator + * @return random double + */ + static double random(final int minExp, final int maxExp, final UniformRandomProvider rng) { + // Create random doubles using random bits in the sign bit and the mantissa. + final long mask = ((1L << 52) - 1) | 1L << 63; + final long bits = rng.nextLong() & mask; + // The exponent must be unsigned so + 1023 to the signed exponent + final long exp = rng.nextInt(Math.abs(maxExp - minExp) + 1) + minExp + 1023; Review comment: Fixed the range issue but just noticed you use an absolute for the range but then add minExp. This assumes min is smaller. But if min is bigger you should subtract the random int from min, or add it to max. Do you require to support min>max? This is simplest with a conditional: ```java if (minExp > maxExp) { exp = rng.nextInt(minExp - maxExp + 1) + maxExp + 1023; } else { exp = rng.nextInt(maxExp - minExp + 1) + minExp + 1023; } ``` Note: Math.abs(int) does a conditional anyway so you are just making it more explicit (although in JDK 11 it is a [hotspot intrinsic](https://chriswhocodes.com/hotspot_intrinsics_openjdk11.html)). If not then you can drop the absolute and just document it to require min<max. It is only for testing anyway. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
