diff --git a/src/main/java/org/apache/commons/lang3/math/Fraction.java b/src/main/java/org/apache/commons/lang3/math/Fraction.java index e90e340ac55..97cef5f3c7c 100644 --- a/src/main/java/org/apache/commons/lang3/math/Fraction.java +++ b/src/main/java/org/apache/commons/lang3/math/Fraction.java @@ -534,6 +534,49 @@ public Fraction abs() { * {@code Integer.MAX_VALUE} */ public Fraction pow(final int power) { + if (power == 1) { + return this; + } + if (power == 0) { + return ONE; + } + if (power == -1) { + return this.invert(); + } + final Fraction base = this.reduce(); + if (base.denominator == 0) { + if (power > 0) { + throw new ArithmeticException("The denominator must not be zero"); + } else { + return ZERO; + } + } + if (base.numerator == 0) { + if (power < 0) { + throw new ArithmeticException("The denominator must not be zero"); + } else { + return ZERO; + } + } + if (power > 0) { + return new Fraction( + pow(base.numerator, power), + pow(base.denominator, power) + ); + } + if (power == Integer.MIN_VALUE) { + return new Fraction( + pow(this.denominator, Integer.MAX_VALUE) * this.denominator, + pow(this.numerator, Integer.MAX_VALUE) * this.numerator + ); + } + return new Fraction( + pow(base.denominator, -power), + pow(base.numerator, -power) + ); + } + + public Fraction powOld(final int power) { if (power == 1) { return this; } else if (power == 0) { @@ -552,6 +595,36 @@ public Fraction pow(final int power) { } } + /** + * Raise an int to an int power. + * Notice that this function is copied and modified directly from class ArithmeticUtils in commons-numbers-core. + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). + * @return \( k^e \) + * @throws ArithmeticException if the result would overflow. + */ + private static int pow(final int k, + final int e) { + int exp = e; + int result = 1; + int k2p = k; + while (true) { + if ((exp & 0x1) != 0) { + result = Math.multiplyExact(result, k2p); + } + + exp >>= 1; + if (exp == 0) { + break; + } + + k2p = Math.multiplyExact(k2p, k2p); + } + + return result; + } + /** *
Gets the greatest common divisor of the absolute value of * two numbers, using the "binary gcd" method which avoids diff --git a/src/test/java/org/apache/commons/lang3/math/FractionPowPerformanceTest.java b/src/test/java/org/apache/commons/lang3/math/FractionPowPerformanceTest.java new file mode 100644 index 00000000000..97b77a03129 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/math/FractionPowPerformanceTest.java @@ -0,0 +1,83 @@ +/* + * 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.lang3.math; + +import java.util.Random; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Thread) +@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1, jvmArgs = {"-server", "-Xms2048M", "-Xms2048M"}) +public class FractionPowPerformanceTest { + private Random random = new Random(0); + private int[] a = buildInts(1000, 8); + private int[] b = buildInts(1000, 8); + private int[] c = buildInts(1000, 8); + + private int[] buildInts(int length, int bound) { + int[] res = new int[length]; + for (int i = 0; i < length; i++) { + res[i] = random.nextInt(bound); + } + return res; + } + + @Benchmark + public Fraction[] testNew() { + final int length = a.length; + Fraction[] res = new Fraction[length]; + for (int i = 0; i < length; i++) { + res[i] = Fraction.getFraction(a[i], b[i] + 1).pow(c[i]); + } + return res; + } + + @Benchmark + public Fraction[] testOld() { + final int length = a.length; + Fraction[] res = new Fraction[length]; + for (int i = 0; i < length; i++) { + res[i] = Fraction.getFraction(a[i], b[i] + 1).powOld(c[i]); + } + return res; + } + + @Test + public void testEquals() { + final int length = a.length; + for (int i = 0; i < length; i++) { + Assertions.assertEquals( + Fraction.getFraction(a[i], b[i] + 1).powOld(c[i]), + Fraction.getFraction(a[i], b[i] + 1).pow(c[i]) + ); + } + } +}