Skip to content

Commit 9f1edaf

Browse files
rgiuliettiBrian Burkhalter
authored andcommitted
8271599: Javadoc of floorDiv() and floorMod() families is inaccurate in some places
Reviewed-by: darcy, bpb
1 parent 452f7d7 commit 9f1edaf

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

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

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ public static long unsignedMultiplyHigh(long x, long y) {
12471247
/**
12481248
* Returns the largest (closest to positive infinity)
12491249
* {@code int} value that is less than or equal to the algebraic quotient.
1250-
* There is one special case, if the dividend is the
1250+
* There is one special case: if the dividend is
12511251
* {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
12521252
* then integer overflow occurs and
12531253
* the result is equal to {@code Integer.MIN_VALUE}.
@@ -1256,14 +1256,16 @@ public static long unsignedMultiplyHigh(long x, long y) {
12561256
* (truncation). This operation instead acts under the round toward
12571257
* negative infinity (floor) rounding mode.
12581258
* The floor rounding mode gives different results from truncation
1259-
* when the exact result is negative.
1259+
* when the exact quotient is not an integer and is negative.
12601260
* <ul>
12611261
* <li>If the signs of the arguments are the same, the results of
12621262
* {@code floorDiv} and the {@code /} operator are the same. <br>
12631263
* For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1264-
* <li>If the signs of the arguments are different, the quotient is negative and
1265-
* {@code floorDiv} returns the integer less than or equal to the quotient
1266-
* and the {@code /} operator returns the integer closest to zero.<br>
1264+
* <li>If the signs of the arguments are different, {@code floorDiv}
1265+
* returns the largest integer less than or equal to the quotient
1266+
* while the {@code /} operator returns the smallest integer greater
1267+
* than or equal to the quotient.
1268+
* They differ if and only if the quotient is not an integer.<br>
12671269
* For example, {@code floorDiv(-4, 3) == -2},
12681270
* whereas {@code (-4 / 3) == -1}.
12691271
* </li>
@@ -1290,7 +1292,7 @@ public static int floorDiv(int x, int y) {
12901292
/**
12911293
* Returns the largest (closest to positive infinity)
12921294
* {@code long} value that is less than or equal to the algebraic quotient.
1293-
* There is one special case, if the dividend is the
1295+
* There is one special case: if the dividend is
12941296
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
12951297
* then integer overflow occurs and
12961298
* the result is equal to {@code Long.MIN_VALUE}.
@@ -1299,7 +1301,7 @@ public static int floorDiv(int x, int y) {
12991301
* (truncation). This operation instead acts under the round toward
13001302
* negative infinity (floor) rounding mode.
13011303
* The floor rounding mode gives different results from truncation
1302-
* when the exact result is negative.
1304+
* when the exact result is not an integer and is negative.
13031305
* <p>
13041306
* For examples, see {@link #floorDiv(int, int)}.
13051307
*
@@ -1319,7 +1321,7 @@ public static long floorDiv(long x, int y) {
13191321
/**
13201322
* Returns the largest (closest to positive infinity)
13211323
* {@code long} value that is less than or equal to the algebraic quotient.
1322-
* There is one special case, if the dividend is the
1324+
* There is one special case: if the dividend is
13231325
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
13241326
* then integer overflow occurs and
13251327
* the result is equal to {@code Long.MIN_VALUE}.
@@ -1328,7 +1330,7 @@ public static long floorDiv(long x, int y) {
13281330
* (truncation). This operation instead acts under the round toward
13291331
* negative infinity (floor) rounding mode.
13301332
* The floor rounding mode gives different results from truncation
1331-
* when the exact result is negative.
1333+
* when the exact result is not an integer and is negative.
13321334
* <p>
13331335
* For examples, see {@link #floorDiv(int, int)}.
13341336
*
@@ -1353,40 +1355,35 @@ public static long floorDiv(long x, long y) {
13531355
/**
13541356
* Returns the floor modulus of the {@code int} arguments.
13551357
* <p>
1356-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1357-
* has the same sign as the divisor {@code y}, and
1358+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1359+
* has the same sign as the divisor {@code y} or is zero, and
13581360
* is in the range of {@code -abs(y) < r < +abs(y)}.
13591361
*
13601362
* <p>
13611363
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
13621364
* <ul>
1363-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1365+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
13641366
* </ul>
13651367
* <p>
1366-
* The difference in values between {@code floorMod} and
1367-
* the {@code %} operator is due to the difference between
1368-
* {@code floorDiv} that returns the integer less than or equal to the quotient
1369-
* and the {@code /} operator that returns the integer closest to zero.
1368+
* The difference in values between {@code floorMod} and the {@code %} operator
1369+
* is due to the difference between {@code floorDiv} and the {@code /}
1370+
* operator, as detailed in {@linkplain #floorDiv(int, int)}.
13701371
* <p>
13711372
* Examples:
13721373
* <ul>
1373-
* <li>If the signs of the arguments are the same, the results
1374-
* of {@code floorMod} and the {@code %} operator are the same.<br>
1374+
* <li>Regardless of the signs of the arguments, {@code floorMod}(x, y)
1375+
* is zero exactly when {@code x % y} is zero as well.</li>
1376+
* <li>If neither of {@code floorMod}(x, y) or {@code x % y} is zero,
1377+
* their results differ exactly when the signs of the arguments differ.<br>
13751378
* <ul>
13761379
* <li>{@code floorMod(+4, +3) == +1}; &nbsp; and {@code (+4 % +3) == +1}</li>
13771380
* <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1}</li>
1378-
* </ul>
1379-
* <li>If the signs of the arguments are different, the results
1380-
* differ from the {@code %} operator.<br>
1381-
* <ul>
13821381
* <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1}</li>
13831382
* <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1}</li>
13841383
* </ul>
13851384
* </li>
13861385
* </ul>
13871386
* <p>
1388-
* If the signs of arguments are unknown and a positive modulus
1389-
* is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
13901387
*
13911388
* @param x the dividend
13921389
* @param y the divisor
@@ -1407,14 +1404,14 @@ public static int floorMod(int x, int y) {
14071404
/**
14081405
* Returns the floor modulus of the {@code long} and {@code int} arguments.
14091406
* <p>
1410-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1411-
* has the same sign as the divisor {@code y}, and
1407+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1408+
* has the same sign as the divisor {@code y} or is zero, and
14121409
* is in the range of {@code -abs(y) < r < +abs(y)}.
14131410
*
14141411
* <p>
14151412
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
14161413
* <ul>
1417-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1414+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
14181415
* </ul>
14191416
* <p>
14201417
* For examples, see {@link #floorMod(int, int)}.
@@ -1434,14 +1431,14 @@ public static int floorMod(long x, int y) {
14341431
/**
14351432
* Returns the floor modulus of the {@code long} arguments.
14361433
* <p>
1437-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1438-
* has the same sign as the divisor {@code y}, and
1434+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1435+
* has the same sign as the divisor {@code y} or is zero, and
14391436
* is in the range of {@code -abs(y) < r < +abs(y)}.
14401437
*
14411438
* <p>
14421439
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
14431440
* <ul>
1444-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1441+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
14451442
* </ul>
14461443
* <p>
14471444
* For examples, see {@link #floorMod(int, int)}.

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,10 @@ public static long unsignedMultiplyHigh(long x, long y) {
10511051
/**
10521052
* Returns the largest (closest to positive infinity)
10531053
* {@code int} value that is less than or equal to the algebraic quotient.
1054-
* There is one special case, if the dividend is the
1054+
* There is one special case: if the dividend is
10551055
* {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
10561056
* then integer overflow occurs and
1057-
* the result is equal to the {@code Integer.MIN_VALUE}.
1057+
* the result is equal to {@code Integer.MIN_VALUE}.
10581058
* <p>
10591059
* See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
10601060
* a comparison to the integer division {@code /} operator.
@@ -1075,7 +1075,7 @@ public static int floorDiv(int x, int y) {
10751075
/**
10761076
* Returns the largest (closest to positive infinity)
10771077
* {@code long} value that is less than or equal to the algebraic quotient.
1078-
* There is one special case, if the dividend is the
1078+
* There is one special case: if the dividend is
10791079
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
10801080
* then integer overflow occurs and
10811081
* the result is equal to {@code Long.MIN_VALUE}.
@@ -1099,10 +1099,10 @@ public static long floorDiv(long x, int y) {
10991099
/**
11001100
* Returns the largest (closest to positive infinity)
11011101
* {@code long} value that is less than or equal to the algebraic quotient.
1102-
* There is one special case, if the dividend is the
1102+
* There is one special case: if the dividend is
11031103
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
11041104
* then integer overflow occurs and
1105-
* the result is equal to the {@code Long.MIN_VALUE}.
1105+
* the result is equal to {@code Long.MIN_VALUE}.
11061106
* <p>
11071107
* See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
11081108
* a comparison to the integer division {@code /} operator.
@@ -1123,13 +1123,14 @@ public static long floorDiv(long x, long y) {
11231123
/**
11241124
* Returns the floor modulus of the {@code int} arguments.
11251125
* <p>
1126-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1127-
* has the same sign as the divisor {@code y}, and
1126+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1127+
* has the same sign as the divisor {@code y} or is zero, and
11281128
* is in the range of {@code -abs(y) < r < +abs(y)}.
1129+
*
11291130
* <p>
11301131
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
11311132
* <ul>
1132-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1133+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
11331134
* </ul>
11341135
* <p>
11351136
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and
@@ -1150,14 +1151,14 @@ public static int floorMod(int x, int y) {
11501151
/**
11511152
* Returns the floor modulus of the {@code long} and {@code int} arguments.
11521153
* <p>
1153-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1154-
* has the same sign as the divisor {@code y}, and
1154+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1155+
* has the same sign as the divisor {@code y} or is zero, and
11551156
* is in the range of {@code -abs(y) < r < +abs(y)}.
11561157
*
11571158
* <p>
11581159
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
11591160
* <ul>
1160-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1161+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
11611162
* </ul>
11621163
* <p>
11631164
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and
@@ -1178,13 +1179,14 @@ public static int floorMod(long x, int y) {
11781179
/**
11791180
* Returns the floor modulus of the {@code long} arguments.
11801181
* <p>
1181-
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
1182-
* has the same sign as the divisor {@code y}, and
1182+
* The floor modulus is {@code r = x - (floorDiv(x, y) * y)},
1183+
* has the same sign as the divisor {@code y} or is zero, and
11831184
* is in the range of {@code -abs(y) < r < +abs(y)}.
1185+
*
11841186
* <p>
11851187
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
11861188
* <ul>
1187-
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1189+
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}</li>
11881190
* </ul>
11891191
* <p>
11901192
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and

0 commit comments

Comments
 (0)