forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrictMath.java
2231 lines (2087 loc) · 83.4 KB
/
StrictMath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.math.DoubleConsts;
import java.util.Random;
/**
* The class {@code StrictMath} contains methods for performing basic
* numeric operations such as the elementary exponential, logarithm,
* square root, and trigonometric functions.
*
* <p>To help ensure portability of Java programs, the definitions of
* some of the numeric functions in this package require that they
* produce the same results as certain published algorithms. These
* algorithms are available from the well-known network library
* {@code netlib} as the package "Freely Distributable Math
* Library," <a
* href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
* algorithms, which are written in the C programming language, are
* then to be understood as executed with all floating-point
* operations following the rules of Java floating-point arithmetic.
*
* <p>The Java math library is defined with respect to
* {@code fdlibm} version 5.3. Where {@code fdlibm} provides
* more than one definition for a function (such as
* {@code acos}), use the "IEEE 754 core function" version
* (residing in a file whose name begins with the letter
* {@code e}). The methods which require {@code fdlibm}
* semantics are {@code sin}, {@code cos}, {@code tan},
* {@code asin}, {@code acos}, {@code atan},
* {@code exp}, {@code log}, {@code log10},
* {@code cbrt}, {@code atan2}, {@code pow},
* {@code sinh}, {@code cosh}, {@code tanh},
* {@code hypot}, {@code expm1}, and {@code log1p}.
*
* <p>
* The platform uses signed two's complement integer arithmetic with
* int and long primitive types. The developer should choose
* the primitive type to ensure that arithmetic operations consistently
* produce correct results, which in some cases means the operations
* will not overflow the range of values of the computation.
* The best practice is to choose the primitive type and algorithm to avoid
* overflow. In cases where the size is {@code int} or {@code long} and
* overflow errors need to be detected, the methods {@code addExact},
* {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
* throw an {@code ArithmeticException} when the results overflow.
* For other arithmetic operations such as divide, absolute value,
* increment by one, decrement by one, and negation overflow occurs only with
* a specific minimum or maximum value and should be checked against
* the minimum or maximum as appropriate.
*
* @author unascribed
* @author Joseph D. Darcy
* @since 1.3
*/
// 数学运算类,相当于一个严格版本的Math类,要求同样的运算在不同平台上结果一致
public final class StrictMath {
/**
* The {@code double} value that is closer than any other to
* <i>e</i>, the base of the natural logarithms.
*/
public static final double E = 2.7182818284590452354;
/**
* The {@code double} value that is closer than any other to
* <i>pi</i>, the ratio of the circumference of a circle to its
* diameter.
*/
public static final double PI = 3.14159265358979323846;
/**
* Constant by which to multiply an angular value in degrees to obtain an angular value in radians.
*/
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
/**
* Constant by which to multiply an angular value in radians to obtain an angular value in degrees.
*/
private static final double RADIANS_TO_DEGREES = 57.29577951308232;
/**
* Don't let anyone instantiate this class.
*/
private StrictMath() {
}
/*▼ 基本运算 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the sum of its arguments,
* throwing an exception if the result overflows an {@code int}.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @throws ArithmeticException if the result overflows an int
* @see Math#addExact(int, int)
* @since 1.8
*/
// 加法
public static int addExact(int x, int y) {
return Math.addExact(x, y);
}
/**
* Returns the sum of its arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @throws ArithmeticException if the result overflows a long
* @see Math#addExact(long, long)
* @since 1.8
*/
// 加法
public static long addExact(long x, long y) {
return Math.addExact(x, y);
}
/**
* Returns the difference of the arguments,
* throwing an exception if the result overflows an {@code int}.
*
* @param x the first value
* @param y the second value to subtract from the first
*
* @return the result
*
* @throws ArithmeticException if the result overflows an int
* @see Math#subtractExact(int, int)
* @since 1.8
*/
// 减法
public static int subtractExact(int x, int y) {
return Math.subtractExact(x, y);
}
/**
* Returns the difference of the arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value to subtract from the first
*
* @return the result
*
* @throws ArithmeticException if the result overflows a long
* @see Math#subtractExact(long, long)
* @since 1.8
*/
// 减法
public static long subtractExact(long x, long y) {
return Math.subtractExact(x, y);
}
/**
* Returns the product of the arguments,
* throwing an exception if the result overflows an {@code int}.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @throws ArithmeticException if the result overflows an int
* @see Math#multiplyExact(int, int)
* @since 1.8
*/
// 乘法
public static int multiplyExact(int x, int y) {
return Math.multiplyExact(x, y);
}
/**
* Returns the product of the arguments, throwing an exception if the result
* overflows a {@code long}.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @throws ArithmeticException if the result overflows a long
* @see Math#multiplyExact(long, int)
* @since 9
*/
// 乘法
public static long multiplyExact(long x, int y) {
return Math.multiplyExact(x, y);
}
/**
* Returns the product of the arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @throws ArithmeticException if the result overflows a long
* @see Math#multiplyExact(long, long)
* @since 1.8
*/
// 乘法
public static long multiplyExact(long x, long y) {
return Math.multiplyExact(x, y);
}
/**
* Returns the exact mathematical product of the arguments.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @see Math#multiplyFull(int, int)
* @since 9
*/
// 乘法
public static long multiplyFull(int x, int y) {
return Math.multiplyFull(x, y);
}
/**
* Returns as a {@code long} the most significant 64 bits of the 128-bit
* product of two 64-bit factors.
*
* @param x the first value
* @param y the second value
*
* @return the result
*
* @see Math#multiplyHigh(long, long)
* @since 9
*/
// 乘法,返回两个long乘积的高64位
public static long multiplyHigh(long x, long y) {
return Math.multiplyHigh(x, y);
}
/**
* Returns the largest (closest to positive infinity)
* {@code int} value that is less than or equal to the algebraic quotient.
* There is one special case, if the dividend is the
* {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to the {@code Integer.MIN_VALUE}.
* <p>
* See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the largest (closest to positive infinity)
* {@code int} value that is less than or equal to the algebraic quotient.
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorDiv(int, int)
* @see Math#floor(double)
* @since 1.8
*/
// 除法,如果两数符号不同,则向下取整
public static int floorDiv(int x, int y) {
return Math.floorDiv(x, y);
}
/**
* Returns the largest (closest to positive infinity)
* {@code long} value that is less than or equal to the algebraic quotient.
* There is one special case, if the dividend is the
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to {@code Long.MIN_VALUE}.
* <p>
* See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the largest (closest to positive infinity)
* {@code int} value that is less than or equal to the algebraic quotient.
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorDiv(long, int)
* @see Math#floor(double)
* @since 9
*/
// 除法,如果两数符号不同,则向下取整
public static long floorDiv(long x, int y) {
return Math.floorDiv(x, y);
}
/**
* Returns the largest (closest to positive infinity)
* {@code long} value that is less than or equal to the algebraic quotient.
* There is one special case, if the dividend is the
* {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
* then integer overflow occurs and
* the result is equal to the {@code Long.MIN_VALUE}.
* <p>
* See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and
* a comparison to the integer division {@code /} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the largest (closest to positive infinity)
* {@code long} value that is less than or equal to the algebraic quotient.
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorDiv(long, long)
* @see Math#floor(double)
* @since 1.8
*/
// 除法,如果两数符号不同,则向下取整
public static long floorDiv(long x, long y) {
return Math.floorDiv(x, y);
}
/**
* Returns the floor modulus of the {@code int} arguments.
* <p>
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
* has the same sign as the divisor {@code y}, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
* <p>
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
* <ul>
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
* </ul>
* <p>
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the floor modulus {@code x - (floorDiv(x, y) * y)}
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorMod(int, int)
* @see StrictMath#floorDiv(int, int)
* @since 1.8
*/
// 取余,相当于(x % y + y) % y
public static int floorMod(int x, int y) {
return Math.floorMod(x, y);
}
/**
* Returns the floor modulus of the {@code long} and {@code int} arguments.
* <p>
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
* has the same sign as the divisor {@code y}, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
*
* <p>
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
* <ul>
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
* </ul>
* <p>
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the floor modulus {@code x - (floorDiv(x, y) * y)}
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorMod(long, int)
* @see StrictMath#floorDiv(long, int)
* @since 9
*/
// 取余,相当于(x % y + y) % y
public static int floorMod(long x, int y) {
return Math.floorMod(x, y);
}
/**
* Returns the floor modulus of the {@code long} arguments.
* <p>
* The floor modulus is {@code x - (floorDiv(x, y) * y)},
* has the same sign as the divisor {@code y}, and
* is in the range of {@code -abs(y) < r < +abs(y)}.
* <p>
* The relationship between {@code floorDiv} and {@code floorMod} is such that:
* <ul>
* <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
* </ul>
* <p>
* See {@link Math#floorMod(int, int) Math.floorMod} for examples and
* a comparison to the {@code %} operator.
*
* @param x the dividend
* @param y the divisor
*
* @return the floor modulus {@code x - (floorDiv(x, y) * y)}
*
* @throws ArithmeticException if the divisor {@code y} is zero
* @see Math#floorMod(long, long)
* @see StrictMath#floorDiv(long, long)
* @since 1.8
*/
// 取余,相当于(x % y + y) % y
public static long floorMod(long x, long y) {
return Math.floorMod(x, y);
}
/**
* Computes the remainder operation on two arguments as prescribed
* by the IEEE 754 standard.
* The remainder value is mathematically equal to
* <code>f1 - f2</code> × <i>n</i>,
* where <i>n</i> is the mathematical integer closest to the exact
* mathematical value of the quotient {@code f1/f2}, and if two
* mathematical integers are equally close to {@code f1/f2},
* then <i>n</i> is the integer that is even. If the remainder is
* zero, its sign is the same as the sign of the first argument.
* Special cases:
* <ul><li>If either argument is NaN, or the first argument is infinite,
* or the second argument is positive zero or negative zero, then the
* result is NaN.
* <li>If the first argument is finite and the second argument is
* infinite, then the result is the same as the first argument.</ul>
*
* @param f1 the dividend.
* @param f2 the divisor.
*
* @return the remainder when {@code f1} is divided by
* {@code f2}.
*/
// 取余,返回精确的余数
public static native double IEEEremainder(double f1, double f2);
/**
* Returns the fused multiply add of the three arguments; that is,
* returns the exact product of the first two arguments summed
* with the third argument and then rounded once to the nearest
* {@code double}.
*
* The rounding is done using the {@linkplain
* java.math.RoundingMode#HALF_EVEN round to nearest even
* rounding mode}.
*
* In contrast, if {@code a * b + c} is evaluated as a regular
* floating-point expression, two rounding errors are involved,
* the first for the multiply operation, the second for the
* addition operation.
*
* <p>Special cases:
* <ul>
* <li> If any argument is NaN, the result is NaN.
*
* <li> If one of the first two arguments is infinite and the
* other is zero, the result is NaN.
*
* <li> If the exact product of the first two arguments is infinite
* (in other words, at least one of the arguments is infinite and
* the other is neither zero nor NaN) and the third argument is an
* infinity of the opposite sign, the result is NaN.
*
* </ul>
*
* <p>Note that {@code fusedMac(a, 1.0, c)} returns the same
* result as ({@code a + c}). However,
* {@code fusedMac(a, b, +0.0)} does <em>not</em> always return the
* same result as ({@code a * b}) since
* {@code fusedMac(-0.0, +0.0, +0.0)} is {@code +0.0} while
* ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fusedMac(a, b, -0.0)} is
* equivalent to ({@code a * b}) however.
*
* @param a a value
* @param b a value
* @param c a value
*
* @return (< i > a < / i > & nbsp ; & times ; & nbsp ; < i > b < / i > & nbsp ; + & nbsp ; < i > c < / i >)
* computed, as if with unlimited range and precision, and rounded
* once to the nearest {@code double} value
*
* @apiNote This method corresponds to the fusedMultiplyAdd
* operation defined in IEEE 754-2008.
* @since 9
*/
// 计算a*b+c
public static double fma(double a, double b, double c) {
return Math.fma(a, b, c);
}
/**
* Returns the fused multiply add of the three arguments; that is,
* returns the exact product of the first two arguments summed
* with the third argument and then rounded once to the nearest
* {@code float}.
*
* The rounding is done using the {@linkplain
* java.math.RoundingMode#HALF_EVEN round to nearest even
* rounding mode}.
*
* In contrast, if {@code a * b + c} is evaluated as a regular
* floating-point expression, two rounding errors are involved,
* the first for the multiply operation, the second for the
* addition operation.
*
* <p>Special cases:
* <ul>
* <li> If any argument is NaN, the result is NaN.
*
* <li> If one of the first two arguments is infinite and the
* other is zero, the result is NaN.
*
* <li> If the exact product of the first two arguments is infinite
* (in other words, at least one of the arguments is infinite and
* the other is neither zero nor NaN) and the third argument is an
* infinity of the opposite sign, the result is NaN.
*
* </ul>
*
* <p>Note that {@code fma(a, 1.0f, c)} returns the same
* result as ({@code a + c}). However,
* {@code fma(a, b, +0.0f)} does <em>not</em> always return the
* same result as ({@code a * b}) since
* {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while
* ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is
* equivalent to ({@code a * b}) however.
*
* @param a a value
* @param b a value
* @param c a value
*
* @return (< i > a < / i > & nbsp ; & times ; & nbsp ; < i > b < / i > & nbsp ; + & nbsp ; < i > c < / i >)
* computed, as if with unlimited range and precision, and rounded
* once to the nearest {@code float} value
*
* @apiNote This method corresponds to the fusedMultiplyAdd
* operation defined in IEEE 754-2008.
* @since 9
*/
// 计算a*b+c
public static float fma(float a, float b, float c) {
return Math.fma(a, b, c);
}
/*▲ 基本运算 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 乘方/开方 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the value of the first argument raised to the power of the
* second argument. Special cases:
*
* <ul><li>If the second argument is positive or negative zero, then the
* result is 1.0.
* <li>If the second argument is 1.0, then the result is the same as the
* first argument.
* <li>If the second argument is NaN, then the result is NaN.
* <li>If the first argument is NaN and the second argument is nonzero,
* then the result is NaN.
*
* <li>If
* <ul>
* <li>the absolute value of the first argument is greater than 1
* and the second argument is positive infinity, or
* <li>the absolute value of the first argument is less than 1 and
* the second argument is negative infinity,
* </ul>
* then the result is positive infinity.
*
* <li>If
* <ul>
* <li>the absolute value of the first argument is greater than 1 and
* the second argument is negative infinity, or
* <li>the absolute value of the
* first argument is less than 1 and the second argument is positive
* infinity,
* </ul>
* then the result is positive zero.
*
* <li>If the absolute value of the first argument equals 1 and the
* second argument is infinite, then the result is NaN.
*
* <li>If
* <ul>
* <li>the first argument is positive zero and the second argument
* is greater than zero, or
* <li>the first argument is positive infinity and the second
* argument is less than zero,
* </ul>
* then the result is positive zero.
*
* <li>If
* <ul>
* <li>the first argument is positive zero and the second argument
* is less than zero, or
* <li>the first argument is positive infinity and the second
* argument is greater than zero,
* </ul>
* then the result is positive infinity.
*
* <li>If
* <ul>
* <li>the first argument is negative zero and the second argument
* is greater than zero but not a finite odd integer, or
* <li>the first argument is negative infinity and the second
* argument is less than zero but not a finite odd integer,
* </ul>
* then the result is positive zero.
*
* <li>If
* <ul>
* <li>the first argument is negative zero and the second argument
* is a positive finite odd integer, or
* <li>the first argument is negative infinity and the second
* argument is a negative finite odd integer,
* </ul>
* then the result is negative zero.
*
* <li>If
* <ul>
* <li>the first argument is negative zero and the second argument
* is less than zero but not a finite odd integer, or
* <li>the first argument is negative infinity and the second
* argument is greater than zero but not a finite odd integer,
* </ul>
* then the result is positive infinity.
*
* <li>If
* <ul>
* <li>the first argument is negative zero and the second argument
* is a negative finite odd integer, or
* <li>the first argument is negative infinity and the second
* argument is a positive finite odd integer,
* </ul>
* then the result is negative infinity.
*
* <li>If the first argument is finite and less than zero
* <ul>
* <li> if the second argument is a finite even integer, the
* result is equal to the result of raising the absolute value of
* the first argument to the power of the second argument
*
* <li>if the second argument is a finite odd integer, the result
* is equal to the negative of the result of raising the absolute
* value of the first argument to the power of the second
* argument
*
* <li>if the second argument is finite and not an integer, then
* the result is NaN.
* </ul>
*
* <li>If both arguments are integers, then the result is exactly equal
* to the mathematical result of raising the first argument to the power
* of the second argument if that result can in fact be represented
* exactly as a {@code double} value.</ul>
*
* <p>(In the foregoing descriptions, a floating-point value is
* considered to be an integer if and only if it is finite and a
* fixed point of the method {@link #ceil ceil} or,
* equivalently, a fixed point of the method {@link #floor
* floor}. A value is a fixed point of a one-argument
* method if and only if the result of applying the method to the
* value is equal to the value.)
*
* @param a base.
* @param b the exponent.
*
* @return the value {@code a}<sup>{@code b}</sup>.
*/
// a的b次方
public static double pow(double a, double b) {
return FdLibm.Pow.compute(a, b);
}
/**
* Returns the correctly rounded positive square root of a
* {@code double} value.
* Special cases:
* <ul><li>If the argument is NaN or less than zero, then the result
* is NaN.
* <li>If the argument is positive infinity, then the result is positive
* infinity.
* <li>If the argument is positive zero or negative zero, then the
* result is the same as the argument.</ul>
* Otherwise, the result is the {@code double} value closest to
* the true mathematical square root of the argument value.
*
* @param a a value.
*
* @return the positive square root of {@code a}.
*/
// 平方根
@HotSpotIntrinsicCandidate
public static native double sqrt(double a);
/**
* Returns the cube root of a {@code double} value. For
* positive finite {@code x}, {@code cbrt(-x) ==
* -cbrt(x)}; that is, the cube root of a negative value is
* the negative of the cube root of that value's magnitude.
* Special cases:
*
* <ul>
*
* <li>If the argument is NaN, then the result is NaN.
*
* <li>If the argument is infinite, then the result is an infinity
* with the same sign as the argument.
*
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.
*
* </ul>
*
* @param a a value.
*
* @return the cube root of {@code a}.
*
* @since 1.5
*/
// 立方根
public static double cbrt(double a) {
return FdLibm.Cbrt.compute(a);
}
/*▲ 乘方/开方 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 指数/对数 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns Euler's number <i>e</i> raised to the power of a
* {@code double} value. Special cases:
* <ul><li>If the argument is NaN, the result is NaN.
* <li>If the argument is positive infinity, then the result is
* positive infinity.
* <li>If the argument is negative infinity, then the result is
* positive zero.</ul>
*
* @param a the exponent to raise <i>e</i> to.
*
* @return the value <i>e</i><sup>{@code a}</sup>,
* where <i>e</i> is the base of the natural logarithms.
*/
// e的a次方
public static double exp(double a) {
return FdLibm.Exp.compute(a);
}
/**
* Returns the natural logarithm (base <i>e</i>) of a {@code double}
* value. Special cases:
* <ul><li>If the argument is NaN or less than zero, then the result
* is NaN.
* <li>If the argument is positive infinity, then the result is
* positive infinity.
* <li>If the argument is positive zero or negative zero, then the
* result is negative infinity.</ul>
*
* @param a a value
*
* @return the value ln {@code a}, the natural logarithm of
* {@code a}.
*/
// 以e为底a的对数,自然对数
public static native double log(double a);
/**
* Returns the base 10 logarithm of a {@code double} value.
* Special cases:
*
* <ul><li>If the argument is NaN or less than zero, then the result
* is NaN.
* <li>If the argument is positive infinity, then the result is
* positive infinity.
* <li>If the argument is positive zero or negative zero, then the
* result is negative infinity.
* <li> If the argument is equal to 10<sup><i>n</i></sup> for
* integer <i>n</i>, then the result is <i>n</i>.
* </ul>
*
* @param a a value
*
* @return the base 10 logarithm of {@code a}.
*
* @since 1.5
*/
// 以10为底a的对数
public static native double log10(double a);
/**
* Returns <i>e</i><sup>x</sup> -1. Note that for values of
* <i>x</i> near 0, the exact sum of
* {@code expm1(x)} + 1 is much closer to the true
* result of <i>e</i><sup>x</sup> than {@code exp(x)}.
*
* <p>Special cases:
* <ul>
* <li>If the argument is NaN, the result is NaN.
*
* <li>If the argument is positive infinity, then the result is
* positive infinity.
*
* <li>If the argument is negative infinity, then the result is
* -1.0.
*
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.
*
* </ul>
*
* @param x the exponent to raise <i>e</i> to in the computation of
* <i>e</i><sup>{@code x}</sup> -1.
*
* @return the value <i>e</i><sup>{@code x}</sup> - 1.
*
* @since 1.5
*/
// e的x次方减1
public static native double expm1(double x);
/**
* Returns the natural logarithm of the sum of the argument and 1.
* Note that for small values {@code x}, the result of
* {@code log1p(x)} is much closer to the true result of ln(1
* + {@code x}) than the floating-point evaluation of
* {@code log(1.0+x)}.
*
* <p>Special cases:
* <ul>
*
* <li>If the argument is NaN or less than -1, then the result is
* NaN.
*
* <li>If the argument is positive infinity, then the result is
* positive infinity.
*
* <li>If the argument is negative one, then the result is
* negative infinity.
*
* <li>If the argument is zero, then the result is a zero with the
* same sign as the argument.
*
* </ul>
*
* @param x a value
*
* @return the value ln({@code x} + 1), the natural
* log of {@code x} + 1
*
* @since 1.5
*/
// 以e为底x+1的对数
public static native double log1p(double x);
/**
* Returns {@code d} ×
* 2<sup>{@code scaleFactor}</sup> rounded as if performed
* by a single correctly rounded floating-point multiply to a
* member of the double value set. See the Java
* Language Specification for a discussion of floating-point
* value sets. If the exponent of the result is between {@link
* Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
* answer is calculated exactly. If the exponent of the result
* would be larger than {@code Double.MAX_EXPONENT}, an
* infinity is returned. Note that if the result is subnormal,
* precision may be lost; that is, when {@code scalb(x, n)}
* is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
* <i>x</i>. When the result is non-NaN, the result has the same
* sign as {@code d}.
*
* <p>Special cases:
* <ul>
* <li> If the first argument is NaN, NaN is returned.
* <li> If the first argument is infinite, then an infinity of the
* same sign is returned.
* <li> If the first argument is zero, then a zero of the same
* sign is returned.
* </ul>
*
* @param d number to be scaled by a power of two.
* @param scaleFactor power of 2 used to scale {@code d}
*
* @return {@code d} × 2<sup>{@code scaleFactor}</sup>
*
* @since 1.6
*/
// d乘以2的scaleFactor次方
public static double scalb(double d, int scaleFactor) {
return Math.scalb(d, scaleFactor);
}
/**
* Returns {@code f} ×
* 2<sup>{@code scaleFactor}</sup> rounded as if performed
* by a single correctly rounded floating-point multiply to a
* member of the float value set. See the Java
* Language Specification for a discussion of floating-point
* value sets. If the exponent of the result is between {@link
* Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
* answer is calculated exactly. If the exponent of the result
* would be larger than {@code Float.MAX_EXPONENT}, an
* infinity is returned. Note that if the result is subnormal,
* precision may be lost; that is, when {@code scalb(x, n)}
* is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
* <i>x</i>. When the result is non-NaN, the result has the same
* sign as {@code f}.
*
* <p>Special cases:
* <ul>
* <li> If the first argument is NaN, NaN is returned.
* <li> If the first argument is infinite, then an infinity of the
* same sign is returned.
* <li> If the first argument is zero, then a zero of the same
* sign is returned.
* </ul>
*
* @param f number to be scaled by a power of two.
* @param scaleFactor power of 2 used to scale {@code f}
*
* @return {@code f} × 2<sup>{@code scaleFactor}</sup>
*
* @since 1.6
*/
// f乘以2的scaleFactor次方
public static float scalb(float f, int scaleFactor) {
return Math.scalb(f, scaleFactor);
}
/*▲ 指数/对数 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 平面坐标 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Converts an angle measured in degrees to an approximately
* equivalent angle measured in radians. The conversion from
* degrees to radians is generally inexact.
*
* @param angdeg an angle, in degrees
*
* @return the measurement of the angle {@code angdeg}
* in radians.
*/
// 角度 --> 弧度
public static strictfp double toRadians(double angdeg) {
// Do not delegate to Math.toRadians(angdeg) because this method has the strictfp modifier.
return angdeg * DEGREES_TO_RADIANS;
}
/**
* Converts an angle measured in radians to an approximately
* equivalent angle measured in degrees. The conversion from
* radians to degrees is generally inexact; users should
* <i>not</i> expect {@code cos(toRadians(90.0))} to exactly