JdkMath.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.math4.core.jdkmath;

  18. import java.util.function.IntUnaryOperator;
  19. import java.util.function.IntBinaryOperator;
  20. import java.util.function.LongUnaryOperator;
  21. import java.util.function.LongBinaryOperator;
  22. import java.util.function.DoubleUnaryOperator;
  23. import java.util.function.DoubleBinaryOperator;
  24. import java.util.function.DoubleToLongFunction;
  25. import java.util.function.DoubleToIntFunction;
  26. import java.util.function.LongToIntFunction;
  27. import java.util.function.DoubleSupplier;

  28. /**
  29.  * Wrapper for alternative implementations of {@link Math} functions.
  30.  * For example, a call to {@code Math.sin(x)} can be replaced by a call
  31.  * to {@code JdkMath.sin(x)}.
  32.  *
  33.  * <p>
  34.  * This class is a "drop-in" replacement for both Math and StrictMath,
  35.  * up to the <em>minimal</em> JDK version required by this library
  36.  * (meaning that, although the library can be used on <em>more</em>
  37.  * recent JVMs, the {@code JdkMath} class may be missing the methods
  38.  * that were absent in older JDKs).
  39.  *
  40.  * <p>
  41.  * Based on the value, at class initialization, of the system property
  42.  * {@code org.apache.commons.math.jdkmath}, this class redirects to a
  43.  * specific implementation:
  44.  * <ul>
  45.  *  <li>{@code CM}: {@link AccurateMath}</li>
  46.  *  <li>{@code JDK}: {@link Math}</li>
  47.  * </ul>
  48.  * When the property is undefined, {@code CM} is the default value.
  49.  */
  50. public final class JdkMath {
  51.     /** Constant. */
  52.     public static final double PI;
  53.     /** Constant. */
  54.     public static final double E;
  55.     /** Property identifier.. */
  56.     private static final String PROPERTY_KEY = "org.apache.commons.math.jdkmath";
  57.     /** abs(x). */
  58.     private static final IntUnaryOperator ABS_INT;
  59.     /** abs(x). */
  60.     private static final LongUnaryOperator ABS_LONG;
  61.     /** abs(x). */
  62.     private static final FloatUnaryOperator ABS_FLOAT;
  63.     /** abs(x). */
  64.     private static final DoubleUnaryOperator ABS_DOUBLE;
  65.     /** acos(x). */
  66.     private static final DoubleUnaryOperator ACOS;
  67.     /** acosh(x). */
  68.     private static final DoubleUnaryOperator ACOSH;
  69.     /** addExact(x, y). */
  70.     private static final IntBinaryOperator ADDEXACT_INT;
  71.     /** addExact(x, y). */
  72.     private static final LongBinaryOperator ADDEXACT_LONG;
  73.     /** asin(x). */
  74.     private static final DoubleUnaryOperator ASIN;
  75.     /** asinh(x). */
  76.     private static final DoubleUnaryOperator ASINH;
  77.     /** atan(x). */
  78.     private static final DoubleUnaryOperator ATAN;
  79.     /** atan2(x, y). */
  80.     private static final DoubleBinaryOperator ATAN2;
  81.     /** atanh(x). */
  82.     private static final DoubleUnaryOperator ATANH;
  83.     /** cbrt(x). */
  84.     private static final DoubleUnaryOperator CBRT;
  85.     /** ceil(x). */
  86.     private static final DoubleUnaryOperator CEIL;
  87.     /** copySign(x, y). */
  88.     private static final FloatBinaryOperator COPYSIGN_FLOAT;
  89.     /** copySign(x, y). */
  90.     private static final DoubleBinaryOperator COPYSIGN_DOUBLE;
  91.     /** cos(x). */
  92.     private static final DoubleUnaryOperator COS;
  93.     /** cosh(x). */
  94.     private static final DoubleUnaryOperator COSH;
  95.     /** decrementExact(x). */
  96.     private static final IntUnaryOperator DECREMENTEXACT_INT;
  97.     /** decrementExact(x). */
  98.     private static final LongUnaryOperator DECREMENTEXACT_LONG;
  99.     /** exp(x). */
  100.     private static final DoubleUnaryOperator EXP;
  101.     /** expm1(x). */
  102.     private static final DoubleUnaryOperator EXPM1;
  103.     /** floor(x). */
  104.     private static final DoubleUnaryOperator FLOOR;
  105.     /** floorDiv(x, y). */
  106.     private static final IntBinaryOperator FLOORDIV_INT;
  107.     /** floorDiv(x, y). */
  108.     private static final LongBinaryOperator FLOORDIV_LONG;
  109.     /** floorMod(x, y). */
  110.     private static final IntBinaryOperator FLOORMOD_INT;
  111.     /** floorMod(x, y). */
  112.     private static final LongBinaryOperator FLOORMOD_LONG;
  113.     /** getExponent(x). */
  114.     private static final FloatToIntFunction GETEXPONENT_FLOAT;
  115.     /** getExponent(x). */
  116.     private static final DoubleToIntFunction GETEXPONENT_DOUBLE;
  117.     /** hypot(x, y). */
  118.     private static final DoubleBinaryOperator HYPOT;
  119.     /** IEEEremainder(x, y). */
  120.     private static final DoubleBinaryOperator IEEEREMAINDER;
  121.     /** incrementExact(x). */
  122.     private static final IntUnaryOperator INCREMENTEXACT_INT;
  123.     /** incrementExact(x). */
  124.     private static final LongUnaryOperator INCREMENTEXACT_LONG;
  125.     /** log(x). */
  126.     private static final DoubleUnaryOperator LOG;
  127.     /** log10(x). */
  128.     private static final DoubleUnaryOperator LOG10;
  129.     /** log1p(x). */
  130.     private static final DoubleUnaryOperator LOG1P;
  131.     /** max(x, y). */
  132.     private static final IntBinaryOperator MAX_INT;
  133.     /** max(x, y). */
  134.     private static final LongBinaryOperator MAX_LONG;
  135.     /** max(x, y). */
  136.     private static final FloatBinaryOperator MAX_FLOAT;
  137.     /** max(x, y). */
  138.     private static final DoubleBinaryOperator MAX_DOUBLE;
  139.     /** min(x, y). */
  140.     private static final IntBinaryOperator MIN_INT;
  141.     /** min(x, y). */
  142.     private static final LongBinaryOperator MIN_LONG;
  143.     /** min(x, y). */
  144.     private static final FloatBinaryOperator MIN_FLOAT;
  145.     /** min(x, y). */
  146.     private static final DoubleBinaryOperator MIN_DOUBLE;
  147.     /** multiplyExact(x, y). */
  148.     private static final IntBinaryOperator MULTIPLYEXACT_INT;
  149.     /** multiplyExact(x, y). */
  150.     private static final LongBinaryOperator MULTIPLYEXACT_LONG;
  151.     /** negateExact(x). */
  152.     private static final IntUnaryOperator NEGATEEXACT_INT;
  153.     /** negateExact(x). */
  154.     private static final LongUnaryOperator NEGATEEXACT_LONG;
  155.     /** nextAfter(x, y). */
  156.     private static final FloatDouble2FloatOperator NEXTAFTER_FLOAT;
  157.     /** nextAfter(x, y). */
  158.     private static final DoubleBinaryOperator NEXTAFTER_DOUBLE;
  159.     /** nextDown(x). */
  160.     private static final FloatUnaryOperator NEXTDOWN_FLOAT;
  161.     /** nextDown(x). */
  162.     private static final DoubleUnaryOperator NEXTDOWN_DOUBLE;
  163.     /** nextUp(x). */
  164.     private static final FloatUnaryOperator NEXTUP_FLOAT;
  165.     /** nextUp(x). */
  166.     private static final DoubleUnaryOperator NEXTUP_DOUBLE;
  167.     /** pow(x, y). */
  168.     private static final DoubleBinaryOperator POW;
  169.     /** random(). */
  170.     private static final DoubleSupplier RANDOM;
  171.     /** rint(x). */
  172.     private static final DoubleUnaryOperator RINT;
  173.     /** round(x). */
  174.     private static final DoubleToLongFunction ROUND_DOUBLE;
  175.     /** round(x). */
  176.     private static final FloatToIntFunction ROUND_FLOAT;
  177.     /** scalb(x, y). */
  178.     private static final DoubleInt2DoubleOperator SCALB_DOUBLE;
  179.     /** scalb(x, y). */
  180.     private static final FloatInt2FloatOperator SCALB_FLOAT;
  181.     /** signum(x). */
  182.     private static final FloatUnaryOperator SIGNUM_FLOAT;
  183.     /** signum(x). */
  184.     private static final DoubleUnaryOperator SIGNUM_DOUBLE;
  185.     /** sin(x). */
  186.     private static final DoubleUnaryOperator SIN;
  187.     /** sinh(x). */
  188.     private static final DoubleUnaryOperator SINH;
  189.     /** sqrt(x). */
  190.     private static final DoubleUnaryOperator SQRT;
  191.     /** subtractExact(x, y). */
  192.     private static final IntBinaryOperator SUBTRACTEXACT_INT;
  193.     /** subtractExact(x, y). */
  194.     private static final LongBinaryOperator SUBTRACTEXACT_LONG;
  195.     /** tan(x). */
  196.     private static final DoubleUnaryOperator TAN;
  197.     /** tanh(x). */
  198.     private static final DoubleUnaryOperator TANH;
  199.     /** toDegrees(x). */
  200.     private static final DoubleUnaryOperator TODEGREES;
  201.     /** toIntExact(x). */
  202.     private static final LongToIntFunction TOINTEXACT;
  203.     /** toRadians(x). */
  204.     private static final DoubleUnaryOperator TORADIANS;
  205.     /** ulp(x). */
  206.     private static final DoubleUnaryOperator ULP_DOUBLE;
  207.     /** ulp(x). */
  208.     private static final FloatUnaryOperator ULP_FLOAT;

  209.     /** Available implementations of {@link Math} functions. */
  210.     public enum Impl {
  211.         /** {@link AccurateMath Commons Math}. */
  212.         CM,
  213.         /** {@link Math JDK}. */
  214.         JDK
  215.     }

  216.     static {
  217.         final String prop = System.getProperty(PROPERTY_KEY);
  218.         final Impl impl = prop != null ?
  219.             Impl.valueOf(prop) :
  220.             Impl.CM;


  221.         switch (impl) {
  222.         case CM:
  223.             PI = AccurateMath.PI;
  224.             E = AccurateMath.E;
  225.             ABS_INT = AccurateMath::abs;
  226.             ABS_LONG = AccurateMath::abs;
  227.             ABS_FLOAT = AccurateMath::abs;
  228.             ABS_DOUBLE = AccurateMath::abs;
  229.             ACOS = AccurateMath::acos;
  230.             ACOSH = AccurateMath::acosh;
  231.             ADDEXACT_INT = AccurateMath::addExact;
  232.             ADDEXACT_LONG = AccurateMath::addExact;
  233.             ASIN = AccurateMath::asin;
  234.             ASINH = AccurateMath::asinh;
  235.             ATAN = AccurateMath::atan;
  236.             ATAN2 = AccurateMath::atan2;
  237.             ATANH = AccurateMath::atanh;
  238.             CBRT = AccurateMath::cbrt;
  239.             CEIL = AccurateMath::ceil;
  240.             COPYSIGN_FLOAT = AccurateMath::copySign;
  241.             COPYSIGN_DOUBLE = AccurateMath::copySign;
  242.             COS = AccurateMath::cos;
  243.             COSH = AccurateMath::cosh;
  244.             DECREMENTEXACT_INT = AccurateMath::decrementExact;
  245.             DECREMENTEXACT_LONG = AccurateMath::decrementExact;
  246.             EXP = AccurateMath::exp;
  247.             EXPM1 = AccurateMath::expm1;
  248.             FLOOR = AccurateMath::floor;
  249.             FLOORDIV_INT = AccurateMath::floorDiv;
  250.             FLOORDIV_LONG = AccurateMath::floorDiv;
  251.             FLOORMOD_INT = AccurateMath::floorMod;
  252.             FLOORMOD_LONG = AccurateMath::floorMod;
  253.             GETEXPONENT_FLOAT = AccurateMath::getExponent;
  254.             GETEXPONENT_DOUBLE = AccurateMath::getExponent;
  255.             HYPOT = AccurateMath::hypot;
  256.             IEEEREMAINDER = AccurateMath::IEEEremainder;
  257.             INCREMENTEXACT_INT = AccurateMath::incrementExact;
  258.             INCREMENTEXACT_LONG = AccurateMath::incrementExact;
  259.             LOG = AccurateMath::log;
  260.             LOG10 = AccurateMath::log10;
  261.             LOG1P = AccurateMath::log1p;
  262.             MAX_INT = AccurateMath::max;
  263.             MAX_LONG = AccurateMath::max;
  264.             MAX_FLOAT = AccurateMath::max;
  265.             MAX_DOUBLE = AccurateMath::max;
  266.             MIN_INT = AccurateMath::min;
  267.             MIN_LONG = AccurateMath::min;
  268.             MIN_FLOAT = AccurateMath::min;
  269.             MIN_DOUBLE = AccurateMath::min;
  270.             MULTIPLYEXACT_INT = AccurateMath::multiplyExact;
  271.             MULTIPLYEXACT_LONG = AccurateMath::multiplyExact;
  272.             NEGATEEXACT_INT = Math::negateExact; // Not implemented.
  273.             NEGATEEXACT_LONG = Math::negateExact; // Not implemented.
  274.             NEXTAFTER_FLOAT = AccurateMath::nextAfter;
  275.             NEXTAFTER_DOUBLE = AccurateMath::nextAfter;
  276.             NEXTDOWN_FLOAT = AccurateMath::nextDown;
  277.             NEXTDOWN_DOUBLE = AccurateMath::nextDown;
  278.             NEXTUP_FLOAT = AccurateMath::nextUp;
  279.             NEXTUP_DOUBLE = AccurateMath::nextUp;
  280.             POW = AccurateMath::pow;
  281.             RANDOM = Math::random; // Not implemented.
  282.             RINT = AccurateMath::rint;
  283.             ROUND_DOUBLE = AccurateMath::round;
  284.             ROUND_FLOAT = AccurateMath::round;
  285.             SCALB_DOUBLE = AccurateMath::scalb;
  286.             SCALB_FLOAT = AccurateMath::scalb;
  287.             SIGNUM_DOUBLE = AccurateMath::signum;
  288.             SIGNUM_FLOAT = AccurateMath::signum;
  289.             SQRT = Math::sqrt; // Not implemented.
  290.             SIN = AccurateMath::sin;
  291.             SINH = AccurateMath::sinh;
  292.             SUBTRACTEXACT_INT = AccurateMath::subtractExact;
  293.             SUBTRACTEXACT_LONG = AccurateMath::subtractExact;
  294.             TAN = AccurateMath::tan;
  295.             TANH = AccurateMath::tanh;
  296.             TODEGREES = AccurateMath::toDegrees;
  297.             TOINTEXACT = AccurateMath::toIntExact;
  298.             TORADIANS = AccurateMath::toRadians;
  299.             ULP_DOUBLE = AccurateMath::ulp;
  300.             ULP_FLOAT = AccurateMath::ulp;
  301.             break;

  302.         case JDK:
  303.             PI = Math.PI;
  304.             E = Math.E;
  305.             ABS_INT = Math::abs;
  306.             ABS_LONG = Math::abs;
  307.             ABS_FLOAT = Math::abs;
  308.             ABS_DOUBLE = Math::abs;
  309.             ACOS = Math::acos;
  310.             ACOSH = AccurateMath::acosh; // Not implemented.
  311.             ADDEXACT_INT = Math::addExact;
  312.             ADDEXACT_LONG = Math::addExact;
  313.             ASIN = Math::asin;
  314.             ASINH = AccurateMath::asinh; // Not implemented.
  315.             ATAN = Math::atan;
  316.             ATAN2 = Math::atan2;
  317.             ATANH = AccurateMath::atanh; // Not implemented.
  318.             CBRT = Math::cbrt;
  319.             CEIL = Math::ceil;
  320.             COPYSIGN_FLOAT = Math::copySign;
  321.             COPYSIGN_DOUBLE = Math::copySign;
  322.             COS = Math::cos;
  323.             COSH = Math::cosh;
  324.             DECREMENTEXACT_INT = Math::decrementExact;
  325.             DECREMENTEXACT_LONG = Math::decrementExact;
  326.             EXP = Math::exp;
  327.             EXPM1 = Math::expm1;
  328.             FLOOR = Math::floor;
  329.             FLOORDIV_INT = Math::floorDiv;
  330.             FLOORDIV_LONG = Math::floorDiv;
  331.             FLOORMOD_INT = Math::floorMod;
  332.             FLOORMOD_LONG = Math::floorMod;
  333.             GETEXPONENT_FLOAT = Math::getExponent;
  334.             GETEXPONENT_DOUBLE = Math::getExponent;
  335.             HYPOT = Math::hypot;
  336.             IEEEREMAINDER = Math::IEEEremainder;
  337.             INCREMENTEXACT_INT = Math::incrementExact;
  338.             INCREMENTEXACT_LONG = Math::incrementExact;
  339.             LOG = Math::log;
  340.             LOG10 = Math::log10;
  341.             LOG1P = Math::log1p;
  342.             MAX_INT = Math::max;
  343.             MAX_LONG = Math::max;
  344.             MAX_FLOAT = Math::max;
  345.             MAX_DOUBLE = Math::max;
  346.             MIN_INT = Math::min;
  347.             MIN_LONG = Math::min;
  348.             MIN_FLOAT = Math::min;
  349.             MIN_DOUBLE = Math::min;
  350.             MULTIPLYEXACT_INT = Math::multiplyExact;
  351.             MULTIPLYEXACT_LONG = Math::multiplyExact;
  352.             NEGATEEXACT_INT = Math::negateExact;
  353.             NEGATEEXACT_LONG = Math::negateExact;
  354.             NEXTAFTER_FLOAT = Math::nextAfter;
  355.             NEXTAFTER_DOUBLE = Math::nextAfter;
  356.             NEXTDOWN_FLOAT = Math::nextDown;
  357.             NEXTDOWN_DOUBLE = Math::nextDown;
  358.             NEXTUP_FLOAT = Math::nextUp;
  359.             NEXTUP_DOUBLE = Math::nextUp;
  360.             POW = Math::pow;
  361.             RANDOM = Math::random;
  362.             RINT = Math::rint;
  363.             ROUND_DOUBLE = Math::round;
  364.             ROUND_FLOAT = Math::round;
  365.             SCALB_DOUBLE = Math::scalb;
  366.             SCALB_FLOAT = Math::scalb;
  367.             SIGNUM_DOUBLE = Math::signum;
  368.             SIGNUM_FLOAT = Math::signum;
  369.             SIN = Math::sin;
  370.             SINH = Math::sinh;
  371.             SQRT = Math::sqrt;
  372.             SUBTRACTEXACT_INT = Math::subtractExact;
  373.             SUBTRACTEXACT_LONG = Math::subtractExact;
  374.             TAN = Math::tan;
  375.             TANH = Math::tanh;
  376.             TODEGREES = Math::toDegrees;
  377.             TOINTEXACT = Math::toIntExact;
  378.             TORADIANS = Math::toRadians;
  379.             ULP_DOUBLE = Math::ulp;
  380.             ULP_FLOAT = Math::ulp;
  381.             break;

  382.         default:
  383.             throw new IllegalStateException("Internal error"); // Should never happen.
  384.         }
  385.     }

  386.     /** Utility class. */
  387.     private JdkMath() {}

  388.     /**
  389.      * @param x Number.
  390.      * @return abs(x).
  391.      *
  392.      * @see Math#abs(int)
  393.      */
  394.     public static int abs(int x) {
  395.         return ABS_INT.applyAsInt(x);
  396.     }

  397.     /**
  398.      * @param x Number.
  399.      * @return abs(x).
  400.      *
  401.      * @see Math#abs(long)
  402.      */
  403.     public static long abs(long x) {
  404.         return ABS_LONG.applyAsLong(x);
  405.     }

  406.     /**
  407.      * @param x Number.
  408.      * @return abs(x).
  409.      *
  410.      * @see Math#abs(float)
  411.      */
  412.     public static float abs(float x) {
  413.         return ABS_FLOAT.applyAsFloat(x);
  414.     }

  415.     /**
  416.      * @param x Number.
  417.      * @return abs(x).
  418.      *
  419.      * @see Math#abs(double)
  420.      */
  421.     public static double abs(double x) {
  422.         return ABS_DOUBLE.applyAsDouble(x);
  423.     }

  424.     /**
  425.      * @param x Number.
  426.      * @return acos(x).
  427.      *
  428.      * @see Math#acos(double)
  429.      */
  430.     public static double acos(double x) {
  431.         return ACOS.applyAsDouble(x);
  432.     }

  433.     /**
  434.      * @param x Number.
  435.      * @return acosh(x).
  436.      */
  437.     public static double acosh(double x) {
  438.         return ACOSH.applyAsDouble(x);
  439.     }

  440.     /**
  441.      * @param x Number.
  442.      * @param y Number.
  443.      * @return addExact(x, y).
  444.      *
  445.      * @see Math#addExact(int,int)
  446.      */
  447.     public static int addExact(int x,
  448.                                int y) {
  449.         return ADDEXACT_INT.applyAsInt(x, y);
  450.     }

  451.     /**
  452.      * @param x Number.
  453.      * @param y Number.
  454.      * @return addExact(x, y).
  455.      *
  456.      * @see Math#addExact(long,long)
  457.      */
  458.     public static long addExact(long x,
  459.                                 long y) {
  460.         return ADDEXACT_LONG.applyAsLong(x, y);
  461.     }

  462.     /**
  463.      * @param x Number.
  464.      * @return asin(x).
  465.      *
  466.      * @see Math#asin(double)
  467.      */
  468.     public static double asin(double x) {
  469.         return ASIN.applyAsDouble(x);
  470.     }

  471.     /**
  472.      * @param x Number.
  473.      * @return asinh(x).
  474.      */
  475.     public static double asinh(double x) {
  476.         return ASINH.applyAsDouble(x);
  477.     }

  478.     /**
  479.      * @param x Number.
  480.      * @return atan(x).
  481.      *
  482.      * @see Math#atan(double)
  483.      */
  484.     public static double atan(double x) {
  485.         return ATAN.applyAsDouble(x);
  486.     }

  487.     /**
  488.      * @param y Number.
  489.      * @param x Number.
  490.      * @return atan2(y, x).
  491.      *
  492.      * @see Math#atan2(double,double)
  493.      */
  494.     public static double atan2(double y,
  495.                                double x) {
  496.         return ATAN2.applyAsDouble(y, x);
  497.     }

  498.     /**
  499.      * @param x Number.
  500.      * @return atanh(x).
  501.      */
  502.     public static double atanh(double x) {
  503.         return ATANH.applyAsDouble(x);
  504.     }

  505.     /**
  506.      * @param x Number.
  507.      * @return cbrt(x).
  508.      *
  509.      * @see Math#cbrt(double)
  510.      */
  511.     public static double cbrt(double x) {
  512.         return CBRT.applyAsDouble(x);
  513.     }

  514.     /**
  515.      * @param x Number.
  516.      * @return ceil(x).
  517.      *
  518.      * @see Math#ceil(double)
  519.      */
  520.     public static double ceil(double x) {
  521.         return CEIL.applyAsDouble(x);
  522.     }

  523.     /**
  524.      * @param x Number.
  525.      * @param y Number.
  526.      * @return copySign(x, y).
  527.      *
  528.      * @see Math#copySign(float,float)
  529.      */
  530.     public static float copySign(float x,
  531.                                  float y) {
  532.         return COPYSIGN_FLOAT.applyAsFloat(x, y);
  533.     }

  534.     /**
  535.      * @param x Number.
  536.      * @param y Number.
  537.      * @return copySign(x, y).
  538.      *
  539.      * @see Math#copySign(double,double)
  540.      */
  541.     public static double copySign(double x,
  542.                                   double y) {
  543.         return COPYSIGN_DOUBLE.applyAsDouble(x, y);
  544.     }

  545.     /**
  546.      * @param x Number.
  547.      * @return cos(x).
  548.      *
  549.      * @see Math#cos(double)
  550.      */
  551.     public static double cos(double x) {
  552.         return COS.applyAsDouble(x);
  553.     }

  554.     /**
  555.      * @param x Number.
  556.      * @return cosh(x).
  557.      *
  558.      * @see Math#cosh(double)
  559.      */
  560.     public static double cosh(double x) {
  561.         return COSH.applyAsDouble(x);
  562.     }

  563.     /**
  564.      * @param x Number.
  565.      * @return decrementExact(x).
  566.      *
  567.      * @see Math#decrementExact(int)
  568.      */
  569.     public static int decrementExact(int x) {
  570.         return DECREMENTEXACT_INT.applyAsInt(x);
  571.     }

  572.     /**
  573.      * @param x Number.
  574.      * @return decrementExact(x).
  575.      *
  576.      * @see Math#decrementExact(long)
  577.      */
  578.     public static long decrementExact(long x) {
  579.         return DECREMENTEXACT_LONG.applyAsLong(x);
  580.     }

  581.     /**
  582.      * @param x Number.
  583.      * @return exp(x).
  584.      *
  585.      * @see Math#exp(double)
  586.      */
  587.     public static double exp(double x) {
  588.         return EXP.applyAsDouble(x);
  589.     }

  590.     /**
  591.      * @param x Number.
  592.      * @return expm1(x).
  593.      *
  594.      * @see Math#expm1(double)
  595.      */
  596.     public static double expm1(double x) {
  597.         return EXPM1.applyAsDouble(x);
  598.     }

  599.     /**
  600.      * @param x Number.
  601.      * @return floor(x).
  602.      *
  603.      * @see Math#floor(double)
  604.      */
  605.     public static double floor(double x) {
  606.         return FLOOR.applyAsDouble(x);
  607.     }

  608.     /**
  609.      * @param x Number.
  610.      * @param y Number.
  611.      * @return floorDiv(x, y).
  612.      *
  613.      * @see Math#floorDiv(int,int)
  614.      */
  615.     public static int floorDiv(int x,
  616.                                int y) {
  617.         return FLOORDIV_INT.applyAsInt(x, y);
  618.     }

  619.     /**
  620.      * @param x Number.
  621.      * @param y Number.
  622.      * @return floorDiv(x, y).
  623.      *
  624.      * @see Math#floorDiv(long,long)
  625.      */
  626.     public static long floorDiv(long x,
  627.                                 long y) {
  628.         return FLOORDIV_LONG.applyAsLong(x, y);
  629.     }

  630.     /**
  631.      * @param x Number.
  632.      * @param y Number.
  633.      * @return floorMod(x, y).
  634.      *
  635.      * @see Math#floorMod(int,int)
  636.      */
  637.     public static int floorMod(int x,
  638.                                int y) {
  639.         return FLOORMOD_INT.applyAsInt(x, y);
  640.     }

  641.     /**
  642.      * @param x Number.
  643.      * @param y Number.
  644.      * @return floorMod(x, y).
  645.      *
  646.      * @see Math#floorMod(long,long)
  647.      */
  648.     public static long floorMod(long x,
  649.                                 long y) {
  650.         return FLOORMOD_LONG.applyAsLong(x, y);
  651.     }

  652.     /**
  653.      * @param x Number.
  654.      * @return getExponent(x).
  655.      *
  656.      * @see Math#getExponent(float)
  657.      */
  658.     public static int getExponent(float x) {
  659.         return GETEXPONENT_FLOAT.applyAsInt(x);
  660.     }

  661.     /**
  662.      * @param x Number.
  663.      * @return getExponent(x).
  664.      *
  665.      * @see Math#getExponent(double)
  666.      */
  667.     public static int getExponent(double x) {
  668.         return GETEXPONENT_DOUBLE.applyAsInt(x);
  669.     }

  670.     /**
  671.      * @param x Number.
  672.      * @param y Number.
  673.      * @return hypot(x, y).
  674.      *
  675.      * @see Math#hypot(double,double)
  676.      */
  677.     public static double hypot(double x,
  678.                                double y) {
  679.         return HYPOT.applyAsDouble(x, y);
  680.     }

  681.     /**
  682.      * @param x Number.
  683.      * @param y Number.
  684.      * @return IEEEremainder(x, y).
  685.      *
  686.      * @see Math#IEEEremainder(double,double)
  687.      */
  688.     public static double IEEEremainder(double x,
  689.                                        double y) {
  690.         return IEEEREMAINDER.applyAsDouble(x, y);
  691.     }

  692.     /**
  693.      * @param x Number.
  694.      * @return incrementExact(x).
  695.      *
  696.      * @see Math#incrementExact(int)
  697.      */
  698.     public static int incrementExact(int x) {
  699.         return INCREMENTEXACT_INT.applyAsInt(x);
  700.     }

  701.     /**
  702.      * @param x Number.
  703.      * @return incrementExact(x).
  704.      *
  705.      * @see Math#incrementExact(long)
  706.      */
  707.     public static long incrementExact(long x) {
  708.         return INCREMENTEXACT_LONG.applyAsLong(x);
  709.     }

  710.     /**
  711.      * @param x Number.
  712.      * @return log(x).
  713.      *
  714.      * @see Math#log(double)
  715.      */
  716.     public static double log(double x) {
  717.         return LOG.applyAsDouble(x);
  718.     }

  719.     /**
  720.      * @param x Number.
  721.      * @return log10(x).
  722.      *
  723.      * @see Math#log10(double)
  724.      */
  725.     public static double log10(double x) {
  726.         return LOG10.applyAsDouble(x);
  727.     }

  728.     /**
  729.      * @param x Number.
  730.      * @return log1p(x).
  731.      *
  732.      * @see Math#log1p(double)
  733.      */
  734.     public static double log1p(double x) {
  735.         return LOG1P.applyAsDouble(x);
  736.     }

  737.     /**
  738.      * @param x Number.
  739.      * @param y Number.
  740.      * @return max(x, y).
  741.      *
  742.      * @see Math#max(int,int)
  743.      */
  744.     public static int max(int x,
  745.                           int y) {
  746.         return MAX_INT.applyAsInt(x, y);
  747.     }

  748.     /**
  749.      * @param x Number.
  750.      * @param y Number.
  751.      * @return max(x, y).
  752.      *
  753.      * @see Math#max(long,long)
  754.      */
  755.     public static long max(long x,
  756.                            long y) {
  757.         return MAX_LONG.applyAsLong(x, y);
  758.     }

  759.     /**
  760.      * @param x Number.
  761.      * @param y Number.
  762.      * @return max(x, y).
  763.      *
  764.      * @see Math#max(float,float)
  765.      */
  766.     public static float max(float x,
  767.                             float y) {
  768.         return MAX_FLOAT.applyAsFloat(x, y);
  769.     }

  770.     /**
  771.      * @param x Number.
  772.      * @param y Number.
  773.      * @return max(x, y).
  774.      *
  775.      * @see Math#max(double,double)
  776.      */
  777.     public static double max(double x,
  778.                              double y) {
  779.         return MAX_DOUBLE.applyAsDouble(x, y);
  780.     }

  781.     /**
  782.      * @param x Number.
  783.      * @param y Number.
  784.      * @return min(x, y).
  785.      *
  786.      * @see Math#min(int,int)
  787.      */
  788.     public static int min(int x,
  789.                           int y) {
  790.         return MIN_INT.applyAsInt(x, y);
  791.     }

  792.     /**
  793.      * @param x Number.
  794.      * @param y Number.
  795.      * @return min(x, y).
  796.      *
  797.      * @see Math#min(long,long)
  798.      */
  799.     public static long min(long x,
  800.                            long y) {
  801.         return MIN_LONG.applyAsLong(x, y);
  802.     }

  803.     /**
  804.      * @param x Number.
  805.      * @param y Number.
  806.      * @return min(x, y).
  807.      *
  808.      * @see Math#min(float,float)
  809.      */
  810.     public static float min(float x,
  811.                             float y) {
  812.         return MIN_FLOAT.applyAsFloat(x, y);
  813.     }

  814.     /**
  815.      * @param x Number.
  816.      * @param y Number.
  817.      * @return min(x, y).
  818.      *
  819.      * @see Math#min(double,double)
  820.      */
  821.     public static double min(double x,
  822.                              double y) {
  823.         return MIN_DOUBLE.applyAsDouble(x, y);
  824.     }

  825.     /**
  826.      * @param x Number.
  827.      * @param y Number.
  828.      * @return multiplyExact(x, y).
  829.      *
  830.      * @see Math#multiplyExact(int,int)
  831.      */
  832.     public static int multiplyExact(int x,
  833.                                     int y) {
  834.         return MULTIPLYEXACT_INT.applyAsInt(x, y);
  835.     }

  836.     /**
  837.      * @param x Number.
  838.      * @param y Number.
  839.      * @return multiplyExact(x, y).
  840.      *
  841.      * @see Math#multiplyExact(long,long)
  842.      */
  843.     public static long multiplyExact(long x,
  844.                                      long y) {
  845.         return MULTIPLYEXACT_LONG.applyAsLong(x, y);
  846.     }

  847.     /**
  848.      * @param x Number.
  849.      * @return negateExact(x).
  850.      *
  851.      * @see Math#negateExact(int)
  852.      */
  853.     public static int negateExact(int x) {
  854.         return NEGATEEXACT_INT.applyAsInt(x);
  855.     }

  856.     /**
  857.      * @param x Number.
  858.      * @return negateExact(x).
  859.      *
  860.      * @see Math#negateExact(long)
  861.      */
  862.     public static long negateExact(long x) {
  863.         return NEGATEEXACT_LONG.applyAsLong(x);
  864.     }

  865.     /**
  866.      * @param x Number.
  867.      * @param y Number.
  868.      * @return nextAfter(x, y).
  869.      *
  870.      * @see Math#nextAfter(double, double)
  871.      */
  872.     public static double nextAfter(double x,
  873.                                    double y) {
  874.         return NEXTAFTER_DOUBLE.applyAsDouble(x, y);
  875.     }

  876.     /**
  877.      * @param x Number.
  878.      * @param y Number.
  879.      * @return nextAfter(x, y).
  880.      *
  881.      * @see Math#nextAfter(float,double)
  882.      */
  883.     public static float nextAfter(float x,
  884.                                   double y) {
  885.         return NEXTAFTER_FLOAT.applyAsFloat(x, y);
  886.     }

  887.     /**
  888.      * @param x Number.
  889.      * @return nextDown(x).
  890.      *
  891.      * @see Math#nextDown(double)
  892.      */
  893.     public static double nextDown(double x) {
  894.         return NEXTDOWN_DOUBLE.applyAsDouble(x);
  895.     }

  896.     /**
  897.      * @param x Number.
  898.      * @return nextDown(x).
  899.      *
  900.      * @see Math#nextDown(float)
  901.      */
  902.     public static float nextDown(float x) {
  903.         return NEXTDOWN_FLOAT.applyAsFloat(x);
  904.     }

  905.     /**
  906.      * @param x Number.
  907.      * @return nextUp(x).
  908.      *
  909.      * @see Math#nextUp(double)
  910.      */
  911.     public static double nextUp(double x) {
  912.         return NEXTUP_DOUBLE.applyAsDouble(x);
  913.     }

  914.     /**
  915.      * @param x Number.
  916.      * @return nextUp(x).
  917.      *
  918.      * @see Math#nextUp(float)
  919.      */
  920.     public static float nextUp(float x) {
  921.         return NEXTUP_FLOAT.applyAsFloat(x);
  922.     }

  923.     /**
  924.      * @param x Number.
  925.      * @param y Number.
  926.      * @return pow(x, y).
  927.      *
  928.      * @see Math#pow(double,double)
  929.      */
  930.     public static double pow(double x,
  931.                              double y) {
  932.         return POW.applyAsDouble(x, y);
  933.     }

  934.     /**
  935.      * @return a random number between 0 and 1.
  936.      *
  937.      * @see Math#random()
  938.      */
  939.     public static double random() {
  940.         return RANDOM.getAsDouble();
  941.     }

  942.     /**
  943.      * @param x Number.
  944.      * @return rint(x).
  945.      *
  946.      * @see Math#rint(double)
  947.      */
  948.     public static double rint(double x) {
  949.         return RINT.applyAsDouble(x);
  950.     }

  951.     /**
  952.      * @param x Number.
  953.      * @return round(x).
  954.      *
  955.      * @see Math#round(float)
  956.      */
  957.     public static int round(float x) {
  958.         return ROUND_FLOAT.applyAsInt(x);
  959.     }

  960.     /**
  961.      * @param x Number.
  962.      * @return round(x).
  963.      *
  964.      * @see Math#round(double)
  965.      */
  966.     public static long round(double x) {
  967.         return ROUND_DOUBLE.applyAsLong(x);
  968.     }

  969.     /**
  970.      * @param x Number.
  971.      * @param y Number.
  972.      * @return scalb(x, y).
  973.      *
  974.      * @see Math#scalb(double,int)
  975.      */
  976.     public static double scalb(double x,
  977.                                int y) {
  978.         return SCALB_DOUBLE.applyAsDouble(x, y);
  979.     }

  980.     /**
  981.      * @param x Number.
  982.      * @param y Number.
  983.      * @return scalb(x, y).
  984.      *
  985.      * @see Math#scalb(float,int)
  986.      */
  987.     public static float scalb(float x,
  988.                               int y) {
  989.         return SCALB_FLOAT.applyAsFloat(x, y);
  990.     }

  991.     /**
  992.      * @param x Number.
  993.      * @return signum(x).
  994.      *
  995.      * @see Math#signum(double)
  996.      */
  997.     public static double signum(double x) {
  998.         return SIGNUM_DOUBLE.applyAsDouble(x);
  999.     }

  1000.     /**
  1001.      * @param x Number.
  1002.      * @return signum(x).
  1003.      *
  1004.      * @see Math#signum(float)
  1005.      */
  1006.     public static float signum(float x) {
  1007.         return SIGNUM_FLOAT.applyAsFloat(x);
  1008.     }

  1009.     /**
  1010.      * @param x Number.
  1011.      * @return sin(x).
  1012.      *
  1013.      * @see Math#sin(double)
  1014.      */
  1015.     public static double sin(double x) {
  1016.         return SIN.applyAsDouble(x);
  1017.     }

  1018.     /**
  1019.      * @param x Number.
  1020.      * @return sinh(x).
  1021.      *
  1022.      * @see Math#sinh(double)
  1023.      */
  1024.     public static double sinh(double x) {
  1025.         return SINH.applyAsDouble(x);
  1026.     }

  1027.     /**
  1028.      * @param x Number.
  1029.      * @return sqrt(x).
  1030.      *
  1031.      * @see Math#sqrt(double)
  1032.      */
  1033.     public static double sqrt(double x) {
  1034.         return SQRT.applyAsDouble(x);
  1035.     }

  1036.     /**
  1037.      * @param x Number.
  1038.      * @param y Number.
  1039.      * @return subtractExact(x, y).
  1040.      *
  1041.      * @see Math#subtractExact(int,int)
  1042.      */
  1043.     public static int subtractExact(int x,
  1044.                                     int y) {
  1045.         return SUBTRACTEXACT_INT.applyAsInt(x, y);
  1046.     }

  1047.     /**
  1048.      * @param x Number.
  1049.      * @param y Number.
  1050.      * @return subtractExact(x, y).
  1051.      *
  1052.      * @see Math#subtractExact(long,long)
  1053.      */
  1054.     public static long subtractExact(long x,
  1055.                                      long y) {
  1056.         return SUBTRACTEXACT_LONG.applyAsLong(x, y);
  1057.     }

  1058.     /**
  1059.      * @param x Number.
  1060.      * @return tan(x).
  1061.      *
  1062.      * @see Math#tan(double)
  1063.      */
  1064.     public static double tan(double x) {
  1065.         return TAN.applyAsDouble(x);
  1066.     }

  1067.     /**
  1068.      * @param x Number.
  1069.      * @return tanh(x).
  1070.      *
  1071.      * @see Math#tanh(double)
  1072.      */
  1073.     public static double tanh(double x) {
  1074.         return TANH.applyAsDouble(x);
  1075.     }

  1076.     /**
  1077.      * @param x Number.
  1078.      * @return toDegrees(x).
  1079.      *
  1080.      * @see Math#toDegrees(double)
  1081.      */
  1082.     public static double toDegrees(double x) {
  1083.         return TODEGREES.applyAsDouble(x);
  1084.     }

  1085.     /**
  1086.      * @param x Number.
  1087.      * @return toIntExact(x).
  1088.      *
  1089.      * @see Math#toIntExact(long)
  1090.      */
  1091.     public static int toIntExact(long x) {
  1092.         return TOINTEXACT.applyAsInt(x);
  1093.     }

  1094.     /**
  1095.      * @param x Number.
  1096.      * @return toRadians(x).
  1097.      *
  1098.      * @see Math#toRadians(double)
  1099.      */
  1100.     public static double toRadians(double x) {
  1101.         return TORADIANS.applyAsDouble(x);
  1102.     }

  1103.     /**
  1104.      * @param x Number.
  1105.      * @return ulp(x).
  1106.      *
  1107.      * @see Math#ulp(double)
  1108.      */
  1109.     public static double ulp(double x) {
  1110.         return ULP_DOUBLE.applyAsDouble(x);
  1111.     }

  1112.     /**
  1113.      * @param x Number.
  1114.      * @return ulp(x).
  1115.      *
  1116.      * @see Math#ulp(float)
  1117.      */
  1118.     public static float ulp(float x) {
  1119.         return ULP_FLOAT.applyAsFloat(x);
  1120.     }

  1121.     /** Interface missing from "java.util.function" package. */
  1122.     private interface FloatUnaryOperator {
  1123.         /**
  1124.          * @param x Operand.
  1125.          * @return the result of applying this operator.
  1126.          */
  1127.         float applyAsFloat(float x);
  1128.     }

  1129.     /** Interface missing from "java.util.function" package. */
  1130.     private interface FloatBinaryOperator {
  1131.         /**
  1132.          * @param x Operand.
  1133.          * @param y Operand.
  1134.          * @return the result of applying this operator.
  1135.          */
  1136.         float applyAsFloat(float x, float y);
  1137.     }

  1138.     /** Interface missing from "java.util.function" package. */
  1139.     private interface FloatDouble2FloatOperator {
  1140.         /**
  1141.          * @param x Operand.
  1142.          * @param y Operand.
  1143.          * @return the result of applying this operator.
  1144.          */
  1145.         float applyAsFloat(float x, double y);
  1146.     }

  1147.     /** Interface missing from "java.util.function" package. */
  1148.     private interface FloatToIntFunction {
  1149.         /**
  1150.          * @param x Operand.
  1151.          * @return the result of applying this operator.
  1152.          */
  1153.         int applyAsInt(float x);
  1154.     }

  1155.     /** Interface missing from "java.util.function" package. */
  1156.     private interface FloatInt2FloatOperator {
  1157.         /**
  1158.          * @param x Operand.
  1159.          * @param y Operand.
  1160.          * @return the result of applying this operator.
  1161.          */
  1162.         float applyAsFloat(float x, int y);
  1163.     }

  1164.     /** Interface missing from "java.util.function" package. */
  1165.     private interface DoubleInt2DoubleOperator {
  1166.         /**
  1167.          * @param x Operand.
  1168.          * @param y Operand.
  1169.          * @return the result of applying this operator.
  1170.          */
  1171.         double applyAsDouble(double x, int y);
  1172.     }
  1173. }