< prev index next >

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

 ``` ``````1336 // if the signs are different and modulo not zero, adjust result 1337 if ((x ^ y) < 0 && mod != 0) { 1338 mod += y; 1339 } 1340 return mod; 1341 } 1342 1343 /** 1344 * Returns the absolute value of an {@code int} value. 1345 * If the argument is not negative, the argument is returned. 1346 * If the argument is negative, the negation of the argument is returned. 1347 * 1348 *

Note that if the argument is equal to the value of 1349 * {@link Integer#MIN_VALUE}, the most negative representable 1350 * {@code int} value, the result is that same value, which is 1351 * negative. 1352 * 1353 * @param a the argument whose absolute value is to be determined 1354 * @return the absolute value of the argument. 1355 */ 1356 public static int abs(int a) { 1357 return (a < 0) ? -a : a; 1358 } 1359 1360 /** 1361 * Returns the absolute value of a {@code long} value. 1362 * If the argument is not negative, the argument is returned. 1363 * If the argument is negative, the negation of the argument is returned. 1364 * 1365 *

Note that if the argument is equal to the value of 1366 * {@link Long#MIN_VALUE}, the most negative representable 1367 * {@code long} value, the result is that same value, which 1368 * is negative. 1369 * 1370 * @param a the argument whose absolute value is to be determined 1371 * @return the absolute value of the argument. 1372 */ 1373 public static long abs(long a) { 1374 return (a < 0) ? -a : a; 1375 } 1376 1377 /** 1378 * Returns the absolute value of a {@code float} value. 1379 * If the argument is not negative, the argument is returned. 1380 * If the argument is negative, the negation of the argument is returned. 1381 * Special cases: 1382 *

• If the argument is positive zero or negative zero, the 1383 * result is positive zero. 1384 *
• If the argument is infinite, the result is positive infinity. 1385 *
• If the argument is NaN, the result is NaN.
1386 * 1387 * @apiNote As implied by the above, one valid implementation of 1388 * this method is given by the expression below which computes a 1389 * {@code float} with the same exponent and significand as the 1390 * argument but with a guaranteed zero sign bit indicating a 1391 * positive value:
1392 * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))} 1393 * 1394 * @param a the argument whose absolute value is to be determined 1395 * @return the absolute value of the argument. 1396 */ 1397 public static float abs(float a) { 1398 return (a <= 0.0F) ? 0.0F - a : a; 1399 } 1400 1401 /** 1402 * Returns the absolute value of a {@code double} value. 1403 * If the argument is not negative, the argument is returned. 1404 * If the argument is negative, the negation of the argument is returned. 1405 * Special cases: 1406 *
• If the argument is positive zero or negative zero, the result 1407 * is positive zero. 1408 *
• If the argument is infinite, the result is positive infinity. 1409 *
• If the argument is NaN, the result is NaN.
1410 * 1411 * @apiNote As implied by the above, one valid implementation of 1412 * this method is given by the expression below which computes a 1413 * {@code double} with the same exponent and significand as the 1414 * argument but with a guaranteed zero sign bit indicating a 1415 * positive value:
1416 * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)} ``` ``` ``````1336 // if the signs are different and modulo not zero, adjust result 1337 if ((x ^ y) < 0 && mod != 0) { 1338 mod += y; 1339 } 1340 return mod; 1341 } 1342 1343 /** 1344 * Returns the absolute value of an {@code int} value. 1345 * If the argument is not negative, the argument is returned. 1346 * If the argument is negative, the negation of the argument is returned. 1347 * 1348 *

Note that if the argument is equal to the value of 1349 * {@link Integer#MIN_VALUE}, the most negative representable 1350 * {@code int} value, the result is that same value, which is 1351 * negative. 1352 * 1353 * @param a the argument whose absolute value is to be determined 1354 * @return the absolute value of the argument. 1355 */ 1356 @HotSpotIntrinsicCandidate 1357 public static int abs(int a) { 1358 return (a < 0) ? -a : a; 1359 } 1360 1361 /** 1362 * Returns the absolute value of a {@code long} value. 1363 * If the argument is not negative, the argument is returned. 1364 * If the argument is negative, the negation of the argument is returned. 1365 * 1366 *

Note that if the argument is equal to the value of 1367 * {@link Long#MIN_VALUE}, the most negative representable 1368 * {@code long} value, the result is that same value, which 1369 * is negative. 1370 * 1371 * @param a the argument whose absolute value is to be determined 1372 * @return the absolute value of the argument. 1373 */ 1374 @HotSpotIntrinsicCandidate 1375 public static long abs(long a) { 1376 return (a < 0) ? -a : a; 1377 } 1378 1379 /** 1380 * Returns the absolute value of a {@code float} value. 1381 * If the argument is not negative, the argument is returned. 1382 * If the argument is negative, the negation of the argument is returned. 1383 * Special cases: 1384 *

• If the argument is positive zero or negative zero, the 1385 * result is positive zero. 1386 *
• If the argument is infinite, the result is positive infinity. 1387 *
• If the argument is NaN, the result is NaN.
1388 * 1389 * @apiNote As implied by the above, one valid implementation of 1390 * this method is given by the expression below which computes a 1391 * {@code float} with the same exponent and significand as the 1392 * argument but with a guaranteed zero sign bit indicating a 1393 * positive value:
1394 * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))} 1395 * 1396 * @param a the argument whose absolute value is to be determined 1397 * @return the absolute value of the argument. 1398 */ 1399 @HotSpotIntrinsicCandidate 1400 public static float abs(float a) { 1401 return (a <= 0.0F) ? 0.0F - a : a; 1402 } 1403 1404 /** 1405 * Returns the absolute value of a {@code double} value. 1406 * If the argument is not negative, the argument is returned. 1407 * If the argument is negative, the negation of the argument is returned. 1408 * Special cases: 1409 *
• If the argument is positive zero or negative zero, the result 1410 * is positive zero. 1411 *
• If the argument is infinite, the result is positive infinity. 1412 *
• If the argument is NaN, the result is NaN.
1413 * 1414 * @apiNote As implied by the above, one valid implementation of 1415 * this method is given by the expression below which computes a 1416 * {@code double} with the same exponent and significand as the 1417 * argument but with a guaranteed zero sign bit indicating a 1418 * positive value:
1419 * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)} ```
< prev index next >