-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdm_maths.hpp
8063 lines (7312 loc) · 208 KB
/
cdm_maths.hpp
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
/* cdm_maths v3.0.0
C++20 geometric library
https://github.com/WubiCookie/cdm
no warranty implied; use at your own risk
LICENSE
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2022 Charles Seizilles de Mazancourt <charles DOT de DOT
mazancourt AT hotmail DOT fr>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
CREDITS
Written by Charles Seizilles de Mazancourt
*/
#ifndef CDM_MATHS_HPP
#define CDM_MATHS_HPP 1
#include <cdm_concepts.hpp>
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
#include <optional>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
namespace cdm
{
#pragma region constants declarations
// clang-format off
constexpr uint64_t version_major = 3;
constexpr uint64_t version_minor = 0;
constexpr uint64_t version_patch = 0;
using std::numbers::pi;
using std::numbers::inv_pi;
using std::numbers::inv_sqrtpi;
constexpr double deg_to_rad = pi / 180.0;
constexpr double rad_to_deg = 180.0 / pi;
using std::numbers::sqrt2;
using std::numbers::sqrt3;
constexpr double inv_sqrt2 = 0.7071067811865475244008443621048490392848359376884740365883398689;
using std::numbers::inv_sqrt3;
constexpr double sqrt3_over_2 = 0.8660254037844386467637231707529361834714026269051903140279034897;
constexpr double epsilon = 1.0e-05;
// clang-format on
#pragma endregion
#pragma region type_traits_and_concepts
// clang-format off
namespace detail
{
template <typename T> struct is_vector : std::false_type {};
template <typename T> inline constexpr bool is_vector_v = is_vector<T>::value;
template <typename T> struct is_matrix : std::false_type {};
template <typename T> inline constexpr bool is_matrix_v = is_matrix<T>::value;
} // namespace detail
template <typename T> concept vector = detail::is_vector_v<T>;
template <typename T> concept matrix = detail::is_matrix_v<T>;
template <typename T> concept normalizable = requires(T& t, const T& ct)
{
{ t.normalize() } -> std::convertible_to<T>;
{ t.get_normalized() } -> std::convertible_to<T>;
{ ct.get_normalized() } -> std::convertible_to<T>;
};
// clang-format on
#pragma endregion
#pragma region forward_declarations
template <normalizable T>
class normalized;
template <arithmetic T>
struct complex_T;
template <arithmetic T>
struct radian_T;
template <arithmetic T>
struct degree_T;
template <std::signed_integral T>
struct pi_fraction_T;
template <std::signed_integral T, T NumeratorT, T DenominatorT>
struct static_pi_fraction_T;
template <arithmetic T>
struct vector2_T;
template <arithmetic T>
struct vector3_T;
template <arithmetic T>
struct vector4_T;
template <arithmetic T>
struct matrix2_T;
template <arithmetic T>
struct matrix3_T;
template <arithmetic T>
struct matrix4_T;
template <arithmetic T>
struct perspective_T;
template <arithmetic T>
struct direction2_T;
template <arithmetic T>
struct direction3_T;
template <arithmetic T>
struct euler_angles_T;
template <arithmetic T>
struct quaternion_T;
enum class line_representation
{
SlopeIntercept,
Points,
PointAngleDegree,
PointAngleRadian,
PointAnglePiFraction,
};
template <arithmetic T, line_representation representation>
struct line_T;
template <arithmetic T>
struct segment2_T;
template <arithmetic T>
struct segment3_T;
template <arithmetic T>
struct plane_T;
template <arithmetic T>
struct oriented_plane_T;
template <arithmetic T>
struct circle_T;
template <arithmetic T>
struct ray2_T;
template <arithmetic T>
struct ray3_T;
template <arithmetic T>
struct aabb2_T;
template <arithmetic T>
struct aabb3_T;
template <arithmetic T>
struct transform2_T;
template <arithmetic T>
struct transform3_T;
template <arithmetic T>
struct uniform_transform2_T;
template <arithmetic T>
struct uniform_transform3_T;
template <arithmetic T>
struct unscaled_transform2_T;
template <arithmetic T>
struct unscaled_transform3_T;
template <typename T>
requires arithmetic<T> || vector<T>
class value_domain_T;
template <typename T>
class unnormalized_value_T;
template <typename T>
class normalized_value_T;
#pragma endregion
#pragma region misc
namespace detail
{
template <int x, int y, arithmetic T>
constexpr T get_quaternion_t_matrix_element(quaternion_T<T> q)
{
static_assert(x < 3, "x must be [0;2]");
static_assert(y < 3, "y must be [0;2]");
if constexpr (y == 0)
{
if constexpr (x == 0)
return T(1) - T(2) * (q.y * q.y + q.z * q.z);
if constexpr (x == 1)
return T(2) * (q.x * q.y - q.z * q.w);
if constexpr (x == 2)
return T(2) * (q.x * q.z + q.y * q.w);
}
if constexpr (y == 1)
{
if constexpr (x == 0)
return T(2) * (q.x * q.y + q.z * q.w);
if constexpr (x == 1)
return T(1) - T(2) * (q.x * q.x + q.z * q.z);
if constexpr (x == 2)
return T(2) * (q.y * q.z - q.x * q.w);
}
if constexpr (y == 2)
{
if constexpr (x == 0)
return T(2) * (q.x * q.z - q.y * q.w);
if constexpr (x == 1)
return T(2) * (q.y * q.z + q.x * q.w);
if constexpr (x == 2)
return T(1) - T(2) * (q.x * q.x + q.y * q.y);
}
}
} // namespace detail
template <typename Functor, arithmetic T>
constexpr std::vector<vector3_T<T>> function2D_sampler(const Functor& functor,
T min,
T max,
T step)
{
std::vector<vector3_T<T>> res;
if (min > max)
std::swap(min, max);
if (step < epsilon)
step = epsilon;
for (T f = min; f < max; f += step)
{
res.push_back(functor(f));
}
return res;
}
template <typename Functor, arithmetic T>
constexpr std::vector<std::vector<vector3_T<T>>> function3D_sampler(
const Functor& functor,
vector2_T<T> min,
vector2_T<T> max,
vector2_T<T> step)
{
std::vector<std::vector<vector3_T<T>>> res;
if (min.x > max.x)
std::swap(min.x, max.x);
if (min.y > max.y)
std::swap(min.y, max.y);
if (step.x < epsilon)
step.x = epsilon;
if (step.y < epsilon)
step.y = epsilon;
size_t xCount = 0;
size_t yCount = 0;
for (T x = min.x; x < max.x; x += step.x)
xCount++;
for (T y = min.y; y < max.y; y += step.y)
yCount++;
res.reserve(yCount);
for (T y = min.y; y < max.y; y += step.y)
{
std::vector<vector3_T<T>> row;
row.reserve(xCount);
for (T x = min.x; x < max.x; x += step.x)
{
row.push_back(functor(x, y));
}
res.push_back(std::move(row));
}
return res;
}
// template <arithmetic T>
// constexpr T lerp(T begin, T end, T percent)
//{
// return std::lerp(begin, end, percent);
// }
template <typename T>
requires arithmetic<T> || vector<T>
constexpr T clamp(T value, T min, T max)
{
if constexpr (arithmetic<T>)
return std::clamp(value, min, max);
else
return value.clamp(min, max);
}
template <arithmetic T>
bool nearly_equal(T f1, T f2, T e = T(epsilon))
{
T f = f1 - f2;
f = f < T(0) ? -f : f;
return f < e;
}
template <arithmetic T>
constexpr int sign(T val)
{
return (T(0) <= val) - (val < T(0));
}
template <arithmetic T>
constexpr int signnum(T val)
{
return (T(0) < val) - (val < T(0));
}
template <arithmetic T>
constexpr T element_wise_min(T v0, T v1)
{
return std::min(v0, v1);
}
template <arithmetic T>
constexpr T element_wise_max(T v0, T v1)
{
return std::max(v0, v1);
}
template <arithmetic T>
constexpr T element_wise_abs(T v)
{
return std::abs(v);
}
template <arithmetic T>
constexpr T element_wise_lerp(T begin, T end, T percent)
{
return std::lerp(begin, end, percent);
}
template <typename T>
requires arithmetic<T> || vector<T> || matrix<T>
constexpr T zero()
{
if constexpr (arithmetic<T>)
return T(0);
else
return T::zero();
}
template <typename T>
requires arithmetic<T> || vector<T> || matrix<T>
constexpr T one()
{
if constexpr (arithmetic<T>)
return T(1);
else
return T::one();
}
#pragma endregion
#pragma region type_traits_and_concepts_details
// clang-format off
namespace detail
{
template <typename T> struct is_vector<vector2_T<T>> : std::true_type {};
template <typename T> struct is_vector<vector4_T<T>> : std::true_type {};
template <typename T> struct is_vector<vector3_T<T>> : std::true_type {};
template <typename T> struct is_vector<direction2_T<T>> : std::true_type {};
template <typename T> struct is_vector<direction3_T<T>> : std::true_type {};
template <typename T> struct is_matrix<matrix2_T<T>> : std::true_type {};
template <typename T> struct is_matrix<matrix3_T<T>> : std::true_type {};
template <typename T> struct is_matrix<matrix4_T<T>> : std::true_type {};
} // namespace detail
// clang-format on
#pragma endregion
#pragma region declarations
#pragma region declaration_complex_T
template <arithmetic T>
struct complex_T
{
T r;
T i;
complex_T() = default;
complex_T(T r_, T i_) : r{r_}, i{i_} {}
explicit complex_T(radian_T<T> angle);
complex_T(const complex_T&) = default;
complex_T(complex_T&&) = default;
complex_T& operator=(const complex_T&) = default;
complex_T& operator=(complex_T&&) = default;
T norm() const;
constexpr T norm_squared() const;
complex_T& normalize();
complex_T get_normalized() const;
constexpr complex_T& conjugate();
constexpr complex_T get_conjugated() const;
constexpr complex_T operator+(complex_T c) const;
constexpr complex_T operator-(complex_T c) const;
constexpr complex_T& operator+=(complex_T c);
constexpr complex_T& operator-=(complex_T c);
constexpr complex_T& operator*=(complex_T c);
static constexpr complex_T identity();
using underlying_type = T;
};
template <arithmetic T>
constexpr complex_T<T> operator*(complex_T<T> c1, complex_T<T> c2);
template <arithmetic T>
constexpr complex_T<T> operator*(complex_T<T> c, T f);
template <arithmetic T>
constexpr complex_T<T> operator*(normalized<complex_T<T>> c1, complex_T<T> c2);
template <arithmetic T>
constexpr complex_T<T> operator*(complex_T<T> c1, normalized<complex_T<T>> c2);
template <arithmetic T>
constexpr normalized<complex_T<T>> operator*(normalized<complex_T<T>> c1,
normalized<complex_T<T>> c2);
template <arithmetic T>
constexpr vector2_T<T> operator*(normalized<complex_T<T>> c, vector2_T<T> v);
template <arithmetic T>
T norm(complex_T<T> c);
template <arithmetic T>
constexpr T norm_squared(complex_T<T> c);
template <arithmetic T>
complex_T<T> normalize(complex_T<T> c);
template <arithmetic T>
constexpr complex_T<T> conjugate(complex_T<T> c);
#pragma endregion
#pragma region declaration_radian_T
template <arithmetic T>
struct radian_T
{
private:
T angle;
public:
radian_T() = default;
explicit constexpr radian_T(T f);
radian_T(const radian_T&) = default;
radian_T(radian_T&&) = default;
constexpr radian_T(degree_T<T> d);
explicit constexpr operator T() const;
template <arithmetic U>
explicit constexpr operator radian_T<U>() const
{
return radian_T<U>{U(angle)};
}
constexpr radian_T& operator+=(radian_T r);
constexpr radian_T& operator-=(radian_T r);
constexpr radian_T& operator*=(radian_T r);
constexpr radian_T& operator/=(radian_T r);
constexpr radian_T operator-() const;
constexpr radian_T& operator=(const radian_T&) = default;
constexpr radian_T& operator=(degree_T<T> d);
using underlying_type = T;
};
template <arithmetic T>
constexpr radian_T<T> operator+(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr radian_T<T> operator-(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr radian_T<T> operator*(T, radian_T<T>);
template <arithmetic T>
constexpr radian_T<T> operator*(radian_T<T>, T);
template <arithmetic T>
constexpr radian_T<T> operator/(radian_T<T>, T);
template <arithmetic T>
constexpr radian_T<T>& operator+=(radian_T<T>&, T);
template <arithmetic T>
constexpr radian_T<T>& operator-=(radian_T<T>&, T);
template <arithmetic T>
constexpr radian_T<T>& operator*=(radian_T<T>&, T);
template <arithmetic T>
constexpr radian_T<T>& operator/=(radian_T<T>&, T);
template <arithmetic T>
constexpr bool operator<(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr bool operator>(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr bool operator==(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr bool operator!=(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr bool operator>=(radian_T<T>, radian_T<T>);
template <arithmetic T>
constexpr bool operator<=(radian_T<T>, radian_T<T>);
template <arithmetic T>
T sin(radian_T<T>);
template <arithmetic T>
T cos(radian_T<T>);
template <arithmetic T>
T tan(radian_T<T>);
template <arithmetic T>
T asin(radian_T<T>);
template <arithmetic T>
T acos(radian_T<T>);
template <arithmetic T>
T atan(radian_T<T>);
template <arithmetic T>
T sinh(radian_T<T>);
template <arithmetic T>
T cosh(radian_T<T>);
template <arithmetic T>
T tanh(radian_T<T>);
template <arithmetic T>
T asinh(radian_T<T>);
template <arithmetic T>
T acosh(radian_T<T>);
template <arithmetic T>
T atanh(radian_T<T>);
#pragma endregion
#pragma region declaration_degree_T
template <arithmetic T>
struct degree_T
{
private:
T angle;
public:
degree_T() = default;
explicit constexpr degree_T(T f);
degree_T(const degree_T&) = default;
degree_T(degree_T&&) = default;
constexpr degree_T(radian_T<T> r);
explicit constexpr operator T() const;
template <arithmetic U>
explicit constexpr operator degree_T<U>() const
{
return degree_T<U>{U(angle)};
}
constexpr degree_T& operator+=(degree_T d);
constexpr degree_T& operator-=(degree_T d);
constexpr degree_T& operator*=(degree_T d);
constexpr degree_T& operator/=(degree_T d);
constexpr degree_T operator-() const;
constexpr degree_T& operator=(const degree_T&) = default;
constexpr degree_T& operator=(radian_T<T> r);
using underlying_type = T;
};
template <arithmetic T>
constexpr degree_T<T> operator+(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr degree_T<T> operator-(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr degree_T<T> operator*(T, degree_T<T>);
template <arithmetic T>
constexpr degree_T<T> operator*(degree_T<T>, T);
template <arithmetic T>
constexpr degree_T<T> operator/(degree_T<T>, T);
template <arithmetic T>
constexpr degree_T<T>& operator+=(degree_T<T>&, T);
template <arithmetic T>
constexpr degree_T<T>& operator-=(degree_T<T>&, T);
template <arithmetic T>
constexpr degree_T<T>& operator*=(degree_T<T>&, T);
template <arithmetic T>
constexpr degree_T<T>& operator/=(degree_T<T>&, T);
template <arithmetic T>
constexpr bool operator<(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr bool operator>(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr bool operator==(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr bool operator!=(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr bool operator>=(degree_T<T>, degree_T<T>);
template <arithmetic T>
constexpr bool operator<=(degree_T<T>, degree_T<T>);
template <arithmetic T>
T sin(degree_T<T>);
template <arithmetic T>
T cos(degree_T<T>);
template <arithmetic T>
T tan(degree_T<T>);
template <arithmetic T>
T asin(degree_T<T>);
template <arithmetic T>
T acos(degree_T<T>);
template <arithmetic T>
T atan(degree_T<T>);
template <arithmetic T>
T sinh(degree_T<T>);
template <arithmetic T>
T cosh(degree_T<T>);
template <arithmetic T>
T tanh(degree_T<T>);
template <arithmetic T>
T asinh(degree_T<T>);
template <arithmetic T>
T acosh(degree_T<T>);
template <arithmetic T>
T atanh(degree_T<T>);
#pragma endregion
#pragma region declaration_pi_fraction_T
template <std::signed_integral T>
struct pi_fraction_T
{
T numerator;
T denominator;
pi_fraction_T() = default;
constexpr pi_fraction_T(T num, T den) : numerator{num}, denominator{den} {}
pi_fraction_T(const pi_fraction_T&) = default;
pi_fraction_T(pi_fraction_T&&) = default;
pi_fraction_T& operator=(const pi_fraction_T&) = default;
pi_fraction_T& operator=(pi_fraction_T&&) = default;
template <arithmetic U>
constexpr operator radian_T<U>() const
{
return radian_T<U>((U(numerator) * U(pi)) / U(denominator));
}
template <arithmetic U>
constexpr operator degree_T<U>() const
{
return operator radian_T<U>();
}
constexpr pi_fraction_T operator-() const
{
return pi_fraction_T{-numerator, denominator};
}
using underlying_type = T;
};
template <arithmetic U, arithmetic T>
U sin(pi_fraction_T<T> d)
{
if constexpr (d.numerator == T(0))
return U(0);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(1)>{})
return U(0);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(2)>{})
return U(1);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(3)>{})
return U(sqrt3_over_2);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(4)>{})
return U(inv_sqrt2);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(6)>{})
return U(0.5);
else if constexpr (d == static_pi_fraction_T<T, T(-1), T(1)>{})
return U(-0);
else if constexpr (d == static_pi_fraction_T<T, T(-1), T(2)>{})
return U(-1);
else if constexpr (d == static_pi_fraction_T<T, T(-1), T(3)>{})
return U(-sqrt3_over_2);
else if constexpr (d == static_pi_fraction_T<T, T(-1), T(4)>{})
return U(-inv_sqrt2);
else if constexpr (d == static_pi_fraction_T<T, T(-1), T(6)>{})
return U(-0.5);
else if constexpr (d == static_pi_fraction_T<T, T(2), T(1)>{})
return U(0);
else if constexpr (d == static_pi_fraction_T<T, T(2), T(3)>{})
return U(sqrt3_over_2);
else if constexpr (d == static_pi_fraction_T<T, T(-2), T(1)>{})
return U(-0);
else if constexpr (d == static_pi_fraction_T<T, T(-2), T(3)>{})
return U(-sqrt3_over_2);
else if constexpr (d == static_pi_fraction_T<T, T(3), T(2)>{})
return U(-1);
else if constexpr (d == static_pi_fraction_T<T, T(3), T(4)>{})
return U(inv_sqrt2);
else if constexpr (d == static_pi_fraction_T<T, T(-3), T(2)>{})
return U(1);
else if constexpr (d == static_pi_fraction_T<T, T(-3), T(4)>{})
return U(-inv_sqrt2);
constexpr radian_T<U> r = d;
return sin(r);
}
template <arithmetic U, arithmetic T>
U cos(pi_fraction_T<T> d)
{
if constexpr (d.numerator == T(0))
return U(1);
else if constexpr (d.numerator == T(1) || d.numerator == T(-1))
{
if constexpr (d.denominator == T(1))
return U(-1);
else if constexpr (d.denominator == T(2))
return U(0);
else if constexpr (d.denominator == T(3))
return U(0.5);
else if constexpr (d.denominator == T(4))
return U(inv_sqrt2);
else if constexpr (d.denominator == T(6))
return U(sqrt3_over_2);
}
else if constexpr (d.numerator == T(2) || d.numerator == T(-2))
{
if constexpr (d.denominator == T(1))
return U(1);
else if constexpr (d.denominator == T(3))
return U(-0.5);
}
else if constexpr (d.numerator == T(3) || d.numerator == T(-3))
{
if constexpr (d.denominator == T(2))
return U(0);
else if constexpr (d.denominator == T(4))
return U(-inv_sqrt2);
}
constexpr radian_T<U> r = d;
return cos(r);
}
template <arithmetic U, arithmetic T>
U tan(pi_fraction_T<T> d)
{
if constexpr (d.numerator == T(0))
return U(0);
else if constexpr (d.numerator == T(1))
{
if constexpr (d.denominator == T(1))
return U(0);
else if constexpr (d.denominator == T(2))
return -std::numeric_limits<U>::infinity();
else if constexpr (d.denominator == T(3))
return U(sqrt3);
else if constexpr (d.denominator == T(4))
return U(1);
else if constexpr (d.denominator == T(6))
return U(inv_sqrt3);
}
else if constexpr (d.numerator == T(-1))
{
if constexpr (d.denominator == T(1))
return U(-0);
else if constexpr (d.denominator == T(2))
return std::numeric_limits<U>::infinity();
else if constexpr (d.denominator == T(3))
return U(-sqrt3);
else if constexpr (d.denominator == T(4))
return U(-1);
else if constexpr (d.denominator == T(6))
return U(-inv_sqrt3);
}
else if constexpr (d.numerator == T(2))
{
if constexpr (d.denominator == T(1))
return U(0);
else if constexpr (d.denominator == T(3))
return U(-sqrt3);
}
else if constexpr (d.numerator == T(-2))
{
if constexpr (d.denominator == T(1))
return U(0);
else if constexpr (d.denominator == T(3))
return U(sqrt3);
}
else if constexpr (d.numerator == T(3))
{
if constexpr (d.denominator == T(2))
return -std::numeric_limits<U>::infinity();
else if constexpr (d.denominator == T(4))
return U(-1);
}
else if constexpr (d.numerator == T(-3))
{
if constexpr (d.denominator == T(2))
return std::numeric_limits<U>::infinity();
else if constexpr (d.denominator == T(4))
return U(1);
}
constexpr radian_T<U> r = d;
return tan(r);
}
template <arithmetic U, arithmetic T>
U asin(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return asin(r);
}
template <arithmetic U, arithmetic T>
U acos(pi_fraction_T<T> d)
{
/// TODO: implement
return acos(radian_T<U>(d));
}
template <arithmetic U, arithmetic T>
U atan(pi_fraction_T<T> d)
{
/// TODO: implement
return atan(radian_T<U>(d));
}
template <arithmetic U, arithmetic T>
U sinh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return sinh(r);
}
template <arithmetic U, arithmetic T>
U cosh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return cosh(r);
}
template <arithmetic U, arithmetic T>
U tanh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return tanh(r);
}
template <arithmetic U, arithmetic T>
U asinh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return asinh(r);
}
template <arithmetic U, arithmetic T>
U acosh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return acosh(r);
}
template <arithmetic U, arithmetic T>
U atanh(pi_fraction_T<T> d)
{
/// TODO: implement
constexpr radian_T<U> r = d;
return atanh(r);
}
#pragma endregion
#pragma region declaration_static_pi_fraction_T
template <std::signed_integral T, T NumeratorT, T DenominatorT>
struct static_pi_fraction_T
{
static_assert(DenominatorT != T(0), "the denominator can not be 0");
template <T NumT, T DenT>
static constexpr std::pair<T, T> resign()
{
if constexpr (DenT < T(0))
return {-NumT, -DenT};
else
return {NumT, DenT};
}
template <T NumT, T DenT>
static constexpr std::pair<T, T> simplify()
{
constexpr auto p = resign<NumT, DenT>();
constexpr auto numt = p.first;
constexpr auto dent = p.second;
if constexpr (numt % dent == T(0))
return {numt / dent, T(1)};
else if constexpr (dent % numt == T(0))
{
if constexpr (numt > T(0))
return {T(1), dent / numt};
else
return {T(-1), -dent / numt};
}
else
return {numt, dent};
}
template <T NumT, T DenT>
static constexpr std::pair<T, T> wrap()
{
constexpr auto p = simplify<NumT, DenT>();
constexpr auto numt = p.first;
constexpr auto dent = p.second;
if constexpr (dent == T(1))
return {numt % T(2), dent};
else if constexpr (dent == T(2))
return {numt % T(4), dent};
else
return {numt, dent};
}
static constexpr T numerator{wrap<NumeratorT, DenominatorT>().first};
static constexpr T denominator{wrap<NumeratorT, DenominatorT>().second};
template <arithmetic U>
constexpr operator radian_T<U>() const
{
if constexpr (numerator == T(0))
return radian_T<U>(U(0));
else
return radian_T<U>((U(numerator) * U(pi)) / U(denominator));
}
template <arithmetic U>
constexpr operator degree_T<U>() const
{
if constexpr (numerator == T(0))
return degree_T<U>(U(0));
else
return degree_T<U>(
radian_T<U>((U(numerator) * U(pi)) / U(denominator)));
}
constexpr operator pi_fraction_T<T>() const
{
return {numerator, denominator};
}
constexpr static_pi_fraction_T<T, -numerator, denominator> operator-() const
{
return {};
}
using underlying_type = T;
};
template <std::signed_integral T,
T NumeratorTL,
T DenominatorTL,
T NumeratorTR,
T DenominatorTR>
constexpr bool operator==(
const static_pi_fraction_T<T, NumeratorTL, DenominatorTL>& lhs,
const static_pi_fraction_T<T, NumeratorTR, DenominatorTR>& rhs)
{
return lhs.numerator == rhs.numerator &&
lhs.denominator == rhs.denominator;
}
template <std::signed_integral T,
T NumeratorTL,
T DenominatorTL,
T NumeratorTR,
T DenominatorTR>
constexpr bool operator!=(
const static_pi_fraction_T<T, NumeratorTL, DenominatorTL>& lhs,
const static_pi_fraction_T<T, NumeratorTR, DenominatorTR>& rhs)
{
return !(lhs == rhs);
}
template <arithmetic U, std::signed_integral T, T NumeratorT, T DenominatorT>
U sin(static_pi_fraction_T<T, NumeratorT, DenominatorT> d)
{
if constexpr (d.numerator == T(0))
return U(0);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(1)>{})
return U(0);
else if constexpr (d == static_pi_fraction_T<T, T(1), T(2)>{})
return U(1);