NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
math.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_NUMERIC_MATH_HPP__
2#define NEFORCE_CORE_NUMERIC_MATH_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
16NEFORCE_BEGIN_CONSTANTS__
17
37
38NEFORCE_INLINE17 constexpr decimal_t EULER = 2.71828182845904523536L;
39NEFORCE_INLINE17 constexpr decimal_t PI = 3.14159265358979323846L;
40NEFORCE_INLINE17 constexpr decimal_t PHI = 1.61803398874989484820L;
41NEFORCE_INLINE17 constexpr decimal_t SEMI_CIRCLE = 180.0L;
42NEFORCE_INLINE17 constexpr decimal_t CIRCLE = 360.0L;
43
45NEFORCE_INLINE17 constexpr decimal_t TWO_PI_HI = 6.28318530717958647692L;
47NEFORCE_INLINE17 constexpr decimal_t TWO_PI_LO = 2.44929359829470641435e-16L;
48
52NEFORCE_INLINE17 constexpr decimal_t DEFAULT_TOLERANCE = 1e-12L;
54NEFORCE_INLINE17 constexpr decimal_t LOOSE_TOLERANCE = 1e-9L;
55
61NEFORCE_INLINE17 constexpr uint64_t FIBONACCI_LIST[] = {
62 0, 1, 1, 2, 3, 5, 8, 13, 21,
63 34, 55, 89, 144, 233, 377, 610, 987, 1597,
64 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393,
65 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465,
66 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733,
67 1134903170, 1836311903, 2971215073, 4807526976, 7778742049};
68
69NEFORCE_INLINE17 constexpr uint32_t FIBONACCI_COUNT = extent_v<decltype(FIBONACCI_LIST)>;
70 // MathConstants
72
73NEFORCE_END_CONSTANTS__
74
147
148NEFORCE_CONST_FUNCTION constexpr int64_t safe_trunc(const decimal_t x) noexcept {
149 constexpr decimal_t max_int64 = static_cast<decimal_t>(numeric_traits<int64_t>::max());
150 constexpr decimal_t min_int64 = static_cast<decimal_t>(numeric_traits<int64_t>::min());
151 if (is_nan(x) || is_infinity(x) || x > max_int64 || x < min_int64) {
152 return 0;
153 }
154 return static_cast<int64_t>(x);
155}
156
165NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t fibonacci(const uint32_t n) noexcept {
166 if (n < constants::FIBONACCI_COUNT) {
167 return constants::FIBONACCI_LIST[n];
168 }
169 uint64_t a = constants::FIBONACCI_LIST[constants::FIBONACCI_COUNT - 2];
170 uint64_t b = constants::FIBONACCI_LIST[constants::FIBONACCI_COUNT - 1];
171 for (uint32_t i = constants::FIBONACCI_COUNT; i <= n; ++i) {
172 const uint64_t c = a + b;
173 a = b;
174 b = c;
175 }
176 return b;
177}
178
186NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t leonardo(const uint32_t n) noexcept {
187 return 2 * fibonacci(n + 1) - 1;
188}
189
196template <typename T>
197NEFORCE_PURE_FUNCTION constexpr T angular2radian(const T angular) noexcept {
198 static_assert(is_arithmetic_v<T>, "arithmetic required");
199 return angular * static_cast<T>(constants::PI) / static_cast<T>(constants::SEMI_CIRCLE);
200}
201
208template <typename T>
209NEFORCE_PURE_FUNCTION constexpr T radian2angular(const T radian) noexcept {
210 static_assert(is_arithmetic_v<T>, "arithmetic required");
211 return radian * (static_cast<T>(constants::SEMI_CIRCLE) / static_cast<T>(constants::PI));
212}
213
220template <typename T>
221NEFORCE_CONST_FUNCTION constexpr enable_if_t<is_signed_v<T>, T> absolute(const T x) noexcept {
222 return x > T(0) ? x : -x;
223}
224
231
232template <typename T>
233NEFORCE_CONST_FUNCTION constexpr enable_if_t<is_unsigned_v<T>, T> absolute(const T x) noexcept {
234 return x;
235}
236
245template <typename T>
246NEFORCE_CONST_FUNCTION constexpr const T& sum(const T& x) noexcept {
247 return x;
248}
249
258template <typename First, typename... Rests, enable_if_t<(sizeof...(Rests) > 0), int> = 0>
259NEFORCE_CONST_FUNCTION constexpr decltype(auto) sum(First first, Rests... args) {
260 return first + _NEFORCE sum(args...);
261}
262
269template <typename... Args, enable_if_t<(sizeof...(Args) > 0), int> = 0>
270NEFORCE_CONST_FUNCTION constexpr decltype(auto) average(Args... args) {
271 return _NEFORCE sum(args...) / sizeof...(Args);
272}
273
280template <typename T>
281NEFORCE_CONSTEXPR14 int sign(const T& value) noexcept {
282 static_assert(is_arithmetic_v<T>, "arithmetic required");
283 constexpr T zero = T(0);
284 if (value > zero) {
285 return 1;
286 }
287 if (value < zero) {
288 return -1;
289 }
290 return 0;
291}
292
300template <typename T>
301NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T gcd(const T& m, const T& n) noexcept {
302 T x = _NEFORCE absolute(m), y = _NEFORCE absolute(n);
303 constexpr T zero = T(0);
304 while (y != zero) {
305 T t = x % y;
306 x = y;
307 y = t;
308 }
309 return x;
310}
311
319template <typename T>
320NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T lcm(const T& m, const T& n) noexcept {
321 return (m / _NEFORCE gcd(m, n)) * n;
322}
323
332template <typename T>
333NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t<is_floating_point_v<T>, T> mod(const T x, const T y) {
334 if (_NEFORCE is_nan(x) || _NEFORCE is_nan(y)) {
336 }
337 if (y == 0) {
338 NEFORCE_THROW_EXCEPTION(math_exception("zero can not be dividend."));
339 }
340 if (_NEFORCE is_infinity(x) || _NEFORCE is_infinity(y)) {
342 }
343 return x - static_cast<make_integer_t<sizeof(T)>>(x / y) * y;
344}
345
354template <typename T>
355NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t<is_integral_v<T>, T> mod(const T x, const T y) {
356 if (y == 0) {
357 NEFORCE_THROW_EXCEPTION(math_exception("zero can not be dividend."));
358 }
359 return x % y;
360}
361
371template <typename T>
372NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T power(const T& x, uint32_t n) noexcept {
373 static_assert(is_arithmetic_v<T>, "arithmetic required");
374 if (n == 0) {
375 return 1;
376 }
377 T result(1);
378 T base = x;
379 while (n > 0) {
380 if (n % 2 == 1) {
381 result *= base;
382 }
383 base *= base;
384 n /= 2;
385 }
386 return result;
387}
388
394NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t exponential(const uint32_t n) noexcept {
395 return power(constants::EULER, n);
396}
397
405NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_e(const decimal_t x) noexcept {
406 if (is_nan(x)) {
407 return x;
408 }
409 if (x < 0.0L) {
411 }
412 if (x == 0.0L) {
414 }
415 if (is_infinity(x) && x > 0) {
416 return x;
417 }
418
419 int64_t k = 0;
420 decimal_t m = x;
421
422 if (m >= 2.0L) {
423 while (m >= 2.0L) {
424 m *= 0.5L;
425 ++k;
426 }
427 } else if (m < 1.0L) {
428 while (m < 1.0L) {
429 m *= 2.0L;
430 --k;
431 }
432 }
433
434 const decimal_t a = (m - 1.0L) / (m + 1.0L);
435 const decimal_t a2 = a * a;
436 decimal_t term = a;
437 decimal_t s = a;
438 decimal_t n = 1.0L;
439
440 while (absolute(term) > constants::MACHINE_EPSILON * absolute(s)) {
441 term *= a2;
442 n += 2.0L;
443 s += term / n;
444 }
445
446 constexpr decimal_t LN2 = 0.69314718055994530941723212145817656807L;
447 return 2.0L * s + static_cast<decimal_t>(k) * LN2;
448}
449
456NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm(const decimal_t x, const uint32_t base) {
457 const decimal_t under = logarithm_e(base);
458 if (under == 0.0L) {
459 NEFORCE_THROW_EXCEPTION(math_exception("zero can not be dividend."));
460 }
461 return logarithm_e(x) / under;
462}
463
469NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_2(const decimal_t x) { return logarithm(x, 2); }
470
476NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_10(const decimal_t x) { return logarithm(x, 10); }
477
486NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t
487square_root(const decimal_t x, const decimal_t precise = constants::DEFAULT_TOLERANCE) noexcept {
488 if (is_nan(x)) {
489 return x;
490 }
491 if (x < 0.0L) {
493 }
494 if (x == 0.0L) {
495 return x;
496 }
497 if (is_infinity(x) && x > 0) {
498 return x;
499 }
500
501 decimal_t guess = x * 0.5L;
503 do {
504 prev = guess;
505 guess = 0.5L * (prev + x / prev);
506 } while (absolute(guess - prev) > precise * absolute(guess));
507 return guess;
508}
509
518NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t
519cube_root(const decimal_t x, const decimal_t precise = constants::DEFAULT_TOLERANCE) noexcept {
520 if (is_nan(x)) {
521 return x;
522 }
523 if (is_infinity(x)) {
524 return x;
525 }
526 if (x == 0.0L) {
527 return x;
528 }
529
530 const bool negative = x < 0.0L;
531 const decimal_t v = negative ? -x : x;
532
533 decimal_t guess = v > 1.0L ? v / 3.0L : v;
535 do {
536 prev = guess;
537 guess = (2.0L * prev + v / (prev * prev)) / 3.0L;
538 } while (absolute(guess - prev) > precise * absolute(guess));
539
540 return negative ? -guess : guess;
541}
542
548NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 uint64_t factorial(const uint32_t n) noexcept {
549 uint64_t result = 1;
550 for (uint32_t i = 2; i <= n; ++i) {
551 result *= i;
552 }
553 return result;
554}
555
561NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor(const decimal_t x) noexcept {
562 if (!is_finite(x)) {
563 return x;
564 }
565 const int64_t i = safe_trunc(x);
566 const decimal_t di = static_cast<decimal_t>(i);
567 return (x < 0.0L && x != di) ? di - 1.0L : di;
568}
569
576NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor(const decimal_t x, const uint32_t bit) noexcept {
577 const decimal_t factor = power(10.0L, bit);
578 return floor(x * factor) / factor;
579}
580
586NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil(const decimal_t x) noexcept {
587 if (!is_finite(x)) {
588 return x;
589 }
590 const int64_t i = safe_trunc(x);
591 const decimal_t di = static_cast<decimal_t>(i);
592 return (x > 0.0L && x != di) ? di + 1.0L : di;
593}
594
601NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil(const decimal_t x, const uint32_t bit) noexcept {
602 const decimal_t factor = power(10.0L, bit);
603 return ceil(x * factor) / factor;
604}
605
611NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round(const decimal_t x) noexcept {
612 if (!is_finite(x)) {
613 return x;
614 }
615
616 const decimal_t int_part = floor(absolute(x));
617 const decimal_t frac = absolute(x) - int_part;
618 if (frac < 0.5L) {
619 return x < 0 ? -int_part : int_part;
620 }
621 if (frac > 0.5L) {
622 return x < 0 ? -(int_part + 1) : (int_part + 1);
623 }
624
625 if (static_cast<int64_t>(int_part) % 2 == 0) {
626 return x < 0 ? -int_part : int_part;
627 } else {
628 return x < 0 ? -(int_part + 1) : (int_part + 1);
629 }
630}
631
632
639NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor_bit(const decimal_t x, const uint32_t bit) noexcept {
640 const decimal_t factor = power(10.0L, bit);
641 return floor(x * factor) / factor;
642}
643
650NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil_bit(const decimal_t x, const uint32_t bit) noexcept {
651 const decimal_t factor = power(10.0L, bit);
652 return ceil(x * factor) / factor;
653}
654
661NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round_bit(const decimal_t x, const uint32_t bit) noexcept {
662 return x < 0 ? ceil_bit(x - 0.5 / power(10.0, bit), bit) : floor_bit(x + 0.5 / power(10.0, bit), bit);
663}
664
671NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate_bit(const decimal_t x, const uint32_t bit) noexcept {
672 return x < 0 ? ceil_bit(x, bit) : floor_bit(x, bit);
673}
674
675
682NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round(const decimal_t x, const uint32_t bit) noexcept {
683 const decimal_t factor = power(10.0L, bit);
684 return round(x * factor) / factor;
685}
686
693NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate(const decimal_t x, const int bit) noexcept {
694 const decimal_t factor = power(10.0L, static_cast<uint32_t>(absolute(bit)));
695 return (x < 0 ? ceil(x * factor, 0) : floor(x * factor, 0)) / factor;
696}
697
703NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate(const decimal_t x) noexcept {
704 if (!is_finite(x)) {
705 return x;
706 }
707 return static_cast<decimal_t>(safe_trunc(x));
708}
709
718NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_multiple(const decimal_t x, const decimal_t axis,
719 const decimal_t toler = constants::DEFAULT_TOLERANCE) {
720 if (absolute(axis) < constants::MACHINE_EPSILON) {
721 NEFORCE_THROW_EXCEPTION(math_exception("Axis Cannot be 0"));
722 }
723 const decimal_t multi = round(x / axis) * axis;
724 return absolute(x - multi) < toler;
725}
726
733NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_pi(const decimal_t x,
734 const decimal_t toler = constants::LOOSE_TOLERANCE) {
735 return around_multiple(x, constants::PI, toler);
736}
737
744NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool
745around_zero(const decimal_t x, const decimal_t toler = constants::LOOSE_TOLERANCE) noexcept {
746 return absolute(x) < toler;
747}
748
749
758NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t remainder(const decimal_t x, const decimal_t y) noexcept {
759 if (is_nan(x) || is_nan(y) || is_infinity(x) || y == 0.0L) {
761 }
762 return x - y * round(x / y);
763}
764
770NEFORCE_CONST_FUNCTION constexpr decimal_t float_part(const decimal_t x) noexcept {
771 return x - static_cast<decimal_t>(safe_trunc(x));
772}
773
782NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t float_apart(decimal_t x, int64_t* int_ptr) noexcept {
783 *int_ptr = safe_trunc(x);
784 return x - static_cast<decimal_t>(*int_ptr);
785}
786
788NEFORCE_BEGIN_INNER__
789
790NEFORCE_CONSTEXPR14 void reduce_arg_sincos(decimal_t& x, int& quadrant) noexcept {
791 if (x < 0.0L) {
792 x = -x;
793 quadrant = 3;
794 } else {
795 quadrant = 0;
796 }
797
798 constexpr decimal_t HALF_PI_HI = 1.57079632679489661923L;
799 constexpr decimal_t HALF_PI_LO = 6.12323399573676603587e-17L;
800 constexpr decimal_t INV_HALF_PI = 0.63661977236758134308L;
801
802 const decimal_t kd = round(x * INV_HALF_PI);
803 const int64_t k = static_cast<int64_t>(kd);
804 quadrant = (quadrant + (k & 3)) & 3;
805
806 decimal_t t = kd * HALF_PI_HI;
807 x = x - t;
808 t = kd * HALF_PI_LO;
809 x = x - t;
810}
811
812NEFORCE_CONSTEXPR14 void sincos_taylor(const decimal_t x, decimal_t& sin_val, decimal_t& cos_val) noexcept {
813 const decimal_t x2 = x * x;
814
815 decimal_t term_sin = x;
816 decimal_t sum_sin = x;
817
818 decimal_t term_cos = 1.0L;
819 decimal_t sum_cos = 1.0L;
820
821 decimal_t i = 1.0L;
822 bool sin_done = false, cos_done = false;
823
824 while (!sin_done || !cos_done) {
825 if (!sin_done) {
826 term_sin = -term_sin * x2 / ((i + 1.0L) * (i + 2.0L));
827 sum_sin += term_sin;
828 if (absolute(term_sin) <= constants::MACHINE_EPSILON * absolute(sum_sin)) {
829 sin_done = true;
830 }
831 }
832 if (!cos_done) {
833 term_cos = -term_cos * x2 / (i * (i + 1.0L));
834 sum_cos += term_cos;
835 if (absolute(term_cos) <= constants::MACHINE_EPSILON * absolute(sum_cos)) {
836 cos_done = true;
837 }
838 }
839 i += 2.0L;
840 }
841
842 sin_val = sum_sin;
843 cos_val = sum_cos;
844}
845
846NEFORCE_END_INNER__
848
856NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t sine(decimal_t x) noexcept {
857 if (is_nan(x) || is_infinity(x)) {
859 }
860
861 int quadrant;
862 inner::reduce_arg_sincos(x, quadrant);
863
864 decimal_t sin_x, cos_x;
865 inner::sincos_taylor(x, sin_x, cos_x);
866
867 switch (quadrant) {
868 case 0:
869 return sin_x;
870 case 1:
871 return cos_x;
872 case 2:
873 return -sin_x;
874 default:
875 return -cos_x;
876 }
877}
878
886NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cosine(decimal_t x) noexcept {
887 if (is_nan(x) || is_infinity(x)) {
889 }
890
891 int quadrant;
892 inner::reduce_arg_sincos(x, quadrant);
893 decimal_t sin_x, cos_x;
894 inner::sincos_taylor(x, sin_x, cos_x);
895
896 switch (quadrant) {
897 case 0:
898 return cos_x;
899 case 1:
900 return -sin_x;
901 case 2:
902 return -cos_x;
903 default:
904 return sin_x;
905 }
906}
907
914NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t tangent(decimal_t x) noexcept {
915 if (is_nan(x) || is_infinity(x)) {
917 }
918
919 int quadrant;
920 inner::reduce_arg_sincos(x, quadrant);
921 decimal_t sin_x, cos_x;
922 inner::sincos_taylor(x, sin_x, cos_x);
923
924 decimal_t s, c;
925 switch (quadrant) {
926 case 0:
927 s = sin_x;
928 c = cos_x;
929 break;
930 case 1:
931 s = cos_x;
932 c = -sin_x;
933 break;
934 case 2:
935 s = -sin_x;
936 c = -cos_x;
937 break;
938 default:
939 s = -cos_x;
940 c = sin_x;
941 break;
942 }
943
944 if (around_zero(c, constants::LOOSE_TOLERANCE)) {
945 constexpr auto inf = numeric_traits<decimal_t>::infinity();
946 return (s > 0) ? inf : -inf;
947 }
948 return s / c;
949}
950
957NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cotangent(const decimal_t x) { return 1.0L / tangent(x); }
958
966NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arctangent(decimal_t x) noexcept {
967 if (is_nan(x)) {
968 return x;
969 }
970 if (is_infinity(x)) {
971 return (x > 0) ? constants::PI / 2 : -constants::PI / 2;
972 }
973
974 bool negative = x < 0.0L;
975 if (negative) {
976 x = -x;
977 }
978
979 bool recip = false;
980 if (x > 1.0L) {
981 x = 1.0L / x;
982 recip = true;
983 }
984
985 x = x / (1.0L + square_root(1.0L + x * x));
986 x = x / (1.0L + square_root(1.0L + x * x));
987
988 const decimal_t x2 = x * x;
989 decimal_t term = x, s = x, n = 1.0L;
990 while (absolute(term) > constants::MACHINE_EPSILON * absolute(s)) {
991 term *= -x2;
992 n += 2.0L;
993 s += term / n;
994 }
995
996 decimal_t result = 4.0L * s;
997 if (recip) {
998 result = constants::PI / 2 - result;
999 }
1000 if (negative) {
1001 result = -result;
1002 }
1003 return result;
1004}
1005
1011NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arcsine(const decimal_t x) noexcept {
1012 if (is_nan(x)) {
1013 return x;
1014 }
1015 if (absolute(x) > 1.0L) {
1017 }
1018 if (x == 1.0L) {
1019 return constants::PI / 2;
1020 }
1021 if (x == -1.0L) {
1022 return -constants::PI / 2;
1023 }
1024 return arctangent(x / square_root(1.0L - x * x));
1025}
1026
1032NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arccosine(const decimal_t x) noexcept {
1033 if (is_nan(x)) {
1034 return x;
1035 }
1036 if (absolute(x) > 1.0L) {
1038 }
1039 if (x == 1.0L) {
1040 return 0.0L;
1041 }
1042 if (x == -1.0L) {
1043 return constants::PI;
1044 }
1045 return constants::PI / 2 - arcsine(x);
1046}
1047 // MathFunctions
1049
1050NEFORCE_END_NAMESPACE__
1051#endif // NEFORCE_CORE_NUMERIC_MATH_HPP__
static NEFORCE_NODISCARD constexpr T quiet_nan() noexcept
获取安静nan表示
static NEFORCE_NODISCARD constexpr T epsilon() noexcept
获取机器精度
static NEFORCE_NODISCARD constexpr T min() noexcept
获取类型的最小值
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
static NEFORCE_NODISCARD constexpr T infinity() noexcept
获取正无穷大表示
异常处理框架
NEFORCE_INLINE17 constexpr size_t extent_v
extent的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_arithmetic_v
is_arithmetic的便捷变量模板
unsigned int uint32_t
32位无符号整数类型
long double decimal_t
扩展精度浮点数类型
long long int64_t
64位有符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr Iterator prev(Iterator iter, iter_difference_t< Iterator > n=-1)
获取迭代器的前一个位置
NEFORCE_INLINE17 constexpr decimal_t SEMI_CIRCLE
半圆角度 180° (角度制)
NEFORCE_INLINE17 constexpr decimal_t TWO_PI_HI
高精度 2π 高位
NEFORCE_INLINE17 constexpr decimal_t LOOSE_TOLERANCE
宽松容差
NEFORCE_INLINE17 constexpr decimal_t PI
圆周率 π (弧度制)
NEFORCE_INLINE17 constexpr decimal_t DEFAULT_TOLERANCE
默认容差
NEFORCE_INLINE17 constexpr decimal_t EULER
自然常数 e
NEFORCE_INLINE17 constexpr decimal_t CIRCLE
全圆角度 360° (角度制)
NEFORCE_INLINE17 constexpr uint64_t FIBONACCI_LIST[]
预计算的斐波那契数列
NEFORCE_INLINE17 constexpr uint32_t FIBONACCI_COUNT
斐波那契数列预计算数量
NEFORCE_INLINE17 constexpr decimal_t PHI
黄金分割比 φ
NEFORCE_INLINE17 constexpr decimal_t TWO_PI_LO
高精度 2π 低位
NEFORCE_INLINE17 constexpr decimal_t MACHINE_EPSILON
机器精度
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t sine(decimal_t x) noexcept
计算正弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate(const decimal_t x, const int bit) noexcept
截断
NEFORCE_CONST_FUNCTION constexpr decimal_t float_part(const decimal_t x) noexcept
获取小数部分
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor(const decimal_t x) noexcept
向下取整
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t floor_bit(const decimal_t x, const uint32_t bit) noexcept
向下舍入到指定位数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t leonardo(const uint32_t n) noexcept
计算莱昂纳多数
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil_bit(const decimal_t x, const uint32_t bit) noexcept
向上舍入到指定位数
NEFORCE_CONST_FUNCTION constexpr enable_if_t< is_signed_v< T >, T > absolute(const T x) noexcept
取绝对值(有符号数版本)
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_2(const decimal_t x)
计算以2为底的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arctangent(decimal_t x) noexcept
计算反正切值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t square_root(const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
计算平方根
NEFORCE_PURE_FUNCTION constexpr T radian2angular(const T radian) noexcept
弧度转角度
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arcsine(const decimal_t x) noexcept
计算反正弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_zero(const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE) noexcept
判断是否接近零
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_10(const decimal_t x)
计算以10为底的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 uint64_t fibonacci(const uint32_t n) noexcept
计算斐波那契数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cotangent(const decimal_t x)
计算余切值
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm(const decimal_t x, const uint32_t base)
计算任意底数的对数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cosine(decimal_t x) noexcept
计算余弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t logarithm_e(const decimal_t x) noexcept
计算自然对数
NEFORCE_CONST_FUNCTION constexpr const T & sum(const T &x) noexcept
单参数求和
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_pi(const decimal_t x, const decimal_t toler=constants::LOOSE_TOLERANCE)
判断是否接近π的整数倍
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t float_apart(decimal_t x, int64_t *int_ptr) noexcept
分离整数和小数部分
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T lcm(const T &m, const T &n) noexcept
计算最小公倍数
NEFORCE_CONST_FUNCTION constexpr decltype(auto) average(Args... args)
计算平均值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 uint64_t factorial(const uint32_t n) noexcept
计算阶乘
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T power(const T &x, uint32_t n) noexcept
幂运算
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t ceil(const decimal_t x) noexcept
向上取整
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool around_multiple(const decimal_t x, const decimal_t axis, const decimal_t toler=constants::DEFAULT_TOLERANCE)
判断是否接近某个倍数值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round(const decimal_t x) noexcept
四舍五入
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 T gcd(const T &m, const T &n) noexcept
计算最大公约数
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t remainder(const decimal_t x, const decimal_t y) noexcept
计算余数
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t arccosine(const decimal_t x) noexcept
计算反余弦值
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 enable_if_t< is_floating_point_v< T >, T > mod(const T x, const T y)
浮点数取模运算
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t tangent(decimal_t x) noexcept
计算正切值
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t exponential(const uint32_t n) noexcept
计算e的n次幂
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 decimal_t cube_root(const decimal_t x, const decimal_t precise=constants::DEFAULT_TOLERANCE) noexcept
计算立方根
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t round_bit(const decimal_t x, const uint32_t bit) noexcept
四舍五入到指定位数
NEFORCE_PURE_FUNCTION constexpr T angular2radian(const T angular) noexcept
角度转弧度
NEFORCE_CONSTEXPR14 int sign(const T &value) noexcept
获取数值的符号
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 decimal_t truncate_bit(const decimal_t x, const uint32_t bit) noexcept
截断到指定位数
NEFORCE_CONST_FUNCTION constexpr bool is_nan(const T x) noexcept
检查浮点数是否为NaN
NEFORCE_CONST_FUNCTION constexpr bool is_infinity(const T x) noexcept
检查浮点数是否为无穷大
NEFORCE_CONST_FUNCTION constexpr bool is_finite(const T x) noexcept
检查浮点数是否为有限值
typename make_integer< Size, IsSigned >::type make_integer_t
make_integer的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
数值类型特性检查
数学计算异常