jdk/src/java.base/share/classes/java/lang/Math.java
changeset 37364 80be215c8c51
parent 35979 4462913d471a
child 38452 ca210bc11ed7
equal deleted inserted replaced
37363:329dba26ffd2 37364:80be215c8c51
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.lang;
    26 package java.lang;
    27 
    27 
       
    28 import java.math.BigDecimal;
    28 import java.util.Random;
    29 import java.util.Random;
    29 import jdk.internal.math.FloatConsts;
    30 import jdk.internal.math.FloatConsts;
    30 import jdk.internal.math.DoubleConsts;
    31 import jdk.internal.math.DoubleConsts;
    31 import jdk.internal.HotSpotIntrinsicCandidate;
    32 import jdk.internal.HotSpotIntrinsicCandidate;
    32 
    33 
  1448         }
  1449         }
  1449         return (a <= b) ? a : b;
  1450         return (a <= b) ? a : b;
  1450     }
  1451     }
  1451 
  1452 
  1452     /**
  1453     /**
       
  1454      * Returns the fused multiply add of the three arguments; that is,
       
  1455      * returns the exact product of the first two arguments summed
       
  1456      * with the third argument and then rounded once to the nearest
       
  1457      * {@code double}.
       
  1458      *
       
  1459      * The rounding is done using the {@linkplain
       
  1460      * java.math.RoundingMode#HALF_EVEN round to nearest even
       
  1461      * rounding mode}.
       
  1462      *
       
  1463      * In contrast, if {@code a * b + c} is evaluated as a regular
       
  1464      * floating-point expression, two rounding errors are involved,
       
  1465      * the first for the multiply operation, the second for the
       
  1466      * addition operation.
       
  1467      *
       
  1468      * <p>Special cases:
       
  1469      * <ul>
       
  1470      * <li> If any argument is NaN, the result is NaN.
       
  1471      *
       
  1472      * <li> If one of the first two arguments is infinite and the
       
  1473      * other is zero, the result is NaN.
       
  1474      *
       
  1475      * <li> If the exact product of the first two arguments is infinite
       
  1476      * (in other words, at least one of the arguments is infinite and
       
  1477      * the other is neither zero nor NaN) and the third argument is an
       
  1478      * infinity of the opposite sign, the result is NaN.
       
  1479      *
       
  1480      * </ul>
       
  1481      *
       
  1482      * <p>Note that {@code fma(a, 1.0, c)} returns the same
       
  1483      * result as ({@code a + c}).  However,
       
  1484      * {@code fma(a, b, +0.0)} does <em>not</em> always return the
       
  1485      * same result as ({@code a * b}) since
       
  1486      * {@code fma(-0.0, +0.0, +0.0)} is {@code +0.0} while
       
  1487      * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fma(a, b, -0.0)} is
       
  1488      * equivalent to ({@code a * b}) however.
       
  1489      *
       
  1490      * @apiNote This method corresponds to the fusedMultiplyAdd
       
  1491      * operation defined in IEEE 754-2008.
       
  1492      *
       
  1493      * @param a a value
       
  1494      * @param b a value
       
  1495      * @param c a value
       
  1496      *
       
  1497      * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
       
  1498      * computed, as if with unlimited range and precision, and rounded
       
  1499      * once to the nearest {@code double} value
       
  1500      */
       
  1501     // @HotSpotIntrinsicCandidate
       
  1502     public static double fma(double a, double b, double c) {
       
  1503         /*
       
  1504          * Infinity and NaN arithmetic is not quite the same with two
       
  1505          * roundings as opposed to just one so the simple expression
       
  1506          * "a * b + c" cannot always be used to compute the correct
       
  1507          * result.  With two roundings, the product can overflow and
       
  1508          * if the addend is infinite, a spurious NaN can be produced
       
  1509          * if the infinity from the overflow and the infinite addend
       
  1510          * have opposite signs.
       
  1511          */
       
  1512 
       
  1513         // First, screen for and handle non-finite input values whose
       
  1514         // arithmetic is not supported by BigDecimal.
       
  1515         if (Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(c)) {
       
  1516             return Double.NaN;
       
  1517         } else { // All inputs non-NaN
       
  1518             boolean infiniteA = Double.isInfinite(a);
       
  1519             boolean infiniteB = Double.isInfinite(b);
       
  1520             boolean infiniteC = Double.isInfinite(c);
       
  1521             double result;
       
  1522 
       
  1523             if (infiniteA || infiniteB || infiniteC) {
       
  1524                 if (infiniteA && b == 0.0 ||
       
  1525                     infiniteB && a == 0.0 ) {
       
  1526                     return Double.NaN;
       
  1527                 }
       
  1528                 // Store product in a double field to cause an
       
  1529                 // overflow even if non-strictfp evaluation is being
       
  1530                 // used.
       
  1531                 double product = a * b;
       
  1532                 if (Double.isInfinite(product) && !infiniteA && !infiniteB) {
       
  1533                     // Intermediate overflow; might cause a
       
  1534                     // spurious NaN if added to infinite c.
       
  1535                     assert Double.isInfinite(c);
       
  1536                     return c;
       
  1537                 } else {
       
  1538                     result = product + c;
       
  1539                     assert !Double.isFinite(result);
       
  1540                     return result;
       
  1541                 }
       
  1542             } else { // All inputs finite
       
  1543                 BigDecimal product = (new BigDecimal(a)).multiply(new BigDecimal(b));
       
  1544                 if (c == 0.0) { // Positive or negative zero
       
  1545                     // If the product is an exact zero, use a
       
  1546                     // floating-point expression to compute the sign
       
  1547                     // of the zero final result. The product is an
       
  1548                     // exact zero if and only if at least one of a and
       
  1549                     // b is zero.
       
  1550                     if (a == 0.0 || b == 0.0) {
       
  1551                         return a * b + c;
       
  1552                     } else {
       
  1553                         // The sign of a zero addend doesn't matter if
       
  1554                         // the product is nonzero. The sign of a zero
       
  1555                         // addend is not factored in the result if the
       
  1556                         // exact product is nonzero but underflows to
       
  1557                         // zero; see IEEE-754 2008 section 6.3 "The
       
  1558                         // sign bit".
       
  1559                         return product.doubleValue();
       
  1560                     }
       
  1561                 } else {
       
  1562                     return product.add(new BigDecimal(c)).doubleValue();
       
  1563                 }
       
  1564             }
       
  1565         }
       
  1566     }
       
  1567 
       
  1568     /**
       
  1569      * Returns the fused multiply add of the three arguments; that is,
       
  1570      * returns the exact product of the first two arguments summed
       
  1571      * with the third argument and then rounded once to the nearest
       
  1572      * {@code float}.
       
  1573      *
       
  1574      * The rounding is done using the {@linkplain
       
  1575      * java.math.RoundingMode#HALF_EVEN round to nearest even
       
  1576      * rounding mode}.
       
  1577      *
       
  1578      * In contrast, if {@code a * b + c} is evaluated as a regular
       
  1579      * floating-point expression, two rounding errors are involved,
       
  1580      * the first for the multiply operation, the second for the
       
  1581      * addition operation.
       
  1582      *
       
  1583      * <p>Special cases:
       
  1584      * <ul>
       
  1585      * <li> If any argument is NaN, the result is NaN.
       
  1586      *
       
  1587      * <li> If one of the first two arguments is infinite and the
       
  1588      * other is zero, the result is NaN.
       
  1589      *
       
  1590      * <li> If the exact product of the first two arguments is infinite
       
  1591      * (in other words, at least one of the arguments is infinite and
       
  1592      * the other is neither zero nor NaN) and the third argument is an
       
  1593      * infinity of the opposite sign, the result is NaN.
       
  1594      *
       
  1595      * </ul>
       
  1596      *
       
  1597      * <p>Note that {@code fma(a, 1.0f, c)} returns the same
       
  1598      * result as ({@code a + c}).  However,
       
  1599      * {@code fma(a, b, +0.0f)} does <em>not</em> always return the
       
  1600      * same result as ({@code a * b}) since
       
  1601      * {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while
       
  1602      * ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is
       
  1603      * equivalent to ({@code a * b}) however.
       
  1604      *
       
  1605      * @apiNote This method corresponds to the fusedMultiplyAdd
       
  1606      * operation defined in IEEE 754-2008.
       
  1607      *
       
  1608      * @param a a value
       
  1609      * @param b a value
       
  1610      * @param c a value
       
  1611      *
       
  1612      * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
       
  1613      * computed, as if with unlimited range and precision, and rounded
       
  1614      * once to the nearest {@code float} value
       
  1615      */
       
  1616     // @HotSpotIntrinsicCandidate
       
  1617     public static float fma(float a, float b, float c) {
       
  1618         /*
       
  1619          *  Since the double format has more than twice the precision
       
  1620          *  of the float format, the multiply of a * b is exact in
       
  1621          *  double. The add of c to the product then incurs one
       
  1622          *  rounding error. Since the double format moreover has more
       
  1623          *  than (2p + 2) precision bits compared to the p bits of the
       
  1624          *  float format, the two roundings of (a * b + c), first to
       
  1625          *  the double format and then secondarily to the float format,
       
  1626          *  are equivalent to rounding the intermediate result directly
       
  1627          *  to the float format.
       
  1628          *
       
  1629          * In terms of strictfp vs default-fp concerns related to
       
  1630          * overflow and underflow, since
       
  1631          *
       
  1632          * (Float.MAX_VALUE * Float.MAX_VALUE) << Double.MAX_VALUE
       
  1633          * (Float.MIN_VALUE * Float.MIN_VALUE) >> Double.MIN_VALUE
       
  1634          *
       
  1635          * neither the multiply nor add will overflow or underflow in
       
  1636          * double. Therefore, it is not necessary for this method to
       
  1637          * be declared strictfp to have reproducible
       
  1638          * behavior. However, it is necessary to explicitly store down
       
  1639          * to a float variable to avoid returning a value in the float
       
  1640          * extended value set.
       
  1641          */
       
  1642         float result = (float)(((double) a * (double) b ) + (double) c);
       
  1643         return result;
       
  1644     }
       
  1645 
       
  1646     /**
  1453      * Returns the size of an ulp of the argument.  An ulp, unit in
  1647      * Returns the size of an ulp of the argument.  An ulp, unit in
  1454      * the last place, of a {@code double} value is the positive
  1648      * the last place, of a {@code double} value is the positive
  1455      * distance between this floating-point value and the {@code
  1649      * distance between this floating-point value and the {@code
  1456      * double} value next larger in magnitude.  Note that for non-NaN
  1650      * double} value next larger in magnitude.  Note that for non-NaN
  1457      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
  1651      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.