Skip to content

Commit 5d4ea9b

Browse files
committedJun 7, 2022
8273346: Expand library mappings to IEEE 754 operations
Reviewed-by: bpb
1 parent 96641c0 commit 5d4ea9b

File tree

4 files changed

+156
-13
lines changed

4 files changed

+156
-13
lines changed
 

‎src/java.base/share/classes/java/lang/Double.java

+27
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ public static double parseDouble(String s) throws NumberFormatException {
739739
* Returns {@code true} if the specified number is a
740740
* Not-a-Number (NaN) value, {@code false} otherwise.
741741
*
742+
* @apiNote
743+
* This method corresponds to the isNaN operation defined in IEEE
744+
* 754.
745+
*
742746
* @param v the value to be tested.
743747
* @return {@code true} if the value of the argument is NaN;
744748
* {@code false} otherwise.
@@ -751,6 +755,10 @@ public static boolean isNaN(double v) {
751755
* Returns {@code true} if the specified number is infinitely
752756
* large in magnitude, {@code false} otherwise.
753757
*
758+
* @apiNote
759+
* This method corresponds to the isInfinite operation defined in
760+
* IEEE 754.
761+
*
754762
* @param v the value to be tested.
755763
* @return {@code true} if the value of the argument is positive
756764
* infinity or negative infinity; {@code false} otherwise.
@@ -765,6 +773,10 @@ public static boolean isInfinite(double v) {
765773
* value; returns {@code false} otherwise (for NaN and infinity
766774
* arguments).
767775
*
776+
* @apiNote
777+
* This method corresponds to the isFinite operation defined in
778+
* IEEE 754.
779+
*
768780
* @param d the {@code double} value to be tested
769781
* @return {@code true} if the argument is a finite
770782
* floating-point value, {@code false} otherwise.
@@ -908,6 +920,10 @@ public long longValue() {
908920
* Returns the value of this {@code Double} as a {@code float}
909921
* after a narrowing primitive conversion.
910922
*
923+
* @apiNote
924+
* This method corresponds to the convertFormat operation defined
925+
* in IEEE 754.
926+
*
911927
* @return the {@code double} value represented by this object
912928
* converted to type {@code float}
913929
* @jls 5.1.3 Narrowing Primitive Conversion
@@ -1217,6 +1233,9 @@ public static int compare(double d1, double d2) {
12171233
/**
12181234
* Adds two {@code double} values together as per the + operator.
12191235
*
1236+
* @apiNote This method corresponds to the addition operation
1237+
* defined in IEEE 754.
1238+
*
12201239
* @param a the first operand
12211240
* @param b the second operand
12221241
* @return the sum of {@code a} and {@code b}
@@ -1232,6 +1251,10 @@ public static double sum(double a, double b) {
12321251
* Returns the greater of two {@code double} values
12331252
* as if by calling {@link Math#max(double, double) Math.max}.
12341253
*
1254+
* @apiNote
1255+
* This method corresponds to the maximum operation defined in
1256+
* IEEE 754.
1257+
*
12351258
* @param a the first operand
12361259
* @param b the second operand
12371260
* @return the greater of {@code a} and {@code b}
@@ -1246,6 +1269,10 @@ public static double max(double a, double b) {
12461269
* Returns the smaller of two {@code double} values
12471270
* as if by calling {@link Math#min(double, double) Math.min}.
12481271
*
1272+
* @apiNote
1273+
* This method corresponds to the minimum operation defined in
1274+
* IEEE 754.
1275+
*
12491276
* @param a the first operand
12501277
* @param b the second operand
12511278
* @return the smaller of {@code a} and {@code b}.

‎src/java.base/share/classes/java/lang/Float.java

+27
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ public static float parseFloat(String s) throws NumberFormatException {
559559
* Returns {@code true} if the specified number is a
560560
* Not-a-Number (NaN) value, {@code false} otherwise.
561561
*
562+
* @apiNote
563+
* This method corresponds to the isNaN operation defined in IEEE
564+
* 754.
565+
*
562566
* @param v the value to be tested.
563567
* @return {@code true} if the argument is NaN;
564568
* {@code false} otherwise.
@@ -571,6 +575,10 @@ public static boolean isNaN(float v) {
571575
* Returns {@code true} if the specified number is infinitely
572576
* large in magnitude, {@code false} otherwise.
573577
*
578+
* @apiNote
579+
* This method corresponds to the isInfinite operation defined in
580+
* IEEE 754.
581+
*
574582
* @param v the value to be tested.
575583
* @return {@code true} if the argument is positive infinity or
576584
* negative infinity; {@code false} otherwise.
@@ -586,6 +594,10 @@ public static boolean isInfinite(float v) {
586594
* value; returns {@code false} otherwise (for NaN and infinity
587595
* arguments).
588596
*
597+
* @apiNote
598+
* This method corresponds to the isFinite operation defined in
599+
* IEEE 754.
600+
*
589601
* @param f the {@code float} value to be tested
590602
* @return {@code true} if the argument is a finite
591603
* floating-point value, {@code false} otherwise.
@@ -754,6 +766,10 @@ public float floatValue() {
754766
* Returns the value of this {@code Float} as a {@code double}
755767
* after a widening primitive conversion.
756768
*
769+
* @apiNote
770+
* This method corresponds to the convertFormat operation defined
771+
* in IEEE 754.
772+
*
757773
* @return the {@code float} value represented by this
758774
* object converted to type {@code double}
759775
* @jls 5.1.2 Widening Primitive Conversion
@@ -1039,6 +1055,9 @@ public static int compare(float f1, float f2) {
10391055
/**
10401056
* Adds two {@code float} values together as per the + operator.
10411057
*
1058+
* @apiNote This method corresponds to the addition operation
1059+
* defined in IEEE 754.
1060+
*
10421061
* @param a the first operand
10431062
* @param b the second operand
10441063
* @return the sum of {@code a} and {@code b}
@@ -1054,6 +1073,10 @@ public static float sum(float a, float b) {
10541073
* Returns the greater of two {@code float} values
10551074
* as if by calling {@link Math#max(float, float) Math.max}.
10561075
*
1076+
* @apiNote
1077+
* This method corresponds to the maximum operation defined in
1078+
* IEEE 754.
1079+
*
10571080
* @param a the first operand
10581081
* @param b the second operand
10591082
* @return the greater of {@code a} and {@code b}
@@ -1068,6 +1091,10 @@ public static float max(float a, float b) {
10681091
* Returns the smaller of two {@code float} values
10691092
* as if by calling {@link Math#min(float, float) Math.min}.
10701093
*
1094+
* @apiNote
1095+
* This method corresponds to the minimum operation defined in
1096+
* IEEE 754.
1097+
*
10711098
* @param a the first operand
10721099
* @param b the second operand
10731100
* @return the smaller of {@code a} and {@code b}

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

+69-4
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ public static double log10(double a) {
398398
* Otherwise, the result is the {@code double} value closest to
399399
* the true mathematical square root of the argument value.
400400
*
401+
* @apiNote
402+
* This method corresponds to the squareRoot operation defined in
403+
* IEEE 754.
404+
*
401405
* @param a a value.
402406
* @return the positive square root of {@code a}.
403407
* If the argument is NaN or less than zero, the result is NaN.
@@ -481,6 +485,9 @@ public static double IEEEremainder(double f1, double f2) {
481485
* that the value of {@code Math.ceil(x)} is exactly the
482486
* value of {@code -Math.floor(-x)}.
483487
*
488+
* @apiNote
489+
* This method corresponds to the roundToIntegralTowardPositive
490+
* operation defined in IEEE 754.
484491
*
485492
* @param a a value.
486493
* @return the smallest (closest to negative infinity)
@@ -502,6 +509,10 @@ public static double ceil(double a) {
502509
* positive zero or negative zero, then the result is the same as
503510
* the argument.</ul>
504511
*
512+
* @apiNote
513+
* This method corresponds to the roundToIntegralTowardNegative
514+
* operation defined in IEEE 754.
515+
*
505516
* @param a a value.
506517
* @return the largest (closest to positive infinity)
507518
* floating-point value that less than or equal to the argument
@@ -523,6 +534,10 @@ public static double floor(double a) {
523534
* <li>If the argument is NaN or an infinity or positive zero or negative
524535
* zero, then the result is the same as the argument.</ul>
525536
*
537+
* @apiNote
538+
* This method corresponds to the roundToIntegralTiesToEven
539+
* operation defined in IEEE 754.
540+
*
526541
* @param a a {@code double} value.
527542
* @return the closest floating-point value to {@code a} that is
528543
* equal to a mathematical integer.
@@ -2033,6 +2048,10 @@ public static long max(long a, long b) {
20332048
* argument is positive zero and the other negative zero, the
20342049
* result is positive zero.
20352050
*
2051+
* @apiNote
2052+
* This method corresponds to the maximum operation defined in
2053+
* IEEE 754.
2054+
*
20362055
* @param a an argument.
20372056
* @param b another argument.
20382057
* @return the larger of {@code a} and {@code b}.
@@ -2060,6 +2079,10 @@ public static float max(float a, float b) {
20602079
* argument is positive zero and the other negative zero, the
20612080
* result is positive zero.
20622081
*
2082+
* @apiNote
2083+
* This method corresponds to the maximum operation defined in
2084+
* IEEE 754.
2085+
*
20632086
* @param a an argument.
20642087
* @param b another argument.
20652088
* @return the larger of {@code a} and {@code b}.
@@ -2116,6 +2139,10 @@ public static long min(long a, long b) {
21162139
* one argument is positive zero and the other is negative zero,
21172140
* the result is negative zero.
21182141
*
2142+
* @apiNote
2143+
* This method corresponds to the minimum operation defined in
2144+
* IEEE 754.
2145+
*
21192146
* @param a an argument.
21202147
* @param b another argument.
21212148
* @return the smaller of {@code a} and {@code b}.
@@ -2143,6 +2170,10 @@ public static float min(float a, float b) {
21432170
* argument is positive zero and the other is negative zero, the
21442171
* result is negative zero.
21452172
*
2173+
* @apiNote
2174+
* This method corresponds to the minimum operation defined in
2175+
* IEEE 754.
2176+
*
21462177
* @param a an argument.
21472178
* @param b another argument.
21482179
* @return the smaller of {@code a} and {@code b}.
@@ -2198,7 +2229,7 @@ public static double min(double a, double b) {
21982229
* equivalent to ({@code a * b}) however.
21992230
*
22002231
* @apiNote This method corresponds to the fusedMultiplyAdd
2201-
* operation defined in IEEE 754-2008.
2232+
* operation defined in IEEE 754.
22022233
*
22032234
* @param a a value
22042235
* @param b a value
@@ -2312,7 +2343,7 @@ public static double fma(double a, double b, double c) {
23122343
* equivalent to ({@code a * b}) however.
23132344
*
23142345
* @apiNote This method corresponds to the fusedMultiplyAdd
2315-
* operation defined in IEEE 754-2008.
2346+
* operation defined in IEEE 754.
23162347
*
23172348
* @param a a value
23182349
* @param b a value
@@ -2692,6 +2723,10 @@ public static double log1p(double x) {
26922723
* permitted to treat some NaN arguments as positive and other NaN
26932724
* arguments as negative to allow greater performance.
26942725
*
2726+
* @apiNote
2727+
* This method corresponds to the copySign operation defined in
2728+
* IEEE 754.
2729+
*
26952730
* @param magnitude the parameter providing the magnitude of the result
26962731
* @param sign the parameter providing the sign of the result
26972732
* @return a value with the magnitude of {@code magnitude}
@@ -2716,6 +2751,10 @@ public static double copySign(double magnitude, double sign) {
27162751
* permitted to treat some NaN arguments as positive and other NaN
27172752
* arguments as negative to allow greater performance.
27182753
*
2754+
* @apiNote
2755+
* This method corresponds to the copySign operation defined in
2756+
* IEEE 754.
2757+
*
27192758
* @param magnitude the parameter providing the magnitude of the result
27202759
* @param sign the parameter providing the sign of the result
27212760
* @return a value with the magnitude of {@code magnitude}
@@ -2739,8 +2778,12 @@ public static float copySign(float magnitude, float sign) {
27392778
* <li>If the argument is NaN or infinite, then the result is
27402779
* {@link Float#MAX_EXPONENT} + 1.
27412780
* <li>If the argument is zero or subnormal, then the result is
2742-
* {@link Float#MIN_EXPONENT} -1.
2781+
* {@link Float#MIN_EXPONENT} - 1.
27432782
* </ul>
2783+
* @apiNote
2784+
* This method is analogous to the logB operation defined in IEEE
2785+
* 754, but returns a different value on subnormal arguments.
2786+
*
27442787
* @param f a {@code float} value
27452788
* @return the unbiased exponent of the argument
27462789
* @since 1.6
@@ -2763,8 +2806,12 @@ public static int getExponent(float f) {
27632806
* <li>If the argument is NaN or infinite, then the result is
27642807
* {@link Double#MAX_EXPONENT} + 1.
27652808
* <li>If the argument is zero or subnormal, then the result is
2766-
* {@link Double#MIN_EXPONENT} -1.
2809+
* {@link Double#MIN_EXPONENT} - 1.
27672810
* </ul>
2811+
* @apiNote
2812+
* This method is analogous to the logB operation defined in IEEE
2813+
* 754, but returns a different value on subnormal arguments.
2814+
*
27682815
* @param d a {@code double} value
27692816
* @return the unbiased exponent of the argument
27702817
* @since 1.6
@@ -2968,6 +3015,9 @@ public static float nextAfter(float start, double direction) {
29683015
*
29693016
* </ul>
29703017
*
3018+
* @apiNote This method corresponds to the nextUp
3019+
* operation defined in IEEE 754.
3020+
*
29713021
* @param d starting floating-point value
29723022
* @return The adjacent floating-point value closer to positive
29733023
* infinity.
@@ -3004,6 +3054,9 @@ public static double nextUp(double d) {
30043054
*
30053055
* </ul>
30063056
*
3057+
* @apiNote This method corresponds to the nextUp
3058+
* operation defined in IEEE 754.
3059+
*
30073060
* @param f starting floating-point value
30083061
* @return The adjacent floating-point value closer to positive
30093062
* infinity.
@@ -3040,6 +3093,9 @@ public static float nextUp(float f) {
30403093
*
30413094
* </ul>
30423095
*
3096+
* @apiNote This method corresponds to the nextDown
3097+
* operation defined in IEEE 754.
3098+
*
30433099
* @param d starting floating-point value
30443100
* @return The adjacent floating-point value closer to negative
30453101
* infinity.
@@ -3077,6 +3133,9 @@ public static double nextDown(double d) {
30773133
*
30783134
* </ul>
30793135
*
3136+
* @apiNote This method corresponds to the nextDown
3137+
* operation defined in IEEE 754.
3138+
*
30803139
* @param f starting floating-point value
30813140
* @return The adjacent floating-point value closer to negative
30823141
* infinity.
@@ -3116,6 +3175,9 @@ public static float nextDown(float f) {
31163175
* sign is returned.
31173176
* </ul>
31183177
*
3178+
* @apiNote This method corresponds to the scaleB operation
3179+
* defined in IEEE 754.
3180+
*
31193181
* @param d number to be scaled by a power of two.
31203182
* @param scaleFactor power of 2 used to scale {@code d}
31213183
* @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
@@ -3200,6 +3262,9 @@ public static double scalb(double d, int scaleFactor) {
32003262
* sign is returned.
32013263
* </ul>
32023264
*
3265+
* @apiNote This method corresponds to the scaleB operation
3266+
* defined in IEEE 754.
3267+
*
32033268
* @param f number to be scaled by a power of two.
32043269
* @param scaleFactor power of 2 used to scale {@code f}
32053270
* @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>

‎src/java.base/share/classes/java/math/RoundingMode.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@
3939
* considered as a numerical value, the discarded fraction could have
4040
* an absolute value greater than one.
4141
*
42+
* <p>More generally, a rounding policy defines a mapping from the
43+
* real numbers to a subset of representable values. In the case of
44+
* {@link BigDecimal}, the representable values are a function of the
45+
* {@linkplain MathContext#getPrecision() precision} being used in the
46+
* computation. Assuming the mathematical result is within the
47+
* exponent range of {@code BigDecimal}, the mathematical result will
48+
* be exactly representable in the result precision or will fall
49+
* between two adjacent representable values. In the case of falling
50+
* between two representable values, the rounding policy determines
51+
* which of those two bracketing values is the result. For in-range
52+
* real numbers, for a given set of representable values, a rounding
53+
* policy maps a continuous segment of the real number line to a
54+
* single representable value where the real number numerically equal
55+
* to a representable value is mapped to that value.
56+
*
4257
* <p>Each rounding mode description includes a table listing how
4358
* different two-digit decimal values would round to a one digit
4459
* decimal value under the rounding mode in question. The result
@@ -90,14 +105,15 @@
90105
* @apiNote
91106
* Five of the rounding modes declared in this class correspond to
92107
* rounding-direction attributes defined in the <cite>IEEE Standard
93-
* for Floating-Point Arithmetic</cite>, IEEE 754-2019. Where present,
108+
* for Floating-Point Arithmetic</cite>. Where present,
94109
* this correspondence will be noted in the documentation of the
95110
* particular constant.
96111
*
97112
* @see BigDecimal
98113
* @see MathContext
99114
* @see <a href="https://standards.ieee.org/ieee/754/6210/">
100115
* <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
116+
* @jls 15.4 Floating-point Expressions
101117
*
102118
* @author Josh Bloch
103119
* @author Mike Cowlishaw
@@ -140,7 +156,13 @@ public enum RoundingMode {
140156
* Rounding mode to round towards zero. Never increments the digit
141157
* prior to a discarded fraction (i.e., truncates). Note that this
142158
* rounding mode never increases the magnitude of the calculated value.
143-
* This mode corresponds to the IEEE 754-2019 rounding-direction
159+
*
160+
* @apiNote
161+
* This rounding mode is analogous to the rounding policy used
162+
* for the {@code float} and {@code double} operators
163+
* remainder and conversion to an integer value (JLS {@jls
164+
* 15.4}).
165+
* This mode corresponds to the IEEE 754 rounding-direction
144166
* attribute roundTowardZero.
145167
*
146168
*<p>Example:
@@ -171,7 +193,7 @@ public enum RoundingMode {
171193
* result is positive, behaves as for {@code RoundingMode.UP};
172194
* if negative, behaves as for {@code RoundingMode.DOWN}. Note
173195
* that this rounding mode never decreases the calculated value.
174-
* This mode corresponds to the IEEE 754-2019 rounding-direction
196+
* This mode corresponds to the IEEE 754 rounding-direction
175197
* attribute roundTowardPositive.
176198
*
177199
*<p>Example:
@@ -202,7 +224,7 @@ public enum RoundingMode {
202224
* result is positive, behave as for {@code RoundingMode.DOWN};
203225
* if negative, behave as for {@code RoundingMode.UP}. Note that
204226
* this rounding mode never increases the calculated value.
205-
* This mode corresponds to the IEEE 754-2019 rounding-direction
227+
* This mode corresponds to the IEEE 754 rounding-direction
206228
* attribute roundTowardNegative.
207229
*
208230
*<p>Example:
@@ -235,7 +257,7 @@ public enum RoundingMode {
235257
* fraction is &ge; 0.5; otherwise, behaves as for
236258
* {@code RoundingMode.DOWN}. Note that this is the rounding
237259
* mode commonly taught at school.
238-
* This mode corresponds to the IEEE 754-2019 rounding-direction
260+
* This mode corresponds to the IEEE 754 rounding-direction
239261
* attribute roundTiesToAway.
240262
*
241263
*<p>Example:
@@ -297,14 +319,16 @@ public enum RoundingMode {
297319
* towards the even neighbor. Behaves as for
298320
* {@code RoundingMode.HALF_UP} if the digit to the left of the
299321
* discarded fraction is odd; behaves as for
300-
* {@code RoundingMode.HALF_DOWN} if it's even. Note that this
322+
* {@code RoundingMode.HALF_DOWN} if it's even.
323+
* @apiNote
324+
* This
301325
* is the rounding mode that statistically minimizes cumulative
302326
* error when applied repeatedly over a sequence of calculations.
303327
* It is sometimes known as {@literal "Banker's rounding,"} and is
304328
* chiefly used in the USA. This rounding mode is analogous to
305-
* the rounding policy used for {@code float} and {@code double}
306-
* arithmetic in Java.
307-
* This mode corresponds to the IEEE 754-2019 rounding-direction
329+
* the rounding policy used for most {@code float} and {@code double}
330+
* arithmetic operators in Java (JLS {@jls 15.4}).
331+
* This mode corresponds to the IEEE 754 rounding-direction
308332
* attribute roundTiesToEven.
309333
*
310334
*<p>Example:

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jun 7, 2022

@openjdk-notifier[bot]
Please sign in to comment.