MCPcopy Create free account
hub / github.com/apache/commons-lang / pow

Method pow

src/main/java/org/apache/commons/lang3/math/Fraction.java:804–822  ·  view source on GitHub ↗

Gets a fraction that is raised to the passed in power. The returned fraction is in reduced form. @param power the power to raise the fraction to @return this if the power is one, #ONE if the power is zero (even if the fraction equals ZERO) or a new fraction instance raised

(final int power)

Source from the content-addressed store, hash-verified

802 * @throws ArithmeticException if the resulting numerator or denominator exceeds {@code Integer.MAX_VALUE}
803 */
804 public Fraction pow(final int power) {
805 if (power == 1) {
806 return this;
807 }
808 if (power == 0) {
809 return ONE;
810 }
811 if (power < 0) {
812 if (power == Integer.MIN_VALUE) { // MIN_VALUE can't be negated.
813 return invert().pow(2).pow(-(power / 2));
814 }
815 return invert().pow(-power);
816 }
817 final Fraction f = multiplyBy(this);
818 if (power % 2 == 0) { // if even...
819 return f.pow(power / 2);
820 }
821 return f.pow(power / 2).multiplyBy(this);
822 }
823
824 /**
825 * Validates the cached hashCode after deserialization. Throws a {@link InvalidObjectException} when the stored hashCode does not match the canonical hash

Callers 1

testPowMethod · 0.95

Calls 2

invertMethod · 0.95
multiplyByMethod · 0.95

Tested by 1

testPowMethod · 0.76