Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Math.cs
Go to the documentation of this file.
9
10namespace System;
11
12public static class Math
13{
14 public const double E = 2.718281828459045;
15
16 public const double PI = 3.141592653589793;
17
18 public const double Tau = Math.PI * 2.0;
19
20 private static readonly double[] roundPower10Double = new double[16]
21 {
22 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
23 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, 100000000000000.0, 1000000000000000.0
24 };
25
26 [MethodImpl(MethodImplOptions.InternalCall)]
27 [Intrinsic]
28 public static extern double Abs(double value);
29
30 [MethodImpl(MethodImplOptions.InternalCall)]
31 [Intrinsic]
32 public static extern float Abs(float value);
33
34 [MethodImpl(MethodImplOptions.InternalCall)]
35 [Intrinsic]
36 public static extern double Acos(double d);
37
38 [MethodImpl(MethodImplOptions.InternalCall)]
39 [Intrinsic]
40 public static extern double Acosh(double d);
41
42 [MethodImpl(MethodImplOptions.InternalCall)]
43 [Intrinsic]
44 public static extern double Asin(double d);
45
46 [MethodImpl(MethodImplOptions.InternalCall)]
47 [Intrinsic]
48 public static extern double Asinh(double d);
49
50 [MethodImpl(MethodImplOptions.InternalCall)]
51 [Intrinsic]
52 public static extern double Atan(double d);
53
54 [MethodImpl(MethodImplOptions.InternalCall)]
55 [Intrinsic]
56 public static extern double Atanh(double d);
57
58 [MethodImpl(MethodImplOptions.InternalCall)]
59 [Intrinsic]
60 public static extern double Atan2(double y, double x);
61
62 [MethodImpl(MethodImplOptions.InternalCall)]
63 [Intrinsic]
64 public static extern double Cbrt(double d);
65
66 [MethodImpl(MethodImplOptions.InternalCall)]
67 [Intrinsic]
68 public static extern double Ceiling(double a);
69
70 [MethodImpl(MethodImplOptions.InternalCall)]
71 [Intrinsic]
72 public static extern double Cos(double d);
73
74 [MethodImpl(MethodImplOptions.InternalCall)]
75 [Intrinsic]
76 public static extern double Cosh(double value);
77
78 [MethodImpl(MethodImplOptions.InternalCall)]
79 [Intrinsic]
80 public static extern double Exp(double d);
81
82 [MethodImpl(MethodImplOptions.InternalCall)]
83 [Intrinsic]
84 public static extern double Floor(double d);
85
86 [MethodImpl(MethodImplOptions.InternalCall)]
87 [Intrinsic]
88 public static extern double FusedMultiplyAdd(double x, double y, double z);
89
90 [MethodImpl(MethodImplOptions.InternalCall)]
91 [Intrinsic]
92 public static extern int ILogB(double x);
93
94 [MethodImpl(MethodImplOptions.InternalCall)]
95 [Intrinsic]
96 public static extern double Log(double d);
97
98 [MethodImpl(MethodImplOptions.InternalCall)]
99 [Intrinsic]
100 public static extern double Log2(double x);
101
102 [MethodImpl(MethodImplOptions.InternalCall)]
103 [Intrinsic]
104 public static extern double Log10(double d);
105
106 [MethodImpl(MethodImplOptions.InternalCall)]
107 [Intrinsic]
108 public static extern double Pow(double x, double y);
109
110 [MethodImpl(MethodImplOptions.InternalCall)]
111 [Intrinsic]
112 public static extern double Sin(double a);
113
114 public unsafe static (double Sin, double Cos) SinCos(double x)
115 {
116 System.Runtime.CompilerServices.Unsafe.SkipInit(out double item);
117 System.Runtime.CompilerServices.Unsafe.SkipInit(out double item2);
118 SinCos(x, &item, &item2);
119 return (Sin: item, Cos: item2);
120 }
121
122 [MethodImpl(MethodImplOptions.InternalCall)]
123 [Intrinsic]
124 public static extern double Sinh(double value);
125
126 [MethodImpl(MethodImplOptions.InternalCall)]
127 [Intrinsic]
128 public static extern double Sqrt(double d);
129
130 [MethodImpl(MethodImplOptions.InternalCall)]
131 [Intrinsic]
132 public static extern double Tan(double a);
133
134 [MethodImpl(MethodImplOptions.InternalCall)]
135 [Intrinsic]
136 public static extern double Tanh(double value);
137
138 [MethodImpl(MethodImplOptions.InternalCall)]
139 private unsafe static extern double ModF(double x, double* intptr);
140
141 [MethodImpl(MethodImplOptions.InternalCall)]
142 private unsafe static extern void SinCos(double x, double* sin, double* cos);
143
144 [MethodImpl(MethodImplOptions.AggressiveInlining)]
145 public static short Abs(short value)
146 {
147 if (value < 0)
148 {
149 value = (short)(-value);
150 if (value < 0)
151 {
153 }
154 }
155 return value;
156 }
157
158 [MethodImpl(MethodImplOptions.AggressiveInlining)]
159 public static int Abs(int value)
160 {
161 if (value < 0)
162 {
163 value = -value;
164 if (value < 0)
165 {
167 }
168 }
169 return value;
170 }
171
172 [MethodImpl(MethodImplOptions.AggressiveInlining)]
173 public static long Abs(long value)
174 {
175 if (value < 0)
176 {
177 value = -value;
178 if (value < 0)
179 {
181 }
182 }
183 return value;
184 }
185
186 [MethodImpl(MethodImplOptions.AggressiveInlining)]
187 public static nint Abs(nint value)
188 {
189 if (value < 0)
190 {
191 value = -value;
192 if (value < 0)
193 {
195 }
196 }
197 return value;
198 }
199
200 [MethodImpl(MethodImplOptions.AggressiveInlining)]
201 [CLSCompliant(false)]
202 public static sbyte Abs(sbyte value)
203 {
204 if (value < 0)
205 {
206 value = (sbyte)(-value);
207 if (value < 0)
208 {
210 }
211 }
212 return value;
213 }
214
215 [MethodImpl(MethodImplOptions.AggressiveInlining)]
216 public static decimal Abs(decimal value)
217 {
218 return decimal.Abs(in value);
219 }
220
221 [DoesNotReturn]
222 [StackTraceHidden]
223 private static void ThrowAbsOverflow()
224 {
226 }
227
228 public static long BigMul(int a, int b)
229 {
230 return (long)a * (long)b;
231 }
232
233 [MethodImpl(MethodImplOptions.AggressiveInlining)]
234 [CLSCompliant(false)]
235 public unsafe static ulong BigMul(ulong a, ulong b, out ulong low)
236 {
237 if (Bmi2.X64.IsSupported)
238 {
239 System.Runtime.CompilerServices.Unsafe.SkipInit(out ulong num);
240 ulong result = Bmi2.X64.MultiplyNoFlags(a, b, &num);
241 low = num;
242 return result;
243 }
245 {
246 }
247 return SoftwareFallback(a, b, out low);
248 static ulong SoftwareFallback(ulong a, ulong b, out ulong low)
249 {
250 uint num2 = (uint)a;
251 uint num3 = (uint)(a >> 32);
252 uint num4 = (uint)b;
253 uint num5 = (uint)(b >> 32);
254 ulong num6 = (ulong)num2 * (ulong)num4;
255 ulong num7 = (ulong)((long)num3 * (long)num4) + (num6 >> 32);
256 ulong num8 = (ulong)((long)num2 * (long)num5 + (uint)num7);
257 low = (num8 << 32) | (uint)num6;
258 return (ulong)((long)num3 * (long)num5 + (long)(num7 >> 32)) + (num8 >> 32);
259 }
260 }
261
262 public static long BigMul(long a, long b, out long low)
263 {
265 {
266 }
267 ulong low2;
268 ulong num = BigMul((ulong)a, (ulong)b, out low2);
269 low = (long)low2;
270 return (long)num - ((a >> 63) & b) - ((b >> 63) & a);
271 }
272
273 public static double BitDecrement(double x)
274 {
275 long num = BitConverter.DoubleToInt64Bits(x);
276 if (((num >> 32) & 0x7FF00000) >= 2146435072)
277 {
278 if (num != 9218868437227405312L)
279 {
280 return x;
281 }
282 return double.MaxValue;
283 }
284 if (num == 0L)
285 {
286 return -5E-324;
287 }
288 num += ((num < 0) ? 1 : (-1));
290 }
291
292 public static double BitIncrement(double x)
293 {
294 long num = BitConverter.DoubleToInt64Bits(x);
295 if (((num >> 32) & 0x7FF00000) >= 2146435072)
296 {
297 if (num != -4503599627370496L)
298 {
299 return x;
300 }
301 return double.MinValue;
302 }
303 if (num == long.MinValue)
304 {
305 return double.Epsilon;
306 }
307 num += ((num >= 0) ? 1 : (-1));
309 }
310
311 [MethodImpl(MethodImplOptions.AggressiveInlining)]
312 public static double CopySign(double x, double y)
313 {
315 {
317 }
318 return SoftwareFallback(x, y);
319 static double SoftwareFallback(double x, double y)
320 {
321 long num = BitConverter.DoubleToInt64Bits(x);
322 long num2 = BitConverter.DoubleToInt64Bits(y);
323 num &= 0x7FFFFFFFFFFFFFFFL;
324 num2 &= long.MinValue;
325 return BitConverter.Int64BitsToDouble(num | num2);
326 }
327 }
328
329 public static int DivRem(int a, int b, out int result)
330 {
331 int num = a / b;
332 result = a - num * b;
333 return num;
334 }
335
336 public static long DivRem(long a, long b, out long result)
337 {
338 long num = a / b;
339 result = a - num * b;
340 return num;
341 }
342
343 [MethodImpl(MethodImplOptions.AggressiveInlining)]
344 [NonVersionable]
345 [CLSCompliant(false)]
346 public static (sbyte Quotient, sbyte Remainder) DivRem(sbyte left, sbyte right)
347 {
348 sbyte b = (sbyte)(left / right);
349 return (Quotient: b, Remainder: (sbyte)(left - b * right));
350 }
351
352 [MethodImpl(MethodImplOptions.AggressiveInlining)]
353 [NonVersionable]
354 public static (byte Quotient, byte Remainder) DivRem(byte left, byte right)
355 {
356 byte b = (byte)(left / right);
357 return (Quotient: b, Remainder: (byte)(left - b * right));
358 }
359
360 [MethodImpl(MethodImplOptions.AggressiveInlining)]
361 [NonVersionable]
362 public static (short Quotient, short Remainder) DivRem(short left, short right)
363 {
364 short num = (short)(left / right);
365 return (Quotient: num, Remainder: (short)(left - num * right));
366 }
367
368 [MethodImpl(MethodImplOptions.AggressiveInlining)]
369 [NonVersionable]
370 [CLSCompliant(false)]
371 public static (ushort Quotient, ushort Remainder) DivRem(ushort left, ushort right)
372 {
373 ushort num = (ushort)(left / right);
374 return (Quotient: num, Remainder: (ushort)(left - num * right));
375 }
376
377 [MethodImpl(MethodImplOptions.AggressiveInlining)]
378 [NonVersionable]
379 public static (int Quotient, int Remainder) DivRem(int left, int right)
380 {
381 int num = left / right;
382 return (Quotient: num, Remainder: left - num * right);
383 }
384
385 [MethodImpl(MethodImplOptions.AggressiveInlining)]
386 [NonVersionable]
387 [CLSCompliant(false)]
388 public static (uint Quotient, uint Remainder) DivRem(uint left, uint right)
389 {
390 uint num = left / right;
391 return (Quotient: num, Remainder: left - num * right);
392 }
393
394 [MethodImpl(MethodImplOptions.AggressiveInlining)]
395 [NonVersionable]
396 public static (long Quotient, long Remainder) DivRem(long left, long right)
397 {
398 long num = left / right;
399 return (Quotient: num, Remainder: left - num * right);
400 }
401
402 [MethodImpl(MethodImplOptions.AggressiveInlining)]
403 [NonVersionable]
404 [CLSCompliant(false)]
405 public static (ulong Quotient, ulong Remainder) DivRem(ulong left, ulong right)
406 {
407 ulong num = left / right;
408 return (Quotient: num, Remainder: left - num * right);
409 }
410
411 [MethodImpl(MethodImplOptions.AggressiveInlining)]
412 [NonVersionable]
413 public static (nint Quotient, nint Remainder) DivRem(nint left, nint right)
414 {
415 nint num = left / right;
416 return (Quotient: num, Remainder: left - num * right);
417 }
418
419 [MethodImpl(MethodImplOptions.AggressiveInlining)]
420 [NonVersionable]
421 [CLSCompliant(false)]
422 public static (nuint Quotient, nuint Remainder) DivRem(nuint left, nuint right)
423 {
424 nuint num = left / right;
425 return (Quotient: num, Remainder: left - num * right);
426 }
427
428 [MethodImpl(MethodImplOptions.AggressiveInlining)]
429 public static decimal Ceiling(decimal d)
430 {
431 return decimal.Ceiling(d);
432 }
433
434 [MethodImpl(MethodImplOptions.AggressiveInlining)]
435 public static byte Clamp(byte value, byte min, byte max)
436 {
437 if (min > max)
438 {
439 ThrowMinMaxException(min, max);
440 }
441 if (value < min)
442 {
443 return min;
444 }
445 if (value > max)
446 {
447 return max;
448 }
449 return value;
450 }
451
452 [MethodImpl(MethodImplOptions.AggressiveInlining)]
453 public static decimal Clamp(decimal value, decimal min, decimal max)
454 {
455 if (min > max)
456 {
457 ThrowMinMaxException(min, max);
458 }
459 if (value < min)
460 {
461 return min;
462 }
463 if (value > max)
464 {
465 return max;
466 }
467 return value;
468 }
469
470 [MethodImpl(MethodImplOptions.AggressiveInlining)]
471 public static double Clamp(double value, double min, double max)
472 {
473 if (min > max)
474 {
475 ThrowMinMaxException(min, max);
476 }
477 if (value < min)
478 {
479 return min;
480 }
481 if (value > max)
482 {
483 return max;
484 }
485 return value;
486 }
487
488 [MethodImpl(MethodImplOptions.AggressiveInlining)]
489 public static short Clamp(short value, short min, short max)
490 {
491 if (min > max)
492 {
493 ThrowMinMaxException(min, max);
494 }
495 if (value < min)
496 {
497 return min;
498 }
499 if (value > max)
500 {
501 return max;
502 }
503 return value;
504 }
505
506 [MethodImpl(MethodImplOptions.AggressiveInlining)]
507 public static int Clamp(int value, int min, int max)
508 {
509 if (min > max)
510 {
511 ThrowMinMaxException(min, max);
512 }
513 if (value < min)
514 {
515 return min;
516 }
517 if (value > max)
518 {
519 return max;
520 }
521 return value;
522 }
523
524 [MethodImpl(MethodImplOptions.AggressiveInlining)]
525 public static long Clamp(long value, long min, long max)
526 {
527 if (min > max)
528 {
529 ThrowMinMaxException(min, max);
530 }
531 if (value < min)
532 {
533 return min;
534 }
535 if (value > max)
536 {
537 return max;
538 }
539 return value;
540 }
541
542 [MethodImpl(MethodImplOptions.AggressiveInlining)]
543 public static nint Clamp(nint value, nint min, nint max)
544 {
545 if (min > max)
546 {
547 ThrowMinMaxException(min, max);
548 }
549 if (value < min)
550 {
551 return min;
552 }
553 if (value > max)
554 {
555 return max;
556 }
557 return value;
558 }
559
560 [MethodImpl(MethodImplOptions.AggressiveInlining)]
561 [CLSCompliant(false)]
562 public static sbyte Clamp(sbyte value, sbyte min, sbyte max)
563 {
564 if (min > max)
565 {
566 ThrowMinMaxException(min, max);
567 }
568 if (value < min)
569 {
570 return min;
571 }
572 if (value > max)
573 {
574 return max;
575 }
576 return value;
577 }
578
579 [MethodImpl(MethodImplOptions.AggressiveInlining)]
580 public static float Clamp(float value, float min, float max)
581 {
582 if (min > max)
583 {
584 ThrowMinMaxException(min, max);
585 }
586 if (value < min)
587 {
588 return min;
589 }
590 if (value > max)
591 {
592 return max;
593 }
594 return value;
595 }
596
597 [MethodImpl(MethodImplOptions.AggressiveInlining)]
598 [CLSCompliant(false)]
599 public static ushort Clamp(ushort value, ushort min, ushort max)
600 {
601 if (min > max)
602 {
603 ThrowMinMaxException(min, max);
604 }
605 if (value < min)
606 {
607 return min;
608 }
609 if (value > max)
610 {
611 return max;
612 }
613 return value;
614 }
615
616 [MethodImpl(MethodImplOptions.AggressiveInlining)]
617 [CLSCompliant(false)]
618 public static uint Clamp(uint value, uint min, uint max)
619 {
620 if (min > max)
621 {
622 ThrowMinMaxException(min, max);
623 }
624 if (value < min)
625 {
626 return min;
627 }
628 if (value > max)
629 {
630 return max;
631 }
632 return value;
633 }
634
635 [MethodImpl(MethodImplOptions.AggressiveInlining)]
636 [CLSCompliant(false)]
637 public static ulong Clamp(ulong value, ulong min, ulong max)
638 {
639 if (min > max)
640 {
641 ThrowMinMaxException(min, max);
642 }
643 if (value < min)
644 {
645 return min;
646 }
647 if (value > max)
648 {
649 return max;
650 }
651 return value;
652 }
653
654 [MethodImpl(MethodImplOptions.AggressiveInlining)]
655 [CLSCompliant(false)]
656 public static nuint Clamp(nuint value, nuint min, nuint max)
657 {
658 if (min > max)
659 {
660 ThrowMinMaxException(min, max);
661 }
662 if (value < min)
663 {
664 return min;
665 }
666 if (value > max)
667 {
668 return max;
669 }
670 return value;
671 }
672
673 [MethodImpl(MethodImplOptions.AggressiveInlining)]
674 public static decimal Floor(decimal d)
675 {
676 return decimal.Floor(d);
677 }
678
679 public static double IEEERemainder(double x, double y)
680 {
681 if (double.IsNaN(x))
682 {
683 return x;
684 }
685 if (double.IsNaN(y))
686 {
687 return y;
688 }
689 double num = x % y;
690 if (double.IsNaN(num))
691 {
692 return double.NaN;
693 }
694 if (num == 0.0 && double.IsNegative(x))
695 {
696 return -0.0;
697 }
698 double num2 = num - Abs(y) * (double)Sign(x);
699 if (Abs(num2) == Abs(num))
700 {
701 double num3 = x / y;
702 double value = Round(num3);
703 if (Abs(value) > Abs(num3))
704 {
705 return num2;
706 }
707 return num;
708 }
709 if (Abs(num2) < Abs(num))
710 {
711 return num2;
712 }
713 return num;
714 }
715
716 public static double Log(double a, double newBase)
717 {
718 if (double.IsNaN(a))
719 {
720 return a;
721 }
722 if (double.IsNaN(newBase))
723 {
724 return newBase;
725 }
726 if (newBase == 1.0)
727 {
728 return double.NaN;
729 }
730 if (a != 1.0 && (newBase == 0.0 || double.IsPositiveInfinity(newBase)))
731 {
732 return double.NaN;
733 }
734 return Log(a) / Log(newBase);
735 }
736
737 [NonVersionable]
738 public static byte Max(byte val1, byte val2)
739 {
740 if (val1 < val2)
741 {
742 return val2;
743 }
744 return val1;
745 }
746
747 [MethodImpl(MethodImplOptions.AggressiveInlining)]
748 public static decimal Max(decimal val1, decimal val2)
749 {
750 return decimal.Max(in val1, in val2);
751 }
752
753 [MethodImpl(MethodImplOptions.AggressiveInlining)]
754 public static double Max(double val1, double val2)
755 {
756 if (val1 != val2)
757 {
758 if (!double.IsNaN(val1))
759 {
760 if (!(val2 < val1))
761 {
762 return val2;
763 }
764 return val1;
765 }
766 return val1;
767 }
768 if (!double.IsNegative(val2))
769 {
770 return val2;
771 }
772 return val1;
773 }
774
775 [NonVersionable]
776 public static short Max(short val1, short val2)
777 {
778 if (val1 < val2)
779 {
780 return val2;
781 }
782 return val1;
783 }
784
785 [NonVersionable]
786 public static int Max(int val1, int val2)
787 {
788 if (val1 < val2)
789 {
790 return val2;
791 }
792 return val1;
793 }
794
795 [NonVersionable]
796 public static long Max(long val1, long val2)
797 {
798 if (val1 < val2)
799 {
800 return val2;
801 }
802 return val1;
803 }
804
805 [NonVersionable]
806 public static nint Max(nint val1, nint val2)
807 {
808 if (val1 < val2)
809 {
810 return val2;
811 }
812 return val1;
813 }
814
815 [CLSCompliant(false)]
816 [NonVersionable]
817 public static sbyte Max(sbyte val1, sbyte val2)
818 {
819 if (val1 < val2)
820 {
821 return val2;
822 }
823 return val1;
824 }
825
826 [MethodImpl(MethodImplOptions.AggressiveInlining)]
827 public static float Max(float val1, float val2)
828 {
829 if (val1 != val2)
830 {
831 if (!float.IsNaN(val1))
832 {
833 if (!(val2 < val1))
834 {
835 return val2;
836 }
837 return val1;
838 }
839 return val1;
840 }
841 if (!float.IsNegative(val2))
842 {
843 return val2;
844 }
845 return val1;
846 }
847
848 [CLSCompliant(false)]
849 [NonVersionable]
850 public static ushort Max(ushort val1, ushort val2)
851 {
852 if (val1 < val2)
853 {
854 return val2;
855 }
856 return val1;
857 }
858
859 [CLSCompliant(false)]
860 [NonVersionable]
861 public static uint Max(uint val1, uint val2)
862 {
863 if (val1 < val2)
864 {
865 return val2;
866 }
867 return val1;
868 }
869
870 [CLSCompliant(false)]
871 [NonVersionable]
872 public static ulong Max(ulong val1, ulong val2)
873 {
874 if (val1 < val2)
875 {
876 return val2;
877 }
878 return val1;
879 }
880
881 [CLSCompliant(false)]
882 [NonVersionable]
883 public static nuint Max(nuint val1, nuint val2)
884 {
885 if (val1 < val2)
886 {
887 return val2;
888 }
889 return val1;
890 }
891
892 public static double MaxMagnitude(double x, double y)
893 {
894 double num = Abs(x);
895 double num2 = Abs(y);
896 if (num > num2 || double.IsNaN(num))
897 {
898 return x;
899 }
900 if (num == num2)
901 {
902 if (!double.IsNegative(x))
903 {
904 return x;
905 }
906 return y;
907 }
908 return y;
909 }
910
911 [NonVersionable]
912 public static byte Min(byte val1, byte val2)
913 {
914 if (val1 > val2)
915 {
916 return val2;
917 }
918 return val1;
919 }
920
921 [MethodImpl(MethodImplOptions.AggressiveInlining)]
922 public static decimal Min(decimal val1, decimal val2)
923 {
924 return decimal.Min(in val1, in val2);
925 }
926
927 [MethodImpl(MethodImplOptions.AggressiveInlining)]
928 public static double Min(double val1, double val2)
929 {
930 if (val1 != val2 && !double.IsNaN(val1))
931 {
932 if (!(val1 < val2))
933 {
934 return val2;
935 }
936 return val1;
937 }
938 if (!double.IsNegative(val1))
939 {
940 return val2;
941 }
942 return val1;
943 }
944
945 [NonVersionable]
946 public static short Min(short val1, short val2)
947 {
948 if (val1 > val2)
949 {
950 return val2;
951 }
952 return val1;
953 }
954
955 [NonVersionable]
956 public static int Min(int val1, int val2)
957 {
958 if (val1 > val2)
959 {
960 return val2;
961 }
962 return val1;
963 }
964
965 [NonVersionable]
966 public static long Min(long val1, long val2)
967 {
968 if (val1 > val2)
969 {
970 return val2;
971 }
972 return val1;
973 }
974
975 [NonVersionable]
976 public static nint Min(nint val1, nint val2)
977 {
978 if (val1 > val2)
979 {
980 return val2;
981 }
982 return val1;
983 }
984
985 [CLSCompliant(false)]
986 [NonVersionable]
987 public static sbyte Min(sbyte val1, sbyte val2)
988 {
989 if (val1 > val2)
990 {
991 return val2;
992 }
993 return val1;
994 }
995
996 [MethodImpl(MethodImplOptions.AggressiveInlining)]
997 public static float Min(float val1, float val2)
998 {
999 if (val1 != val2 && !float.IsNaN(val1))
1000 {
1001 if (!(val1 < val2))
1002 {
1003 return val2;
1004 }
1005 return val1;
1006 }
1007 if (!float.IsNegative(val1))
1008 {
1009 return val2;
1010 }
1011 return val1;
1012 }
1013
1014 [CLSCompliant(false)]
1015 [NonVersionable]
1016 public static ushort Min(ushort val1, ushort val2)
1017 {
1018 if (val1 > val2)
1019 {
1020 return val2;
1021 }
1022 return val1;
1023 }
1024
1025 [CLSCompliant(false)]
1026 [NonVersionable]
1027 public static uint Min(uint val1, uint val2)
1028 {
1029 if (val1 > val2)
1030 {
1031 return val2;
1032 }
1033 return val1;
1034 }
1035
1036 [CLSCompliant(false)]
1037 [NonVersionable]
1038 public static ulong Min(ulong val1, ulong val2)
1039 {
1040 if (val1 > val2)
1041 {
1042 return val2;
1043 }
1044 return val1;
1045 }
1046
1047 [CLSCompliant(false)]
1048 [NonVersionable]
1049 public static nuint Min(nuint val1, nuint val2)
1050 {
1051 if (val1 > val2)
1052 {
1053 return val2;
1054 }
1055 return val1;
1056 }
1057
1058 public static double MinMagnitude(double x, double y)
1059 {
1060 double num = Abs(x);
1061 double num2 = Abs(y);
1062 if (num < num2 || double.IsNaN(num))
1063 {
1064 return x;
1065 }
1066 if (num == num2)
1067 {
1068 if (!double.IsNegative(x))
1069 {
1070 return y;
1071 }
1072 return x;
1073 }
1074 return y;
1075 }
1076
1077 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1078 public static double ReciprocalEstimate(double d)
1079 {
1081 {
1082 }
1083 return 1.0 / d;
1084 }
1085
1086 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1087 public static double ReciprocalSqrtEstimate(double d)
1088 {
1090 {
1091 }
1092 return 1.0 / Sqrt(d);
1093 }
1094
1095 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1096 public static decimal Round(decimal d)
1097 {
1098 return decimal.Round(d, 0);
1099 }
1100
1101 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1102 public static decimal Round(decimal d, int decimals)
1103 {
1104 return decimal.Round(d, decimals);
1105 }
1106
1107 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1108 public static decimal Round(decimal d, MidpointRounding mode)
1109 {
1110 return decimal.Round(d, 0, mode);
1111 }
1112
1113 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1114 public static decimal Round(decimal d, int decimals, MidpointRounding mode)
1115 {
1116 return decimal.Round(d, decimals, mode);
1117 }
1118
1119 [Intrinsic]
1120 public static double Round(double a)
1121 {
1122 ulong num = BitConverter.DoubleToUInt64Bits(a);
1123 int num2 = double.ExtractExponentFromBits(num);
1124 if (num2 <= 1022)
1125 {
1126 if (num << 1 == 0L)
1127 {
1128 return a;
1129 }
1130 double x = ((num2 == 1022 && double.ExtractSignificandFromBits(num) != 0L) ? 1.0 : 0.0);
1131 return CopySign(x, a);
1132 }
1133 if (num2 >= 1075)
1134 {
1135 return a;
1136 }
1137 ulong num3 = (ulong)(1L << 1075 - num2);
1138 ulong num4 = num3 - 1;
1139 num += num3 >> 1;
1140 num = (((num & num4) != 0L) ? (num & ~num4) : (num & ~num3));
1141 return BitConverter.UInt64BitsToDouble(num);
1142 }
1143
1144 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1145 public static double Round(double value, int digits)
1146 {
1147 return Round(value, digits, MidpointRounding.ToEven);
1148 }
1149
1150 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1151 public static double Round(double value, MidpointRounding mode)
1152 {
1153 return Round(value, 0, mode);
1154 }
1155
1156 public unsafe static double Round(double value, int digits, MidpointRounding mode)
1157 {
1158 if (digits < 0 || digits > 15)
1159 {
1161 }
1162 if (mode < MidpointRounding.ToEven || mode > MidpointRounding.ToPositiveInfinity)
1163 {
1164 throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, "MidpointRounding"), "mode");
1165 }
1166 if (Abs(value) < 10000000000000000.0)
1167 {
1168 double num = roundPower10Double[digits];
1169 value *= num;
1170 switch (mode)
1171 {
1172 case MidpointRounding.ToEven:
1173 value = Round(value);
1174 break;
1175 case MidpointRounding.AwayFromZero:
1176 {
1177 double value2 = ModF(value, &value);
1178 if (Abs(value2) >= 0.5)
1179 {
1180 value += (double)Sign(value2);
1181 }
1182 break;
1183 }
1184 case MidpointRounding.ToZero:
1185 value = Truncate(value);
1186 break;
1187 case MidpointRounding.ToNegativeInfinity:
1188 value = Floor(value);
1189 break;
1190 case MidpointRounding.ToPositiveInfinity:
1191 value = Ceiling(value);
1192 break;
1193 default:
1194 throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, "MidpointRounding"), "mode");
1195 }
1196 value /= num;
1197 }
1198 return value;
1199 }
1200
1201 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1202 public static int Sign(decimal value)
1203 {
1204 return decimal.Sign(in value);
1205 }
1206
1207 public static int Sign(double value)
1208 {
1209 if (value < 0.0)
1210 {
1211 return -1;
1212 }
1213 if (value > 0.0)
1214 {
1215 return 1;
1216 }
1217 if (value == 0.0)
1218 {
1219 return 0;
1220 }
1222 }
1223
1224 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1225 public static int Sign(short value)
1226 {
1227 return Sign((int)value);
1228 }
1229
1230 public static int Sign(int value)
1231 {
1232 return (value >> 31) | (-value >>> 31);
1233 }
1234
1235 public static int Sign(long value)
1236 {
1237 return (int)((value >> 63) | (-value >>> 63));
1238 }
1239
1240 public static int Sign(nint value)
1241 {
1242 return (int)((long)(value >> 63) | (long)((ulong)(-value) >> 63));
1243 }
1244
1245 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1246 [CLSCompliant(false)]
1247 public static int Sign(sbyte value)
1248 {
1249 return Sign((int)value);
1250 }
1251
1252 public static int Sign(float value)
1253 {
1254 if (value < 0f)
1255 {
1256 return -1;
1257 }
1258 if (value > 0f)
1259 {
1260 return 1;
1261 }
1262 if (value == 0f)
1263 {
1264 return 0;
1265 }
1267 }
1268
1269 [MethodImpl(MethodImplOptions.AggressiveInlining)]
1270 public static decimal Truncate(decimal d)
1271 {
1272 return decimal.Truncate(d);
1273 }
1274
1275 public unsafe static double Truncate(double d)
1276 {
1277 ModF(d, &d);
1278 return d;
1279 }
1280
1281 [DoesNotReturn]
1282 private static void ThrowMinMaxException<T>(T min, T max)
1283 {
1284 throw new ArgumentException(SR.Format(SR.Argument_MinMaxValue, min, max));
1285 }
1286
1287 public static double ScaleB(double x, int n)
1288 {
1289 double num = x;
1290 if (n > 1023)
1291 {
1292 num *= 8.98846567431158E+307;
1293 n -= 1023;
1294 if (n > 1023)
1295 {
1296 num *= 8.98846567431158E+307;
1297 n -= 1023;
1298 if (n > 1023)
1299 {
1300 n = 1023;
1301 }
1302 }
1303 }
1304 else if (n < -1022)
1305 {
1306 num *= 2.004168360008973E-292;
1307 n += 969;
1308 if (n < -1022)
1309 {
1310 num *= 2.004168360008973E-292;
1311 n += 969;
1312 if (n < -1022)
1313 {
1314 n = -1022;
1315 }
1316 }
1317 }
1318 double num2 = BitConverter.Int64BitsToDouble((long)(1023 + n) << 52);
1319 return num * num2;
1320 }
1321}
static unsafe double Int64BitsToDouble(long value)
static ulong DoubleToUInt64Bits(double value)
static unsafe long DoubleToInt64Bits(double value)
static double UInt64BitsToDouble(ulong value)
static double Atan(double d)
static double Log2(double x)
static sbyte Abs(sbyte value)
Definition Math.cs:202
static decimal Round(decimal d, int decimals)
Definition Math.cs:1102
static byte Clamp(byte value, byte min, byte max)
Definition Math.cs:435
static ulong Clamp(ulong value, ulong min, ulong max)
Definition Math.cs:637
static long long Remainder DivRem(long left, long right)
Definition Math.cs:396
static double Min(double val1, double val2)
Definition Math.cs:928
static double Acos(double d)
static unsafe ulong BigMul(ulong a, ulong b, out ulong low)
Definition Math.cs:235
static uint uint Remainder DivRem(uint left, uint right)
Definition Math.cs:388
static decimal Truncate(decimal d)
Definition Math.cs:1270
static nint nint Remainder DivRem(nint left, nint right)
Definition Math.cs:413
static double Cos(double d)
static unsafe double Round(double value, int digits, MidpointRounding mode)
Definition Math.cs:1156
static decimal Max(decimal val1, decimal val2)
Definition Math.cs:748
static double Exp(double d)
static sbyte Quotient
Definition Math.cs:346
static int Abs(int value)
Definition Math.cs:159
static double ReciprocalEstimate(double d)
Definition Math.cs:1078
static decimal Round(decimal d, int decimals, MidpointRounding mode)
Definition Math.cs:1114
static double Tanh(double value)
static double Round(double value, int digits)
Definition Math.cs:1145
static short short Remainder DivRem(short left, short right)
Definition Math.cs:362
static decimal Ceiling(decimal d)
Definition Math.cs:429
static ulong Min(ulong val1, ulong val2)
Definition Math.cs:1038
static void ThrowMinMaxException< T >(T min, T max)
Definition Math.cs:1282
static nint Abs(nint value)
Definition Math.cs:187
static float Min(float val1, float val2)
Definition Math.cs:997
static double Atanh(double d)
static int Sign(float value)
Definition Math.cs:1252
static sbyte Min(sbyte val1, sbyte val2)
Definition Math.cs:987
static unsafe void SinCos(double x, double *sin, double *cos)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static int Sign(sbyte value)
Definition Math.cs:1247
static double ScaleB(double x, int n)
Definition Math.cs:1287
static decimal Round(decimal d, MidpointRounding mode)
Definition Math.cs:1108
static double CopySign(double x, double y)
Definition Math.cs:312
static ushort Clamp(ushort value, ushort min, ushort max)
Definition Math.cs:599
static double Tan(double a)
static int int Remainder DivRem(int left, int right)
Definition Math.cs:379
static nuint nuint Remainder DivRem(nuint left, nuint right)
Definition Math.cs:422
static short Clamp(short value, short min, short max)
Definition Math.cs:489
static ushort Max(ushort val1, ushort val2)
Definition Math.cs:850
static double Atan2(double y, double x)
static byte byte Remainder DivRem(byte left, byte right)
Definition Math.cs:354
static double Cbrt(double d)
static short Max(short val1, short val2)
Definition Math.cs:776
static long BigMul(long a, long b, out long low)
Definition Math.cs:262
static nint Max(nint val1, nint val2)
Definition Math.cs:806
static decimal Floor(decimal d)
Definition Math.cs:674
static double Sqrt(double d)
static nuint Clamp(nuint value, nuint min, nuint max)
Definition Math.cs:656
static double Asin(double d)
static double Pow(double x, double y)
static double MinMagnitude(double x, double y)
Definition Math.cs:1058
static nuint Max(nuint val1, nuint val2)
Definition Math.cs:883
static long Clamp(long value, long min, long max)
Definition Math.cs:525
static int Sign(int value)
Definition Math.cs:1230
static short Abs(short value)
Definition Math.cs:145
static decimal Min(decimal val1, decimal val2)
Definition Math.cs:922
static decimal Round(decimal d)
Definition Math.cs:1096
static double Ceiling(double a)
static double Asinh(double d)
static long Abs(long value)
Definition Math.cs:173
static float Abs(float value)
static nuint Min(nuint val1, nuint val2)
Definition Math.cs:1049
static double ReciprocalSqrtEstimate(double d)
Definition Math.cs:1087
const double E
Definition Math.cs:14
static readonly double[] roundPower10Double
Definition Math.cs:20
static int Min(int val1, int val2)
Definition Math.cs:956
static sbyte Clamp(sbyte value, sbyte min, sbyte max)
Definition Math.cs:562
static double IEEERemainder(double x, double y)
Definition Math.cs:679
static double Max(double val1, double val2)
Definition Math.cs:754
static uint Min(uint val1, uint val2)
Definition Math.cs:1027
static double Log10(double d)
static int Sign(double value)
Definition Math.cs:1207
static double Clamp(double value, double min, double max)
Definition Math.cs:471
static float Clamp(float value, float min, float max)
Definition Math.cs:580
static ushort ushort Remainder DivRem(ushort left, ushort right)
Definition Math.cs:371
static ushort Min(ushort val1, ushort val2)
Definition Math.cs:1016
static int Clamp(int value, int min, int max)
Definition Math.cs:507
static double BitDecrement(double x)
Definition Math.cs:273
static double Log(double a, double newBase)
Definition Math.cs:716
static double Log(double d)
static long Max(long val1, long val2)
Definition Math.cs:796
static int Max(int val1, int val2)
Definition Math.cs:786
static double Acosh(double d)
static double Abs(double value)
static long DivRem(long a, long b, out long result)
Definition Math.cs:336
static int DivRem(int a, int b, out int result)
Definition Math.cs:329
static unsafe(double Sin, double Cos) SinCos(double x)
Definition Math.cs:114
static nint Clamp(nint value, nint min, nint max)
Definition Math.cs:543
static double Sin(double a)
static float Max(float val1, float val2)
Definition Math.cs:827
const double PI
Definition Math.cs:16
static short Min(short val1, short val2)
Definition Math.cs:946
static void ThrowAbsOverflow()
Definition Math.cs:223
static uint Max(uint val1, uint val2)
Definition Math.cs:861
static double BitIncrement(double x)
Definition Math.cs:292
static ulong Max(ulong val1, ulong val2)
Definition Math.cs:872
static double Sinh(double value)
static decimal Clamp(decimal value, decimal min, decimal max)
Definition Math.cs:453
static long Min(long val1, long val2)
Definition Math.cs:966
static double Round(double value, MidpointRounding mode)
Definition Math.cs:1151
static ulong ulong Remainder DivRem(ulong left, ulong right)
Definition Math.cs:405
static int Sign(long value)
Definition Math.cs:1235
static double Floor(double d)
static double Cosh(double value)
static double FusedMultiplyAdd(double x, double y, double z)
static sbyte sbyte Remainder DivRem(sbyte left, sbyte right)
Definition Math.cs:346
static decimal Abs(decimal value)
Definition Math.cs:216
static int Sign(short value)
Definition Math.cs:1225
const double Tau
Definition Math.cs:18
static double Round(double a)
Definition Math.cs:1120
static double MaxMagnitude(double x, double y)
Definition Math.cs:892
static long BigMul(int a, int b)
Definition Math.cs:228
static sbyte Max(sbyte val1, sbyte val2)
Definition Math.cs:817
static uint Clamp(uint value, uint min, uint max)
Definition Math.cs:618
static int Sign(nint value)
Definition Math.cs:1240
static nint Min(nint val1, nint val2)
Definition Math.cs:976
static int Sign(decimal value)
Definition Math.cs:1202
static unsafe double ModF(double x, double *intptr)
static unsafe double Truncate(double d)
Definition Math.cs:1275
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static int ILogB(double x)
static Vector128< float > ConditionalSelectBitwise(Vector128< float > selector, Vector128< float > ifTrue, Vector128< float > ifFalse)
Definition VectorMath.cs:11
static unsafe Vector128< byte > CreateScalarUnsafe(byte value)
Definition Vector128.cs:829
static ulong MultiplyNoFlags(ulong left, ulong right)
Definition Bmi2.cs:19
static new bool IsSupported
Definition Sse2.cs:60
static string Overflow_NegateTwosCompNum
Definition SR.cs:1776
static string Argument_MinMaxValue
Definition SR.cs:766
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentOutOfRange_RoundingDigits
Definition SR.cs:1098
static string Arithmetic_NaN
Definition SR.cs:1130
static string Argument_InvalidEnumValue
Definition SR.cs:18
Definition SR.cs:7