< prev index next >

src/java.base/share/classes/java/lang/Math.java

Print this page




1342      * @since 1.8
1343      */
1344     public static long floorMod(long x, long y) {
1345         long mod = x % y;
1346         // if the signs are different and modulo not zero, adjust result
1347         if ((x ^ y) < 0 && mod != 0) {
1348             mod += y;
1349         }
1350         return mod;
1351     }
1352 
1353     /**
1354      * Returns the absolute value of an {@code int} value.
1355      * If the argument is not negative, the argument is returned.
1356      * If the argument is negative, the negation of the argument is returned.
1357      *
1358      * <p>Note that if the argument is equal to the value of
1359      * {@link Integer#MIN_VALUE}, the most negative representable
1360      * {@code int} value, the result is that same value, which is
1361      * negative.


1362      *
1363      * @param   a   the argument whose absolute value is to be determined
1364      * @return  the absolute value of the argument.

1365      */
1366     @HotSpotIntrinsicCandidate
1367     public static int abs(int a) {
1368         return (a < 0) ? -a : a;
1369     }
1370 
1371     /**


























1372      * Returns the absolute value of a {@code long} value.
1373      * If the argument is not negative, the argument is returned.
1374      * If the argument is negative, the negation of the argument is returned.
1375      *
1376      * <p>Note that if the argument is equal to the value of
1377      * {@link Long#MIN_VALUE}, the most negative representable
1378      * {@code long} value, the result is that same value, which
1379      * is negative.


1380      *
1381      * @param   a   the argument whose absolute value is to be determined
1382      * @return  the absolute value of the argument.
1383      */
1384     @HotSpotIntrinsicCandidate
1385     public static long abs(long a) {
1386         return (a < 0) ? -a : a;


























1387     }
1388 
1389     /**
1390      * Returns the absolute value of a {@code float} value.
1391      * If the argument is not negative, the argument is returned.
1392      * If the argument is negative, the negation of the argument is returned.
1393      * Special cases:
1394      * <ul><li>If the argument is positive zero or negative zero, the
1395      * result is positive zero.
1396      * <li>If the argument is infinite, the result is positive infinity.
1397      * <li>If the argument is NaN, the result is NaN.</ul>
1398      *
1399      * @apiNote As implied by the above, one valid implementation of
1400      * this method is given by the expression below which computes a
1401      * {@code float} with the same exponent and significand as the
1402      * argument but with a guaranteed zero sign bit indicating a
1403      * positive value:<br>
1404      * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))}
1405      *
1406      * @param   a   the argument whose absolute value is to be determined




1342      * @since 1.8
1343      */
1344     public static long floorMod(long x, long y) {
1345         long mod = x % y;
1346         // if the signs are different and modulo not zero, adjust result
1347         if ((x ^ y) < 0 && mod != 0) {
1348             mod += y;
1349         }
1350         return mod;
1351     }
1352 
1353     /**
1354      * Returns the absolute value of an {@code int} value.
1355      * If the argument is not negative, the argument is returned.
1356      * If the argument is negative, the negation of the argument is returned.
1357      *
1358      * <p>Note that if the argument is equal to the value of
1359      * {@link Integer#MIN_VALUE}, the most negative representable
1360      * {@code int} value, the result is that same value, which is
1361      * negative.
1362      * In contrast, the {@link Math#absExact(int)} method throws an
1363      * {@code ArithmeticException} for this value.
1364      *
1365      * @param   a   the argument whose absolute value is to be determined
1366      * @return  the absolute value of the argument.
1367      * @see Math#absExact(int)
1368      */
1369     @HotSpotIntrinsicCandidate
1370     public static int abs(int a) {
1371         return (a < 0) ? -a : a;
1372     }
1373 
1374     /**
1375      * Returns the mathematical absolute value of an {@code int} value
1376      * if it is exactly representable as an {@code int}, throwing
1377      * {@code ArithmeticException} if the result overflows the
1378      * positive {@code int} range.
1379      *
1380      * <p>Since the range of two's complement integers is asymmetric
1381      * with one additional negative value (JLS {@jls 4.2.1}), the
1382      * mathematical absolute value of {@link Integer#MIN_VALUE}
1383      * overflows the positive {@code int} range, so an exception is
1384      * thrown for that argument.
1385      *
1386      * @param  a  the argument whose absolute value is to be determined
1387      * @return the absolute value of the argument, unless overflow occurs
1388      * @throws ArithmeticException if the argument is {@link Integer#MIN_VALUE}
1389      * @see Math#abs(int)
1390      * @since 15
1391      */
1392     public static int absExact(int a) {
1393         if (a == Integer.MIN_VALUE)
1394             throw new ArithmeticException(
1395                 "Overflow to represent absolute value of Integer.MIN_VALUE");
1396         else
1397             return abs(a);
1398     }
1399 
1400     /**
1401      * Returns the absolute value of a {@code long} value.
1402      * If the argument is not negative, the argument is returned.
1403      * If the argument is negative, the negation of the argument is returned.
1404      *
1405      * <p>Note that if the argument is equal to the value of
1406      * {@link Long#MIN_VALUE}, the most negative representable
1407      * {@code long} value, the result is that same value, which
1408      * is negative.
1409      * In contrast, the {@link Math#absExact(long)} method throws an
1410      * {@code ArithmeticException} for this value.
1411      *
1412      * @param   a   the argument whose absolute value is to be determined
1413      * @return  the absolute value of the argument.
1414      */
1415     @HotSpotIntrinsicCandidate
1416     public static long abs(long a) {
1417         return (a < 0) ? -a : a;
1418     }
1419 
1420     /**
1421      * Returns the mathematical absolute value of an {@code long} value
1422      * if it is exactly representable as an {@code long}, throwing
1423      * {@code ArithmeticException} if the result overflows the
1424      * positive {@code long} range.
1425      *
1426      * <p>Since the range of two's complement integers is asymmetric
1427      * with one additional negative value (JLS {@jls 4.2.1}), the
1428      * mathematical absolute value of {@link Long#MIN_VALUE} overflows
1429      * the positive {@code long} range, so an exception is thrown for
1430      * that argument.
1431      *
1432      * @param  a  the argument whose absolute value is to be determined
1433      * @return the absolute value of the argument, unless overflow occurs
1434      * @throws ArithmeticException if the argument is {@link Long#MIN_VALUE}
1435      * @see Math#abs(long)
1436      * @since 15
1437      */
1438     public static long absExact(long a) {
1439         if (a == Long.MIN_VALUE)
1440             throw new ArithmeticException(
1441                 "Overflow to represent absolute value of Long.MIN_VALUE");
1442         else
1443             return abs(a);
1444     }
1445 
1446     /**
1447      * Returns the absolute value of a {@code float} value.
1448      * If the argument is not negative, the argument is returned.
1449      * If the argument is negative, the negation of the argument is returned.
1450      * Special cases:
1451      * <ul><li>If the argument is positive zero or negative zero, the
1452      * result is positive zero.
1453      * <li>If the argument is infinite, the result is positive infinity.
1454      * <li>If the argument is NaN, the result is NaN.</ul>
1455      *
1456      * @apiNote As implied by the above, one valid implementation of
1457      * this method is given by the expression below which computes a
1458      * {@code float} with the same exponent and significand as the
1459      * argument but with a guaranteed zero sign bit indicating a
1460      * positive value:<br>
1461      * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))}
1462      *
1463      * @param   a   the argument whose absolute value is to be determined


< prev index next >