ArithmeticUtils.java

  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.core;

  18. import java.math.BigInteger;

  19. /**
  20.  * Some useful, arithmetics related, additions to the built-in functions in
  21.  * {@link Math}.
  22.  *
  23.  */
  24. public final class ArithmeticUtils {

  25.     /** Negative exponent exception message part 1. */
  26.     private static final String NEGATIVE_EXPONENT_1 = "negative exponent ({";
  27.     /** Negative exponent exception message part 2. */
  28.     private static final String NEGATIVE_EXPONENT_2 = "})";

  29.     /** Private constructor. */
  30.     private ArithmeticUtils() {
  31.         // intentionally empty.
  32.     }

  33.     /**
  34.      * Computes the greatest common divisor of the absolute value of two
  35.      * numbers, using a modified version of the "binary gcd" method.
  36.      * See Knuth 4.5.2 algorithm B.
  37.      * The algorithm is due to Josef Stein (1961).
  38.      * <br>
  39.      * Special cases:
  40.      * <ul>
  41.      *  <li>The invocations
  42.      *   {@code gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)},
  43.      *   {@code gcd(Integer.MIN_VALUE, 0)} and
  44.      *   {@code gcd(0, Integer.MIN_VALUE)} throw an
  45.      *   {@code ArithmeticException}, because the result would be 2^31, which
  46.      *   is too large for an int value.</li>
  47.      *  <li>The result of {@code gcd(x, x)}, {@code gcd(0, x)} and
  48.      *   {@code gcd(x, 0)} is the absolute value of {@code x}, except
  49.      *   for the special cases above.</li>
  50.      *  <li>The invocation {@code gcd(0, 0)} is the only one which returns
  51.      *   {@code 0}.</li>
  52.      * </ul>
  53.      *
  54.      * <p>Two numbers are relatively prime, or coprime, if their gcd is 1.</p>
  55.      *
  56.      * @param p Number.
  57.      * @param q Number.
  58.      * @return the greatest common divisor (never negative).
  59.      * @throws ArithmeticException if the result cannot be represented as
  60.      * a non-negative {@code int} value.
  61.      */
  62.     public static int gcd(int p, int q) {
  63.         // Perform the gcd algorithm on negative numbers, so that -2^31 does not
  64.         // need to be handled separately
  65.         int a = p > 0 ? -p : p;
  66.         int b = q > 0 ? -q : q;

  67.         int negatedGcd;
  68.         if (a == 0) {
  69.             negatedGcd = b;
  70.         } else if (b == 0) {
  71.             negatedGcd = a;
  72.         } else {
  73.             // Make "a" and "b" odd, keeping track of common power of 2.
  74.             final int aTwos = Integer.numberOfTrailingZeros(a);
  75.             final int bTwos = Integer.numberOfTrailingZeros(b);
  76.             a >>= aTwos;
  77.             b >>= bTwos;
  78.             final int shift = Math.min(aTwos, bTwos);

  79.             // "a" and "b" are negative and odd.
  80.             // If a < b then "gdc(a, b)" is equal to "gcd(a - b, b)".
  81.             // If a > b then "gcd(a, b)" is equal to "gcd(b - a, a)".
  82.             // Hence, in the successive iterations:
  83.             //  "a" becomes the negative absolute difference of the current values,
  84.             //  "b" becomes that value of the two that is closer to zero.
  85.             while (a != b) {
  86.                 final int delta = a - b;
  87.                 b = Math.max(a, b);
  88.                 a = delta > 0 ? -delta : delta;

  89.                 // Remove any power of 2 in "a" ("b" is guaranteed to be odd).
  90.                 a >>= Integer.numberOfTrailingZeros(a);
  91.             }

  92.             // Recover the common power of 2.
  93.             negatedGcd = a << shift;
  94.         }
  95.         if (negatedGcd == Integer.MIN_VALUE) {
  96.             throw new NumbersArithmeticException("overflow: gcd(%d, %d) is 2^31",
  97.                                                  p, q);
  98.         }
  99.         return -negatedGcd;
  100.     }

  101.     /**
  102.      * <p>
  103.      * Gets the greatest common divisor of the absolute value of two numbers,
  104.      * using the "binary gcd" method which avoids division and modulo
  105.      * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef
  106.      * Stein (1961).
  107.      * </p>
  108.      * Special cases:
  109.      * <ul>
  110.      * <li>The invocations
  111.      * {@code gcd(Long.MIN_VALUE, Long.MIN_VALUE)},
  112.      * {@code gcd(Long.MIN_VALUE, 0L)} and
  113.      * {@code gcd(0L, Long.MIN_VALUE)} throw an
  114.      * {@code ArithmeticException}, because the result would be 2^63, which
  115.      * is too large for a long value.</li>
  116.      * <li>The result of {@code gcd(x, x)}, {@code gcd(0L, x)} and
  117.      * {@code gcd(x, 0L)} is the absolute value of {@code x}, except
  118.      * for the special cases above.
  119.      * <li>The invocation {@code gcd(0L, 0L)} is the only one which returns
  120.      * {@code 0L}.</li>
  121.      * </ul>
  122.      *
  123.      * <p>Two numbers are relatively prime, or coprime, if their gcd is 1.</p>
  124.      *
  125.      * @param p Number.
  126.      * @param q Number.
  127.      * @return the greatest common divisor, never negative.
  128.      * @throws ArithmeticException if the result cannot be represented as
  129.      * a non-negative {@code long} value.
  130.      */
  131.     public static long gcd(long p, long q) {
  132.         // Perform the gcd algorithm on negative numbers, so that -2^63 does not
  133.         // need to be handled separately
  134.         long a = p > 0 ? -p : p;
  135.         long b = q > 0 ? -q : q;

  136.         long negatedGcd;
  137.         if (a == 0) {
  138.             negatedGcd = b;
  139.         } else if (b == 0) {
  140.             negatedGcd = a;
  141.         } else {
  142.             // Make "a" and "b" odd, keeping track of common power of 2.
  143.             final int aTwos = Long.numberOfTrailingZeros(a);
  144.             final int bTwos = Long.numberOfTrailingZeros(b);
  145.             a >>= aTwos;
  146.             b >>= bTwos;
  147.             final int shift = Math.min(aTwos, bTwos);

  148.             // "a" and "b" are negative and odd.
  149.             // If a < b then "gdc(a, b)" is equal to "gcd(a - b, b)".
  150.             // If a > b then "gcd(a, b)" is equal to "gcd(b - a, a)".
  151.             // Hence, in the successive iterations:
  152.             //  "a" becomes the negative absolute difference of the current values,
  153.             //  "b" becomes that value of the two that is closer to zero.
  154.             while (true) {
  155.                 final long delta = a - b;

  156.                 if (delta == 0) {
  157.                     // This way of terminating the loop is intentionally different from the int gcd implementation.
  158.                     // Benchmarking shows that testing for long inequality (a != b) is slow compared to
  159.                     // testing the delta against zero. The same change on the int gcd reduces performance there,
  160.                     // hence we have two variants of this loop.
  161.                     break;
  162.                 }

  163.                 b = Math.max(a, b);
  164.                 a = delta > 0 ? -delta : delta;

  165.                 // Remove any power of 2 in "a" ("b" is guaranteed to be odd).
  166.                 a >>= Long.numberOfTrailingZeros(a);
  167.             }

  168.             // Recover the common power of 2.
  169.             negatedGcd = a << shift;
  170.         }
  171.         if (negatedGcd == Long.MIN_VALUE) {
  172.             throw new NumbersArithmeticException("overflow: gcd(%d, %d) is 2^63",
  173.                     p, q);
  174.         }
  175.         return -negatedGcd;
  176.     }

  177.     /**
  178.      * <p>
  179.      * Returns the least common multiple of the absolute value of two numbers,
  180.      * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}.
  181.      * </p>
  182.      * Special cases:
  183.      * <ul>
  184.      * <li>The invocations {@code lcm(Integer.MIN_VALUE, n)} and
  185.      * {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a
  186.      * power of 2, throw an {@code ArithmeticException}, because the result
  187.      * would be 2^31, which is too large for an int value.</li>
  188.      * <li>The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is
  189.      * {@code 0} for any {@code x}.
  190.      * </ul>
  191.      *
  192.      * @param a Number.
  193.      * @param b Number.
  194.      * @return the least common multiple, never negative.
  195.      * @throws ArithmeticException if the result cannot be represented as
  196.      * a non-negative {@code int} value.
  197.      */
  198.     public static int lcm(int a, int b) {
  199.         if (a == 0 || b == 0) {
  200.             return 0;
  201.         }
  202.         final int lcm = Math.abs(Math.multiplyExact(a / gcd(a, b), b));
  203.         if (lcm == Integer.MIN_VALUE) {
  204.             throw new NumbersArithmeticException("overflow: lcm(%d, %d) is 2^31",
  205.                                                  a, b);
  206.         }
  207.         return lcm;
  208.     }

  209.     /**
  210.      * <p>
  211.      * Returns the least common multiple of the absolute value of two numbers,
  212.      * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}.
  213.      * </p>
  214.      * Special cases:
  215.      * <ul>
  216.      * <li>The invocations {@code lcm(Long.MIN_VALUE, n)} and
  217.      * {@code lcm(n, Long.MIN_VALUE)}, where {@code abs(n)} is a
  218.      * power of 2, throw an {@code ArithmeticException}, because the result
  219.      * would be 2^63, which is too large for an int value.</li>
  220.      * <li>The result of {@code lcm(0L, x)} and {@code lcm(x, 0L)} is
  221.      * {@code 0L} for any {@code x}.
  222.      * </ul>
  223.      *
  224.      * @param a Number.
  225.      * @param b Number.
  226.      * @return the least common multiple, never negative.
  227.      * @throws ArithmeticException if the result cannot be represented
  228.      * as a non-negative {@code long} value.
  229.      */
  230.     public static long lcm(long a, long b) {
  231.         if (a == 0 || b == 0) {
  232.             return 0;
  233.         }
  234.         final long lcm = Math.abs(Math.multiplyExact(a / gcd(a, b), b));
  235.         if (lcm == Long.MIN_VALUE) {
  236.             throw new NumbersArithmeticException("overflow: lcm(%d, %d) is 2^63",
  237.                                                  a, b);
  238.         }
  239.         return lcm;
  240.     }

  241.     /**
  242.      * Raise an int to an int power.
  243.      *
  244.      * <p>Special cases:</p>
  245.      * <ul>
  246.      *   <li>{@code k^0} returns {@code 1} (including {@code k=0})
  247.      *   <li>{@code k^1} returns {@code k} (including {@code k=0})
  248.      *   <li>{@code 0^0} returns {@code 1}
  249.      *   <li>{@code 0^e} returns {@code 0}
  250.      *   <li>{@code 1^e} returns {@code 1}
  251.      *   <li>{@code (-1)^e} returns {@code -1 or 1} if {@code e} is odd or even
  252.      * </ul>
  253.      *
  254.      * @param k Number to raise.
  255.      * @param e Exponent (must be positive or zero).
  256.      * @return \( k^e \)
  257.      * @throws IllegalArgumentException if {@code e < 0}.
  258.      * @throws ArithmeticException if the result would overflow.
  259.      */
  260.     public static int pow(final int k,
  261.                           final int e) {
  262.         if (e < 0) {
  263.             throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
  264.         }

  265.         if (k == 0) {
  266.             return e == 0 ? 1 : 0;
  267.         }

  268.         if (k == 1) {
  269.             return 1;
  270.         }

  271.         if (k == -1) {
  272.             return (e & 1) == 0 ? 1 : -1;
  273.         }

  274.         if (e >= 31) {
  275.             throw new ArithmeticException("integer overflow");
  276.         }

  277.         int exp = e;
  278.         int result = 1;
  279.         int k2p    = k;
  280.         while (true) {
  281.             if ((exp & 0x1) != 0) {
  282.                 result = Math.multiplyExact(result, k2p);
  283.             }

  284.             exp >>= 1;
  285.             if (exp == 0) {
  286.                 break;
  287.             }

  288.             k2p = Math.multiplyExact(k2p, k2p);
  289.         }

  290.         return result;
  291.     }

  292.     /**
  293.      * Raise a long to an int power.
  294.      *
  295.      * <p>Special cases:</p>
  296.      * <ul>
  297.      *   <li>{@code k^0} returns {@code 1} (including {@code k=0})
  298.      *   <li>{@code k^1} returns {@code k} (including {@code k=0})
  299.      *   <li>{@code 0^0} returns {@code 1}
  300.      *   <li>{@code 0^e} returns {@code 0}
  301.      *   <li>{@code 1^e} returns {@code 1}
  302.      *   <li>{@code (-1)^e} returns {@code -1 or 1} if {@code e} is odd or even
  303.      * </ul>
  304.      *
  305.      * @param k Number to raise.
  306.      * @param e Exponent (must be positive or zero).
  307.      * @return \( k^e \)
  308.      * @throws IllegalArgumentException if {@code e < 0}.
  309.      * @throws ArithmeticException if the result would overflow.
  310.      */
  311.     public static long pow(final long k,
  312.                            final int e) {
  313.         if (e < 0) {
  314.             throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
  315.         }

  316.         if (k == 0L) {
  317.             return e == 0 ? 1L : 0L;
  318.         }

  319.         if (k == 1L) {
  320.             return 1L;
  321.         }

  322.         if (k == -1L) {
  323.             return (e & 1) == 0 ? 1L : -1L;
  324.         }

  325.         if (e >= 63) {
  326.             throw new ArithmeticException("long overflow");
  327.         }

  328.         int exp = e;
  329.         long result = 1;
  330.         long k2p    = k;
  331.         while (true) {
  332.             if ((exp & 0x1) != 0) {
  333.                 result = Math.multiplyExact(result, k2p);
  334.             }

  335.             exp >>= 1;
  336.             if (exp == 0) {
  337.                 break;
  338.             }

  339.             k2p = Math.multiplyExact(k2p, k2p);
  340.         }

  341.         return result;
  342.     }

  343.     /**
  344.      * Raise a BigInteger to an int power.
  345.      *
  346.      * @param k Number to raise.
  347.      * @param e Exponent (must be positive or zero).
  348.      * @return k<sup>e</sup>
  349.      * @throws IllegalArgumentException if {@code e < 0}.
  350.      */
  351.     public static BigInteger pow(final BigInteger k, int e) {
  352.         if (e < 0) {
  353.             throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
  354.         }

  355.         return k.pow(e);
  356.     }

  357.     /**
  358.      * Raise a BigInteger to a long power.
  359.      *
  360.      * @param k Number to raise.
  361.      * @param e Exponent (must be positive or zero).
  362.      * @return k<sup>e</sup>
  363.      * @throws IllegalArgumentException if {@code e < 0}.
  364.      */
  365.     public static BigInteger pow(final BigInteger k, final long e) {
  366.         if (e < 0) {
  367.             throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
  368.         }

  369.         long exp = e;
  370.         BigInteger result = BigInteger.ONE;
  371.         BigInteger k2p    = k;
  372.         while (exp != 0) {
  373.             if ((exp & 0x1) != 0) {
  374.                 result = result.multiply(k2p);
  375.             }
  376.             k2p = k2p.multiply(k2p);
  377.             exp >>= 1;
  378.         }

  379.         return result;

  380.     }

  381.     /**
  382.      * Raise a BigInteger to a BigInteger power.
  383.      *
  384.      * @param k Number to raise.
  385.      * @param e Exponent (must be positive or zero).
  386.      * @return k<sup>e</sup>
  387.      * @throws IllegalArgumentException if {@code e < 0}.
  388.      */
  389.     public static BigInteger pow(final BigInteger k, final BigInteger e) {
  390.         if (e.compareTo(BigInteger.ZERO) < 0) {
  391.             throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
  392.         }

  393.         BigInteger exp = e;
  394.         BigInteger result = BigInteger.ONE;
  395.         BigInteger k2p    = k;
  396.         while (!BigInteger.ZERO.equals(exp)) {
  397.             if (exp.testBit(0)) {
  398.                 result = result.multiply(k2p);
  399.             }
  400.             k2p = k2p.multiply(k2p);
  401.             exp = exp.shiftRight(1);
  402.         }

  403.         return result;
  404.     }

  405.     /**
  406.      * Returns true if the argument is a power of two.
  407.      *
  408.      * @param n the number to test
  409.      * @return true if the argument is a power of two
  410.      */
  411.     public static boolean isPowerOfTwo(long n) {
  412.         return n > 0 && (n & (n - 1)) == 0;
  413.     }

  414.     /**
  415.      * Returns the unsigned remainder from dividing the first argument
  416.      * by the second where each argument and the result is interpreted
  417.      * as an unsigned value.
  418.      *
  419.      * <p>Implementation note
  420.      *
  421.      * <p>In v1.0 this method did not use the {@code long} datatype.
  422.      * Modern 64-bit processors make use of the {@code long} datatype
  423.      * faster than an algorithm using the {@code int} datatype. This method
  424.      * now delegates to {@link Integer#remainderUnsigned(int, int)}
  425.      * which uses {@code long} arithmetic; or from JDK 19 an intrinsic method.
  426.      *
  427.      * @param dividend the value to be divided
  428.      * @param divisor the value doing the dividing
  429.      * @return the unsigned remainder of the first argument divided by
  430.      * the second argument.
  431.      * @see Integer#remainderUnsigned(int, int)
  432.      */
  433.     public static int remainderUnsigned(int dividend, int divisor) {
  434.         return Integer.remainderUnsigned(dividend, divisor);
  435.     }

  436.     /**
  437.      * Returns the unsigned remainder from dividing the first argument
  438.      * by the second where each argument and the result is interpreted
  439.      * as an unsigned value.
  440.      *
  441.      * <p>Implementation note
  442.      *
  443.      * <p>This method does not use the {@code BigInteger} datatype.
  444.      * The JDK implementation of {@link Long#remainderUnsigned(long, long)}
  445.      * uses {@code BigInteger} prior to JDK 17 and this method is 15-25x faster.
  446.      * From JDK 17 onwards the JDK implementation is as fast; or from JDK 19
  447.      * even faster due to use of an intrinsic method.
  448.      *
  449.      * @param dividend the value to be divided
  450.      * @param divisor the value doing the dividing
  451.      * @return the unsigned remainder of the first argument divided by
  452.      * the second argument.
  453.      * @see Long#remainderUnsigned(long, long)
  454.      */
  455.     public static long remainderUnsigned(long dividend, long divisor) {
  456.         // Adapts the divideUnsigned method to compute the remainder.
  457.         if (divisor < 0) {
  458.             // Using unsigned compare:
  459.             // if dividend < divisor: return dividend
  460.             // else: return dividend - divisor

  461.             // Subtracting divisor using masking is more complex in this case
  462.             // and we use a condition
  463.             return dividend >= 0 || dividend < divisor ? dividend : dividend - divisor;

  464.         }
  465.         // From Hacker's Delight 2.0, section 9.3
  466.         final long q = ((dividend >>> 1) / divisor) << 1;
  467.         final long r = dividend - q * divisor;
  468.         // unsigned r: 0 <= r < 2 * divisor
  469.         // if (r < divisor): r
  470.         // else: r - divisor

  471.         // The compare of unsigned r can be done using:
  472.         // return (r + Long.MIN_VALUE) < (divisor | Long.MIN_VALUE) ? r : r - divisor

  473.         // Here we subtract divisor if (r - divisor) is positive, else the result is r.
  474.         // This can be done by flipping the sign bit and
  475.         // creating a mask as -1 or 0 by signed shift.
  476.         return r - (divisor & (~(r - divisor) >> 63));
  477.     }

  478.     /**
  479.      * Returns the unsigned quotient of dividing the first argument by
  480.      * the second where each argument and the result is interpreted as
  481.      * an unsigned value.
  482.      * <p>Note that in two's complement arithmetic, the three other
  483.      * basic arithmetic operations of add, subtract, and multiply are
  484.      * bit-wise identical if the two operands are regarded as both
  485.      * being signed or both being unsigned. Therefore separate {@code
  486.      * addUnsigned}, etc. methods are not provided.</p>
  487.      *
  488.      * <p>Implementation note
  489.      *
  490.      * <p>In v1.0 this method did not use the {@code long} datatype.
  491.      * Modern 64-bit processors make use of the {@code long} datatype
  492.      * faster than an algorithm using the {@code int} datatype. This method
  493.      * now delegates to {@link Integer#divideUnsigned(int, int)}
  494.      * which uses {@code long} arithmetic; or from JDK 19 an intrinsic method.
  495.      *
  496.      * @param dividend the value to be divided
  497.      * @param divisor the value doing the dividing
  498.      * @return the unsigned quotient of the first argument divided by
  499.      * the second argument
  500.      * @see Integer#divideUnsigned(int, int)
  501.      */
  502.     public static int divideUnsigned(int dividend, int divisor) {
  503.         return Integer.divideUnsigned(dividend, divisor);
  504.     }

  505.     /**
  506.      * Returns the unsigned quotient of dividing the first argument by
  507.      * the second where each argument and the result is interpreted as
  508.      * an unsigned value.
  509.      * <p>Note that in two's complement arithmetic, the three other
  510.      * basic arithmetic operations of add, subtract, and multiply are
  511.      * bit-wise identical if the two operands are regarded as both
  512.      * being signed or both being unsigned. Therefore separate {@code
  513.      * addUnsigned}, etc. methods are not provided.</p>
  514.      *
  515.      * <p>Implementation note
  516.      *
  517.      * <p>This method does not use the {@code BigInteger} datatype.
  518.      * The JDK implementation of {@link Long#divideUnsigned(long, long)}
  519.      * uses {@code BigInteger} prior to JDK 17 and this method is 15-25x faster.
  520.      * From JDK 17 onwards the JDK implementation is as fast; or from JDK 19
  521.      * even faster due to use of an intrinsic method.
  522.      *
  523.      * @param dividend the value to be divided
  524.      * @param divisor the value doing the dividing
  525.      * @return the unsigned quotient of the first argument divided by
  526.      * the second argument.
  527.      * @see Long#divideUnsigned(long, long)
  528.      */
  529.     public static long divideUnsigned(long dividend, long divisor) {
  530.         // The implementation is a Java port of algorithm described in the book
  531.         // "Hacker's Delight 2.0" (section 9.3 "Unsigned short division from signed division").
  532.         // Adapts 6-line predicate expressions program with (u >=) an unsigned compare
  533.         // using the provided branchless variants.
  534.         if (divisor < 0) {
  535.             // line 1 branchless:
  536.             // q <- (dividend (u >=) divisor)
  537.             return (dividend & ~(dividend - divisor)) >>> 63;
  538.         }
  539.         final long q = ((dividend >>> 1) / divisor) << 1;
  540.         final long r = dividend - q * divisor;
  541.         // line 5 branchless:
  542.         // q <- q + (r (u >=) divisor)
  543.         return q + ((r | ~(r - divisor)) >>> 63);
  544.     }

  545.     /**
  546.      * Exception.
  547.      */
  548.     private static class NumbersArithmeticException extends ArithmeticException {
  549.         /** Serializable version Id. */
  550.         private static final long serialVersionUID = 20180130L;

  551.         /**
  552.          * Create an exception where the message is constructed by applying
  553.          * {@link String#format(String, Object...)}.
  554.          *
  555.          * @param message Exception message format string
  556.          * @param args Arguments for formatting the message
  557.          */
  558.         NumbersArithmeticException(String message, Object... args) {
  559.             super(String.format(message, args));
  560.         }
  561.     }
  562. }