num-quaternion 1.0.8

Quaternion numbers implementation for Rust
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
use {
    crate::UnitQuaternion,
    core::ops::{Add, Mul, Neg},
    num_traits::{ConstOne, ConstZero, Inv, Num, One, Zero},
};

#[cfg(any(feature = "std", feature = "libm"))]
use {
    core::num::FpCategory,
    num_traits::{Float, FloatConst},
};

/// Quaternion type.
///
/// We follow the naming conventions from
/// [Wikipedia](https://en.wikipedia.org/wiki/Quaternion) for quaternions.
/// You can generate quaternions using the [`new`](Quaternion::new) function:
///
/// ```
/// // 1 + 2i + 3j + 4k
/// # use num_quaternion::Quaternion;
/// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
/// ```
///
/// Alternatively, you can construct quaternions directly with the member data
/// fields:
///
/// ```
/// # use num_quaternion::Q32;
/// let q = Q32 { w: 1.0, x: 2.0, y: 3.0, z: 4.0 };
/// ```
///
/// This is exactly equivalent to the first method. The latter example uses the
/// shorthand `Q32` for `Quaternion<f32>`. For your convenience, there are also
/// the member constants [`ONE`](Quaternion::ONE), [`I`](Quaternion::I),
/// [`J`](Quaternion::J), and [`K`](Quaternion::K) for the mathematical
/// values $1$, $i$, $j$, and $k$, respectively.
///
/// `Quaternion`s support the usual arithmetic operations of addition,
/// subtraction, multiplication, and division. You can compute the
/// norm with [`norm`](Quaternion::norm) or [`fast_norm`](Quaternion::fast_norm)
/// and its square with [`norm_sqr`](Quaternion::norm_sqr). Quaternion
/// conjugation is done by the member function [`conj`](Quaternion::conj).
/// You can normalize a quaternion by calling
/// [`normalize`](Quaternion::normalize), which returns a [`UnitQuaternion`].
///
/// Furthermore, the following functions are supported:
///
/// - [`dot`](Quaternion::dot): Computes the dot product of two quaternions.
/// - [`exp`](Quaternion::exp): Computes the exponential of a quaternion.
/// - [`expf`](Quaternion::expf): Raises a real value to a quaternion power.
/// - [`inv`](Quaternion::inv): Computes the multiplicative inverse.
/// - [`ln`](Quaternion::ln): Computes the natural logarithm of a quaternion.
/// - [`powf`](Quaternion::powf): Raises a quaternion to a real power.
/// - [`powi`](Quaternion::powi): Raises a quaternion to a signed integer power.
/// - [`powu`](Quaternion::powu): Raises a quaternion to an unsigned integer power.
///
/// To work with rotations, please use [`UnitQuaternion`]s.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// # use num_quaternion::Quaternion;
/// let q1 = Quaternion::new(1.0f32, 0.0, 0.0, 0.0);
/// let q2 = Quaternion::new(0.0, 1.0, 0.0, 0.0);
/// let q3 = q1 + q2;
/// assert_eq!(q3, Quaternion::new(1.0, 1.0, 0.0, 0.0));
/// ```
///
/// # Fields
///
/// - `w`: Real part of the quaternion.
/// - `x`: The coefficient of $i$.
/// - `y`: The coefficient of $j$.
/// - `z`: The coefficient of $k$.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Quaternion<T> {
    /// Real part of the quaternion.
    pub w: T,
    /// The coefficient of $i$.
    pub x: T,
    /// The coefficient of $j$.
    pub y: T,
    /// The coefficient of $k$.
    pub z: T,
}

/// Alias for a [`Quaternion<f32>`].
pub type Q32 = Quaternion<f32>;
/// Alias for a [`Quaternion<f64>`].
pub type Q64 = Quaternion<f64>;

impl<T> Quaternion<T> {
    /// Create a new quaternion $a + bi + cj + dk$.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// ```
    #[inline]
    pub const fn new(w: T, x: T, y: T, z: T) -> Self {
        Self { w, x, y, z }
    }
}

impl<T> Quaternion<T>
where
    T: ConstZero,
{
    /// A constant zero `Quaternion`.
    ///
    /// This is the additive identity element of the quaternion space.
    /// See also [`Quaternion::zero`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::ZERO;
    /// assert_eq!(q, Quaternion::new(0.0f32, 0.0, 0.0, 0.0));
    /// ```
    pub const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO, T::ZERO);
}

impl<T> ConstZero for Quaternion<T>
where
    T: ConstZero,
{
    const ZERO: Self = Self::ZERO;
}

impl<T> Zero for Quaternion<T>
where
    T: Zero,
{
    #[inline]
    fn zero() -> Self {
        Self::new(Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero())
    }

    #[inline]
    fn is_zero(&self) -> bool {
        self.w.is_zero()
            && self.x.is_zero()
            && self.y.is_zero()
            && self.z.is_zero()
    }

    #[inline]
    fn set_zero(&mut self) {
        self.w.set_zero();
        self.x.set_zero();
        self.y.set_zero();
        self.z.set_zero();
    }
}

impl<T> Quaternion<T>
where
    T: ConstZero + ConstOne,
{
    /// A constant `Quaternion` of value $1$.
    ///
    /// See also [`Quaternion::one`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::ONE;
    /// assert_eq!(q, Quaternion::new(1.0f32, 0.0, 0.0, 0.0));
    /// ```
    pub const ONE: Self = Self::new(T::ONE, T::ZERO, T::ZERO, T::ZERO);

    /// A constant `Quaternion` of value $i$.
    ///
    /// See also [`Quaternion::i`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::I;
    /// assert_eq!(q, Quaternion::new(0.0f32, 1.0, 0.0, 0.0));
    /// ```
    pub const I: Self = Self::new(T::ZERO, T::ONE, T::ZERO, T::ZERO);

    /// A constant `Quaternion` of value $j$.
    ///
    /// See also [`Quaternion::j`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::J;
    /// assert_eq!(q, Quaternion::new(0.0f32, 0.0, 1.0, 0.0));
    /// ```
    pub const J: Self = Self::new(T::ZERO, T::ZERO, T::ONE, T::ZERO);

    /// A constant `Quaternion` of value $k$.
    ///
    /// See also [`Quaternion::k`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::K;
    /// assert_eq!(q, Quaternion::new(0.0f32, 0.0, 0.0, 1.0));
    /// ```
    pub const K: Self = Self::new(T::ZERO, T::ZERO, T::ZERO, T::ONE);
}

impl<T> ConstOne for Quaternion<T>
where
    T: ConstZero + ConstOne + Num + Clone,
{
    const ONE: Self = Self::ONE;
}

impl<T> One for Quaternion<T>
where
    T: Num + Clone,
{
    #[inline]
    fn one() -> Self {
        Self::new(One::one(), Zero::zero(), Zero::zero(), Zero::zero())
    }

    #[inline]
    fn is_one(&self) -> bool {
        self.w.is_one()
            && self.x.is_zero()
            && self.y.is_zero()
            && self.z.is_zero()
    }

    #[inline]
    fn set_one(&mut self) {
        self.w.set_one();
        self.x.set_zero();
        self.y.set_zero();
        self.z.set_zero();
    }
}

impl<T> Quaternion<T>
where
    T: Zero + One,
{
    /// Returns the real unit $1$.
    ///
    /// See also [`Quaternion::ONE`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::one();
    /// assert_eq!(q, Quaternion::new(1.0f32, 0.0, 0.0, 0.0));
    /// ```
    #[inline]
    pub fn one() -> Self {
        Self::new(T::one(), T::zero(), T::zero(), T::zero())
    }

    /// Returns the imaginary unit $i$.
    ///
    /// See also [`Quaternion::I`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::i();
    /// assert_eq!(q, Quaternion::new(0.0f32, 1.0, 0.0, 0.0));
    /// ```
    #[inline]
    pub fn i() -> Self {
        Self::new(T::zero(), T::one(), T::zero(), T::zero())
    }

    /// Returns the imaginary unit $j$.
    ///
    /// See also [`Quaternion::J`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::j();
    /// assert_eq!(q, Quaternion::new(0.0f32, 0.0, 1.0, 0.0));
    /// ```
    #[inline]
    pub fn j() -> Self {
        Self::new(T::zero(), T::zero(), T::one(), T::zero())
    }

    /// Returns the imaginary unit $k$.
    ///
    /// See also [`Quaternion::K`].
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::k();
    /// assert_eq!(q, Quaternion::new(0.0f32, 0.0, 0.0, 1.0));
    /// ```
    #[inline]
    pub fn k() -> Self {
        Self::new(T::zero(), T::zero(), T::zero(), T::one())
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float,
{
    /// Returns a quaternion filled with `NaN` values.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Q32;
    /// let q = Q32::nan();
    /// assert!(q.w.is_nan());
    /// assert!(q.x.is_nan());
    /// assert!(q.y.is_nan());
    /// assert!(q.z.is_nan());
    /// ```
    #[inline]
    pub fn nan() -> Self {
        let nan = T::nan();
        Self::new(nan, nan, nan, nan)
    }
}

impl<T> Quaternion<T>
where
    T: Clone + Mul<T, Output = T> + Add<T, Output = T>,
{
    /// Returns the square of the norm.
    ///
    /// The result is $w^2 + x^2 + y^2 + z^2$ with some rounding errors.
    /// The rounding error is at most 2
    /// [ulps](https://en.wikipedia.org/wiki/Unit_in_the_last_place).
    ///
    /// This is guaranteed to be more efficient than [`norm`](Quaternion::norm()).
    /// Furthermore, `T` only needs to support addition and multiplication
    /// and therefore, this function works for more types than
    /// [`norm`](Quaternion::norm()).
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert_eq!(q.norm_sqr(), 30.0);
    /// ```
    #[inline]
    pub fn norm_sqr(&self) -> T {
        (self.w.clone() * self.w.clone() + self.y.clone() * self.y.clone())
            + (self.x.clone() * self.x.clone()
                + self.z.clone() * self.z.clone())
    }
}

impl<T> Quaternion<T>
where
    T: Clone + Neg<Output = T>,
{
    /// Returns the conjugate quaternion, i. e. the imaginary part is negated.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert_eq!(q.conj(), Quaternion::new(1.0, -2.0, -3.0, -4.0));
    /// ```
    #[inline]
    pub fn conj(&self) -> Self {
        Self::new(
            self.w.clone(),
            -self.x.clone(),
            -self.y.clone(),
            -self.z.clone(),
        )
    }
}

impl<T> Quaternion<T>
where
    for<'a> &'a Self: Inv<Output = Quaternion<T>>,
{
    /// Returns the multiplicative inverse `1/self`.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert_eq!(q.inv(), Quaternion::new(
    ///     1.0 / 30.0, -1.0 / 15.0, -0.1, -2.0 / 15.0));
    #[inline]
    pub fn inv(&self) -> Self {
        Inv::inv(self)
    }
}

impl<T> Inv for &Quaternion<T>
where
    T: Clone + Neg<Output = T> + Num,
{
    type Output = Quaternion<T>;

    #[inline]
    fn inv(self) -> Self::Output {
        let norm_sqr = self.norm_sqr();
        Quaternion::new(
            self.w.clone() / norm_sqr.clone(),
            -self.x.clone() / norm_sqr.clone(),
            -self.y.clone() / norm_sqr.clone(),
            -self.z.clone() / norm_sqr,
        )
    }
}

impl<T> Inv for Quaternion<T>
where
    for<'a> &'a Self: Inv<Output = Quaternion<T>>,
{
    type Output = Quaternion<T>;

    #[inline]
    fn inv(self) -> Self::Output {
        Inv::inv(&self)
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float,
{
    /// Calculates |self|.
    ///
    /// The result is $\sqrt{w^2+x^2+y^2+z^2}$ with some possible rounding
    /// errors. The total relative rounding error is at most two
    /// [ulps](https://en.wikipedia.org/wiki/Unit_in_the_last_place).
    ///
    /// If any of the components of the input quaternion is `NaN`, then `NaN`
    /// is returned. Otherwise, if any of the components is infinite, then
    /// a positive infinite value is returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert_eq!(q.norm(), 30.0f32.sqrt());
    /// ```
    #[inline]
    pub fn norm(self) -> T {
        let one = T::one();
        let two = one + one;
        let s = T::min_positive_value();
        let norm_sqr = self.norm_sqr();
        if norm_sqr < T::infinity() {
            if norm_sqr >= s * two {
                norm_sqr.sqrt()
            } else if self.is_zero() {
                // Likely, the whole vector is zero. If so, we can return
                // zero directly and avoid expensive floating point math.
                T::zero()
            } else {
                // Otherwise, scale up, such that the norm will be in the
                // normal floating point range, then scale down the result.
                (self / s).fast_norm() * s
            }
        } else {
            // There are three possible cases:
            //   1. one of w, x, y, z is NaN,
            //   2. neither is `NaN`, but at least one of them is infinite, or
            //   3. all of them are finite.
            // In the first case, multiplying by s or dividing by it does not
            // change the that the result is `NaN`. The same applies in the
            // second case: the result remains infinite. In the third case,
            // multiplying by s makes sure that the square norm is a normal
            // floating point number. Dividing by it will rescale the result
            // to the correct magnitude.
            (self * s).fast_norm() / s
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float,
{
    /// Calculates |self| without branching.
    ///
    /// This function returns the same result as [`norm`](Self::norm), if
    /// |self|² is a normal floating point number (i. e. there is no overflow
    /// nor underflow), or if `self` is zero. In these cases the maximum
    /// relative error of the result is guaranteed to be less than two ulps.
    /// In all other cases, there's no guarantee on the precision of the
    /// result:
    ///
    /// * If |self|² overflows, then $\infty$ is returned.
    /// * If |self|² underflows to zero, then zero will be returned.
    /// * If |self|² is a subnormal number (very small floating point value
    ///   with reduced relative precision), then the result is the square
    ///   root of that.
    ///
    /// In other words, this function can be imprecise for very large and very
    /// small floating point numbers, but it is generally faster than
    /// [`norm`](Self::norm), because it does not do any branching. So if you
    /// are interested in maximum speed of your code, feel free to use this
    /// function. If you need to be precise results for the whole range of the
    /// floating point type `T`, stay with [`norm`](Self::norm).
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert_eq!(q.fast_norm(), q.norm());
    /// ```
    #[inline]
    pub fn fast_norm(self) -> T {
        self.norm_sqr().sqrt()
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float,
{
    /// Normalizes the quaternion to length $1$.
    ///
    /// The sign of the real part will be the same as the sign of the input.
    /// If the input quaternion
    ///
    /// * is zero, or
    /// * has infinite length, or
    /// * has a `NaN` value,
    ///
    /// then `None` will be returned.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// # fn test() -> Option<()> {
    /// let q = Quaternion::new(1.0f32, 2.0, 2.0, 4.0);
    /// assert_eq!(q.normalize()?.into_quaternion(),
    ///         Quaternion::new(0.2f32, 0.4, 0.4, 0.8));
    /// # Some(())
    /// # }
    /// # assert!(test().is_some());
    /// ```
    #[inline]
    pub fn normalize(self) -> Option<UnitQuaternion<T>> {
        UnitQuaternion::normalize(self)
    }
}

impl<T> From<T> for Quaternion<T>
where
    T: Zero,
{
    #[inline]
    fn from(a: T) -> Self {
        Self::new(a, T::zero(), T::zero(), T::zero())
    }
}

impl<T> From<&T> for Quaternion<T>
where
    T: Clone + Zero,
{
    #[inline]
    fn from(a: &T) -> Self {
        From::from(a.clone())
    }
}

impl<T> From<UnitQuaternion<T>> for Quaternion<T> {
    #[inline]
    fn from(q: UnitQuaternion<T>) -> Self {
        q.into_inner()
    }
}

impl<'a, T> From<&'a UnitQuaternion<T>> for &'a Quaternion<T> {
    #[inline]
    fn from(q: &'a UnitQuaternion<T>) -> Self {
        q.as_quaternion()
    }
}

impl<T> Quaternion<T>
where
    T: Add<T, Output = T> + Mul<T, Output = T>,
{
    /// Computes the dot product of two quaternions interpreted as
    /// 4D real vectors.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q1 = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let q2 = Quaternion::new(0.0f32, 0.0, 1.0, 1.0);
    /// let d = q1.dot(q2);
    /// assert_eq!(d, 7.0);
    /// ```
    #[inline]
    pub fn dot(self, other: Self) -> T {
        self.w * other.w
            + self.y * other.y
            + (self.x * other.x + self.z * other.z)
    }
}

impl<T> Quaternion<T>
where
    T: Num + Clone,
{
    /// Raises `self` to an unsigned integer power `n`, i. e. $q^n$.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let q_sqr = q.powu(2);
    /// assert_eq!(q_sqr, q * q);
    /// ```
    pub fn powu(&self, mut n: u32) -> Self {
        if n == 0 {
            Self::one()
        } else {
            let mut base = self.clone();
            while n & 1 == 0 {
                n /= 2;
                base = base.clone() * base;
            }

            if n == 1 {
                return base;
            }

            let mut acc = base.clone();
            while n > 1 {
                n /= 2;
                base = base.clone() * base;
                if n & 1 == 1 {
                    acc *= base.clone();
                }
            }
            acc
        }
    }
}

impl<T> Quaternion<T>
where
    T: Clone + Num + Neg<Output = T>,
{
    /// Raises `self` to a signed integer power `n`, i. e. $q^n$
    ///
    /// For $n=0$ the result is exactly $1$.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let q_sqr = q.powi(2);
    /// assert_eq!(q_sqr, q * q);
    /// ```
    #[inline]
    pub fn powi(&self, n: i32) -> Self {
        if n >= 0 {
            self.powu(n as u32)
        } else {
            self.inv().powu(n.wrapping_neg() as u32)
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float + FloatConst,
{
    /// Given a quaternion $q$, returns $e^q$, where $e$ is the base of the
    /// natural logarithm.
    ///
    /// This method computes the exponential of a quaternion, handling various
    /// edge cases to ensure numerical stability and correctness:
    ///
    /// 1. **Negative Real Part**: If the real part is sufficiently negative,
    ///    such that $e^{\Re q}$ is approximately zero, the method returns
    ///    zero. This is done even if the imaginary part contains infinite or
    ///    NaN values.
    ///
    /// 2. **NaN Input**: If any component of the input quaternion is `NaN`,
    ///    the method returns a quaternion filled with `NaN` values.
    ///
    /// 3. **Large Imaginary Norm**: If the norm of the imaginary part is too
    ///    large, the method may return a `NaN` quaternion or a quaternion with
    ///    the correct magnitude but inaccurate direction.
    ///
    /// 4. **Infinite Result**: If $e^{\Re q}$ results in `+∞`, the method
    ///    computes the direction and returns an infinite quaternion in that
    ///    direction, ensuring that `∞ * 0` values are mapped to zero instead
    ///    of `NaN`.
    ///
    /// 5. **Finite Norm**: For finite norms, the method ensures a very small
    ///    relative error in all components, depending on the accuracy of the
    ///    underlying floating point function implementations.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let exp_q = q.exp();
    /// ```
    pub fn exp(self) -> Self {
        let one = T::one();
        let two = one + one;
        let four = two + two;
        let half = one / two;
        let quarter = one / four;
        let inf = T::infinity();

        // Compute the exponential of the real part, which gives the norm of
        // the result
        let result_norm = self.w.exp();

        match result_norm.partial_cmp(&inf) {
            Some(core::cmp::Ordering::Less) => {
                if result_norm.is_zero() {
                    return Self::zero();
                }
                // Case: 0 < result_norm < ∞

                // Compute the squared norm of the imaginary part
                let sqr_angle =
                    self.x * self.x + self.y * self.y + self.z * self.z;

                if sqr_angle <= T::epsilon() {
                    // Use Taylor series approximation for small angles to
                    // maintain numerical stability. By Taylor expansion of
                    // `cos(angle)` we get
                    //     cos(angle) >= 1 - angle² / 2
                    // and thus |cos(angle) - 1| is less than half a floating
                    // point epsilon. Similarly,
                    //     sinc(angle) >= 1 - angle² / 6
                    // and thus |sinc(angle) - 1| is less than a sixth of a
                    // floating point epsilon.
                    let w = result_norm;
                    let x = result_norm * self.x;
                    let y = result_norm * self.y;
                    let z = result_norm * self.z;
                    Self::new(w, x, y, z)
                } else {
                    // Standard computation for larger angles
                    let angle = sqr_angle.sqrt();
                    let cos_angle = angle.cos();
                    let sinc_angle = angle.sin() / angle;
                    let w = result_norm * cos_angle;
                    let x = result_norm * self.x * sinc_angle;
                    let y = result_norm * self.y * sinc_angle;
                    let z = result_norm * self.z * sinc_angle;
                    Self::new(w, x, y, z)
                }
            }
            Some(_) => {
                // Case: result_norm == ∞
                let map = |a: T| {
                    // Map zero to zero with same sign and everything else to
                    // infinity with same sign as the input.
                    if a.is_zero() {
                        a
                    } else {
                        inf.copysign(a)
                    }
                };
                let sqr_angle =
                    self.x * self.x + self.y * self.y + self.z * self.z;
                if sqr_angle < T::PI() * T::PI() * quarter {
                    // Angle less than 90 degrees
                    Self::new(inf, map(self.x), map(self.y), map(self.z))
                } else if sqr_angle.is_finite() {
                    // Angle 90 degrees or more -> careful sign handling
                    let angle = sqr_angle.sqrt();
                    let angle_revolutions_fract =
                        (angle * T::FRAC_1_PI() * half).fract();
                    let cos_angle_signum =
                        (angle_revolutions_fract - half).abs() - quarter;
                    let sin_angle_signum =
                        one.copysign(half - angle_revolutions_fract);
                    Self::new(
                        inf.copysign(cos_angle_signum),
                        map(self.x) * sin_angle_signum,
                        map(self.y) * sin_angle_signum,
                        map(self.z) * sin_angle_signum,
                    )
                } else {
                    // Angle is super large or NaN
                    debug_assert!(
                        sqr_angle.is_infinite() || sqr_angle.is_nan()
                    );
                    Self::nan()
                }
            }
            None => {
                // Case: result_norm is NaN
                debug_assert!(result_norm.is_nan());
                Self::nan()
            }
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float + FloatConst,
{
    /// Raises a real value (`base`) to a quaternion (`self`) power.
    ///
    /// Given a quaternion $q$ and a real value $t$, this function computes
    /// $t^q := e^{q \ln t}$. The function handles special cases as follows:
    ///
    /// - If $t = \pm 0$ and $\Re q > 0$, or $t = +\infty$ and $\Re q < 0$,
    ///   a zero quaternion is returned.
    /// - If $t < 0$ or $t$ is `NaN`, a `NaN` quaternion (filled with `NaN`
    ///   values) is returned.
    /// - If $t$ is $+0$, $-0$, or $+\infty$ and $\Re q = 0$, a `NaN`
    ///   quaternion is returned.
    /// - If $t = +\infty$ and $q$ is a positive real number, $+\infty$ is
    ///   returned. For other values of $q$ with $\Re q > 0$, a `NaN`
    ///   quaternion is returned.
    /// - If $t = \pm 0$ and $q$ is a negative real number, $+\infty$ is
    ///   returned. For other values of $q$ with $\Re q < 0$, a `NaN`
    ///   quaternion is returned.
    ///
    /// For finite positive $t$, the following conventions for boundary values
    /// of $q$ are applied:
    ///
    /// - If any component of $q$ is `NaN` or any imaginary component of $q$ is
    ///   infinite, a `NaN` quaternion is returned.
    /// - Otherwise, if $\Re q = -\infty$ and $t > 1$, a zero quaternion is
    ///   returned.
    /// - Otherwise, if $\Re q = +\infty$ and $0 < t < 1$, a zero quaternion is
    ///   returned.
    /// - Otherwise, if $\Re q$ is infinite and $t = 1$, a `NaN` quaternion is
    ///   returned.
    /// - Otherwise, if $\Re q = +\infty$ and $t > 1$, an infinite quaternion
    ///   without `NaN` values is returned.
    /// - Otherwise, if $\Re q = -\infty$ and $0 < t < 1$, an infinite
    ///   quaternion without `NaN` values is returned.
    ///
    /// If the true result's norm is neither greater than the largest
    /// representable floating point value nor less than the smallest
    /// representable floating point value, and the direction of the output
    /// quaternion cannot be accurately determined, a `NaN` quaternion may or
    /// may not be returned to indicate inaccuracy. This can occur when
    /// $\|\Im(q) \ln t\|$ is on the order of $1/\varepsilon$, where
    /// $\varepsilon$ is the machine precision of the floating point type used.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let exp_q = q.expf(2.0);
    /// ```
    #[inline]
    pub fn expf(self, base: T) -> Self {
        if (base.is_infinite()
            && self.w > T::zero()
            && self.x.is_zero()
            && self.y.is_zero()
            && self.z.is_zero())
            || (base.is_zero()
                && self.w < T::zero()
                && self.x.is_zero()
                && self.y.is_zero()
                && self.z.is_zero())
        {
            T::infinity().into()
        } else {
            (self * base.ln()).exp()
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float + FloatConst,
{
    /// Raises a quaternion (`self`) to a real value (`exponent`) power.
    ///
    /// Given a quaternion $q$ and a real value $t$, this function computes
    /// $q^t := e^{t \ln q}$. The function handles special cases as follows:
    ///
    /// - If $q = 0$ and $t > 0$, a zero quaternion is returned.
    /// - If $|q| = \infty$ and $t < 0$, a zero quaternion is returned.
    /// - If $|q| = \infty$ and $t > 0$ is not too large to prevent
    ///   numerical accuracy for the direction of the quaternion, then an
    ///   infinite quaternion without `NaN` components is returned. For larger
    ///   but finite values of $t$ this may still be hold, or alternatively a
    ///   quaternion filled with `NaN` values is returned.
    /// - If $q = +\infty$ and $t = +\infty$, then positive infinity is
    ///   returned.
    /// - If $|q| = \infty$, but $q \neq +\infty$ and $t = +\infty$, then `NaN`
    ///   is returned.
    /// - If $q = 0$ and $t < 0$, then positive infinity is returned.
    /// - If $q$ contains a `NaN` component, or if $t$ is `NaN`, a `NaN`
    ///   quaternion is returned.
    /// - If $|q| = \infty$ and $t = 0$, a `NaN` quaternion is returned.
    /// - If $q = 0$ and $t = 0$, a `NaN` quaternion is returned.
    ///
    /// For non-zero finite $q$, the following conventions for boundary values
    /// of $t$ are applied:
    ///
    /// - If $t = +\infty$ and $q, |q| \ge 1$ is not real or $q = 1$ or
    ///   $q \le -1$, a `NaN` quaternion is returned.
    /// - If $t = +\infty$ and $q > 1$, positive infinity is returned.
    /// - If $t = +\infty$ and $|q| < 1$, zero is returned.
    /// - If $t = -\infty$ and $|q| > 1$, zero is returned.
    /// - If $t = -\infty$ and $0 \le q < 1$, positive infinity is returned.
    /// - If $t = -\infty$ and $q, |q| \le 1$ is not real or $q = 1$ or
    ///   $-1 \le q < 0$, a `NaN` quaternion is returned.
    ///
    /// If the true result's norm is neither greater than the largest
    /// representable floating point value nor less than the smallest
    /// representable floating point value, and the direction of the output
    /// quaternion cannot be accurately determined, a `NaN` quaternion may or
    /// may not be returned to indicate inaccuracy. This can occur when
    /// $\|t \Im(\ln q)\|$ is on the order of $1/\varepsilon$, where
    /// $\varepsilon$ is the machine precision of the floating point type used.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let exp_q = q.powf(2.0);
    /// ```
    #[inline]
    pub fn powf(self, exponent: T) -> Self {
        if exponent.is_finite() {
            // -∞ < t < +∞ ==> apply the general formula.
            (self.ln() * exponent).exp()
        } else if exponent > T::zero() {
            // t = +∞
            if self.x.is_zero() && self.y.is_zero() && self.z.is_zero() {
                // q is real --> handle special cases
                match self.w.partial_cmp(&T::one()) {
                    Some(core::cmp::Ordering::Greater) => T::infinity().into(),
                    Some(core::cmp::Ordering::Less) => T::zero().into(),
                    _ => Self::nan(),
                }
            } else if self.norm_sqr() < T::one() {
                // |q| < 1
                Self::zero()
            } else {
                // Otherwise, return NaN
                Self::nan()
            }
        } else if exponent < T::zero() {
            // t = -∞
            if self.x.is_zero() && self.y.is_zero() && self.z.is_zero() {
                // q is real --> handle special cases
                match self.w.partial_cmp(&T::one()) {
                    Some(core::cmp::Ordering::Greater) => T::zero().into(),
                    Some(core::cmp::Ordering::Less) => T::infinity().into(),
                    _ => Self::nan(),
                }
            } else if self.norm_sqr() > T::one() {
                // |q| > 1
                Self::zero()
            } else {
                // Otherwise, return NaN
                Self::nan()
            }
        } else {
            // t is NaN
            debug_assert!(exponent.is_nan());
            Self::nan()
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float,
{
    /// Returns whether all components of the quaternion are finite.
    ///
    /// If a `Quaternion` has an infinite or `NaN` entry, the function returns
    /// `false`, otherwise `true`.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert!(q.is_finite());
    /// ```
    pub fn is_finite(&self) -> bool {
        self.w.is_finite()
            && self.x.is_finite()
            && self.y.is_finite()
            && self.z.is_finite()
    }

    /// Returns whether any component of the quaternion is `NaN`.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// assert!(!q.has_nan());
    /// ```
    pub fn has_nan(&self) -> bool {
        self.w.is_nan() || self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
    }

    /// Returns whether all components of a quaternion are `NaN`.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Q64;
    /// let q = Q64::nan();
    /// assert!(q.is_all_nan());
    /// ```
    pub fn is_all_nan(&self) -> bool {
        self.w.is_nan() && self.x.is_nan() && self.y.is_nan() && self.z.is_nan()
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float + FloatConst,
{
    /// Computes the natural logarithm of a quaternion.
    ///
    /// The function implements the following guarantees for extreme input
    /// values:
    ///
    /// - The function is continuous onto the branch cut taking into account
    ///   the sign of the coefficient of $i$.
    /// - For all quaternions $q$ it holds `q.conj().ln() == q.ln().conj()`.
    /// - The signs of the coefficients of the imaginary parts of the outputs
    ///   are equal to the signs of the respective coefficients of the inputs.
    ///   This also holds for signs of zeros, but not for `NaNs`.
    /// - If $q = 0$, the result is $-\infty$. (The coefficients of $i$, $j$,
    ///   and $k$ are zero with the original signs copied.)
    /// - If the input has a `NaN` value, then the result is `NaN` in all
    ///   components.
    /// - Otherwise, if $q = w + xi + yj + zk$ where at least one of
    ///   $w, x, y, z$ is infinite, then the real part of the result is
    ///   $+\infty$ and the imaginary part is the imaginary part
    ///   of the logarithm of $f(w) + f(x)i + f(y)j + f(z)k$ where
    ///     - $f(+\infty) := 1$,
    ///     - $f(-\infty) :=-1$, and
    ///     - $f(s) := 0$ for finite values of $s$.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let ln_q = q.ln();
    /// ```
    pub fn ln(self) -> Self {
        // The square norm of the imaginary part.
        let sqr_norm_im = self.x * self.x + self.y * self.y + self.z * self.z;
        // The square norm of `self`.
        let norm_sqr = self.w * self.w + sqr_norm_im;

        match norm_sqr.classify() {
            FpCategory::Normal => {
                // The normal case: First compute the real part of the result.
                let w =
                    norm_sqr.ln() * T::from(0.5).expect("Conversion failed");

                if sqr_norm_im <= self.w * self.w * T::epsilon() {
                    // We're close to or on the positive real axis
                    if self.w.is_sign_positive() {
                        // This approximation leaves a relative error of less
                        // than a floating point epsilon for the imaginary part
                        let x = self.x / self.w;
                        let y = self.y / self.w;
                        let z = self.z / self.w;
                        Self::new(w, x, y, z)
                    } else if self.x.is_zero()
                        && self.y.is_zero()
                        && self.z.is_zero()
                    {
                        // We're on the negative real axis.
                        Self::new(w, T::PI().copysign(self.x), self.y, self.z)
                    } else if sqr_norm_im.is_normal() {
                        // We're close the the negative real axis. Compute the
                        // norm of the imaginary part.
                        let norm_im = sqr_norm_im.sqrt();

                        // The angle of `self` to the positive real axis is
                        // pi minus the angle from the negative real axis.
                        // The angle from the negative real axis
                        // can be approximated by `norm_im / self.w.abs()`
                        // which is equal to `-norm_im / self.w`. This the
                        // angle from the positive real axis is
                        // `pi + norm_im / self.w`. We obtain the imaginary
                        // part of the result by multiplying this value by
                        // the imaginary part of the input normalized, or
                        // equivalently, by multiplying the imaginary part
                        // of the input by the following factor:
                        let f = T::PI() / norm_im + self.w.recip();

                        Self::new(w, f * self.x, f * self.y, f * self.z)
                    } else {
                        // The imaginary part is so small, that the norm of the
                        // resulting imaginary part differs from `pi` by way
                        // less than half an ulp. Therefore, it's sufficient to
                        // normalize the imaginary part and multiply it by
                        // `pi`.
                        let f = T::one()
                            / (T::min_positive_value().sqrt() * T::epsilon());
                        // `f` is a power of two (for `f32` and `f64`), so the
                        // following multiplications are exact.
                        let xf = self.x * f;
                        let yf = self.y * f;
                        let zf = self.z * f;
                        let sqr_sum = xf * xf + yf * yf + zf * zf;
                        let im_norm_div_f = sqr_sum.sqrt();
                        let pi_div_f = T::PI() * f;
                        // We could try to reduce the number of divisions by
                        // computing `pi_div_f / im_norm_div_f` and then
                        // multiplying the imaginary part by this value.
                        // However, this reduces numerical accuracy, if the
                        // pi times the norm of the imaginary part is
                        // subnormal. We could also introduce another branch
                        // here, but this would make the code more complex
                        // and extend the worst case latency. Therefore, we
                        // keep the divisions like that.
                        Self::new(
                            w,
                            self.x * pi_div_f / im_norm_div_f,
                            self.y * pi_div_f / im_norm_div_f,
                            self.z * pi_div_f / im_norm_div_f,
                        )
                    }
                } else {
                    // The most natural case: We're far enough from the real
                    // axis and the norm of the input quaternion is large
                    // enough to exclude any numerical instabilities.
                    let norm_im = if sqr_norm_im.is_normal() {
                        // `sqr_norm_im` has maximum precision.
                        sqr_norm_im.sqrt()
                    } else {
                        // Otherwise, using `sqr_norm_im` is imprecise.
                        // We magnify the imaginary part first, so we can
                        // get around this problem.
                        let f = T::one() / T::min_positive_value().sqrt();
                        // `f` is a power of two (for `f32` and `f64`), so the
                        // following multiplications are exact.
                        let xf = self.x * f;
                        let yf = self.y * f;
                        let zf = self.z * f;
                        let sqr_sum = xf * xf + yf * yf + zf * zf;
                        // Since `f` is a power of two, the following line can
                        // be optimized into a multiplication without loss of
                        // precision.
                        sqr_sum.sqrt() / f
                    };
                    let angle = norm_im.atan2(self.w);
                    let x = self.x * angle / norm_im;
                    let y = self.y * angle / norm_im;
                    let z = self.z * angle / norm_im;
                    Self::new(w, x, y, z)
                }
            }
            FpCategory::Zero if self.is_zero() => {
                Self::new(T::neg_infinity(), self.x, self.y, self.z)
            }
            FpCategory::Nan => Self::nan(),
            FpCategory::Infinite => {
                // The square norm overflows.
                if self.is_finite() {
                    // There is no infinity entry in the quaternion. Hence,
                    // We can scale the quaternion down and recurse.
                    let factor = T::one() / T::max_value().sqrt();
                    (self * factor).ln() - factor.ln()
                } else {
                    // There is an infinite value in the input quaternion.
                    // Let's map the infinite entries to `±1` and all other
                    // entries to `±0` maintaining the sign.
                    let f = |r: T| {
                        if r.is_infinite() {
                            r.signum()
                        } else {
                            T::zero().copysign(r)
                        }
                    };
                    let q =
                        Self::new(f(self.w), f(self.x), f(self.y), f(self.z));
                    // TODO: Optimize this. There are only a few possible
                    // angles which could be hard-coded. Recursing here
                    // may be a bit heavy.
                    q.ln() + T::infinity()
                }
            }
            _ => {
                // Square norm is less than smallest positive normal value,
                // but `self` is not zero. Let's scale up the value to obtain
                // the precision by recursing and then fix the factor
                // afterwards.
                let factor = T::one() / T::min_positive_value();
                (self * factor).ln() - factor.ln()
            }
        }
    }
}

#[cfg(any(feature = "std", feature = "libm"))]
impl<T> Quaternion<T>
where
    T: Float + FloatConst,
{
    // Computes the square root of a quaternion.
    ///
    /// Given the input quaternion $c$, this function returns the quaternion
    /// $q$ which satisfies $q^2 = c$ and has a real part with a positive sign.
    ///
    /// For extreme values, the following guarantees are implemented:
    ///
    /// - If any coefficient in $c$ is `NaN`, then the result is `NaN` in all
    ///   components.
    /// - Otherwise, for any input $c$, the expression `c.sqrt().conj()` is
    ///   exactly equivalent to `c.conj().sqrt()`, including the signs of
    ///   zeros and infinities, if any.
    /// - For any input $c$, `c.sqrt().w` always has a positive sign.
    /// - For any input $c$, the signs of the three output imaginary parts are
    ///   the same as the input imaginary parts in their respective order,
    ///   except in the case of a `NaN` input.
    /// - For negative real inputs $c$, the result is $\pm\sqrt{-c} i$, where
    ///   the sign is determined by the sign of the input's coefficient of $i$.
    /// - If there is at least one infinite coefficient in the imaginary part,
    ///   then the result will have the same infinite imaginary coefficients
    ///   and the real part is $+\infty$. All other coefficients of the result
    ///   are $0$ with the sign of the respective input.
    /// - If the real part is $-\infty$ and the imaginary part is finite, then
    ///   the result is $\pm\infty i$ with the sign of the coefficient of $i$
    ///   from the input.
    ///
    /// # Example
    ///
    /// ```
    /// # use num_quaternion::Quaternion;
    /// let q = Quaternion::new(1.0f32, 2.0, 3.0, 4.0);
    /// let sqrt_q = q.sqrt();
    /// ```
    pub fn sqrt(self) -> Self {
        let zero = T::zero();
        let one = T::one();
        let two = one + one;
        let half = one / two;
        let inf = T::infinity();
        let s = one / T::min_positive_value(); // large scale factor
        let norm_sqr = self.norm_sqr();
        match norm_sqr.classify() {
            FpCategory::Normal => {
                // norm of the input
                let norm = norm_sqr.sqrt();

                if self.w.is_sign_positive() {
                    // Compute double the real part of the result directly and
                    // robustly.
                    //
                    // Note: We could also compute the real part directly.
                    // However, this would be inferior for the following
                    //  reasons:
                    //
                    // - To compute the imaginary parts of the result, we
                    //   would need to double the real part anyway, which
                    //   would require an extra arithmetic operation, adding
                    //   to the latency of the computation.
                    // - To avoid this latency, we could also multiply
                    //   `self.x`, `self.y`, and `self.z` by 1/2 and then
                    //   divide by the real part (which takes longer to
                    //   compute). However, this could cost some accuracy
                    //   for subnormal imaginary parts.
                    let wx2 = ((self.w + norm) * two).sqrt();

                    Self::new(
                        wx2 * half,
                        self.x / wx2,
                        self.y / wx2,
                        self.z / wx2,
                    )
                } else {
                    // The first formula for the real part of the result may
                    //  not be robust if the sign of the input real part is
                    // negative.
                    let im_norm_sqr =
                        self.y * self.y + (self.x * self.x + self.z * self.z);
                    if im_norm_sqr >= T::min_positive_value() {
                        // Second formula for the real part of the result,
                        // which is robust for inputs with a negative real
                        // part.
                        let wx2 = (im_norm_sqr * two / (norm - self.w)).sqrt();

                        Self::new(
                            wx2 * half,
                            self.x / wx2,
                            self.y / wx2,
                            self.z / wx2,
                        )
                    } else if self.x.is_zero()
                        && self.y.is_zero()
                        && self.z.is_zero()
                    {
                        // The input is a negative real number.
                        Self::new(
                            zero,
                            (-self.w).sqrt().copysign(self.x),
                            self.y,
                            self.z,
                        )
                    } else {
                        // `im_norm_sqr` is subnormal. Compute the norm of the
                        // imaginary part by scaling up first.
                        let sx = s * self.x;
                        let sy = s * self.y;
                        let sz = s * self.z;
                        let im_norm =
                            (sy * sy + (sx * sx + sz * sz)).sqrt() / s;

                        // Compute the real part according to the second
                        // formula from above.
                        let w = im_norm / (half * (norm - self.w)).sqrt();

                        Self::new(w * half, self.x / w, self.y / w, self.z / w)
                    }
                }
            }
            FpCategory::Zero if self.is_zero() => {
                Self::new(zero, self.x, self.y, self.z)
            }
            FpCategory::Infinite => {
                if self.w == inf
                    || self.x.is_infinite()
                    || self.y.is_infinite()
                    || self.z.is_infinite()
                {
                    let f = |a: T| {
                        if a.is_infinite() {
                            a
                        } else {
                            zero.copysign(a)
                        }
                    };
                    Self::new(inf, f(self.x), f(self.y), f(self.z))
                } else if self.w == -inf {
                    Self::new(
                        zero,
                        inf.copysign(self.x),
                        zero.copysign(self.y),
                        zero.copysign(self.z),
                    )
                } else {
                    // Input has no infinities. Therefore, the square norm
                    // must have overflowed. Let's scale down.
                    // In release mode, the compiler turns the division into
                    // a multiplication, because `s` is a power of two. Thus,
                    // it's fast.
                    (self / s).sqrt() * s.sqrt()
                }
            }
            FpCategory::Nan => Self::nan(),
            _ => {
                // Square norm is subnormal or zero (underflow), but `self`
                // is not zero. Let's scale up.
                // In release mode, the compiler turns the division into a
                // multiplication, because `s.sqrt()` is a power of two. Thus,
                // it's fast.
                (self * s).sqrt() / s.sqrt()
            }
        }
    }
}

#[cfg(feature = "serde")]
impl<T> serde::Serialize for Quaternion<T>
where
    T: serde::Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        (&self.w, &self.x, &self.y, &self.z).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, T> serde::Deserialize<'de> for Quaternion<T>
where
    T: serde::Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let (w, x, y, z) = serde::Deserialize::deserialize(deserializer)?;
        Ok(Self::new(w, x, y, z))
    }
}

#[cfg(test)]
mod tests {

    use {
        crate::{Quaternion, Q32, Q64, UQ32, UQ64},
        num_traits::{ConstOne, ConstZero, Inv, One, Zero},
    };

    #[cfg(any(feature = "std", feature = "libm"))]
    use num_traits::FloatConst;

    #[test]
    fn test_new() {
        // Test the new function
        let q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
        assert_eq!(q.w, 1.0);
        assert_eq!(q.x, 2.0);
        assert_eq!(q.y, 3.0);
        assert_eq!(q.z, 4.0);
    }

    #[test]
    fn test_zero_constant() {
        // Test the zero function
        let q1 = Q32::ZERO;
        let q2 = Q32::default();
        assert_eq!(q1, q2);
    }

    #[test]
    fn test_const_zero_trait() {
        // Test the ConstZero trait
        let q3 = <Q64 as ConstZero>::ZERO;
        let q4 = Q64::default();
        assert_eq!(q3, q4);
    }

    #[test]
    fn test_zero_trait() {
        // Test the Zero trait's `zero` method
        let q1 = Q32::zero();
        let q2 = Q32::default();
        assert_eq!(q1, q2);
        assert!(q1.is_zero());
    }

    #[test]
    fn test_zero_trait_set_zero() {
        // Test the Zero trait's `set_zero` method
        let mut q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
        q.set_zero();
        assert!(q.is_zero());
    }

    #[test]
    fn test_one_constant() {
        // Test the `ONE` constant
        assert_eq!(Q32::ONE, Q32::new(1.0, 0.0, 0.0, 0.0))
    }

    #[test]
    fn test_i_constant() {
        // Test the `I` constant
        assert_eq!(Q64::I, Q64::new(0.0, 1.0, 0.0, 0.0))
    }

    #[test]
    fn test_j_constant() {
        // Test the `J` constant
        assert_eq!(Q32::J, Q32::new(0.0, 0.0, 1.0, 0.0))
    }

    #[test]
    fn test_k_constant() {
        // Test the `K` constant
        assert_eq!(Q64::K, Q64::new(0.0, 0.0, 0.0, 1.0))
    }

    #[test]
    fn test_const_one_trait() {
        // Test the ConstOne trait
        assert_eq!(<Q32 as ConstOne>::ONE, Q32::new(1.0, 0.0, 0.0, 0.0))
    }

    #[test]
    fn test_one_trait_one() {
        // Test the One trait's `one` method
        assert_eq!(<Q64 as One>::one(), Q64::ONE);
        assert!(Q64::ONE.is_one());
    }

    #[test]
    fn test_one_trait_set_one() {
        // Test the One trait's `set_one` method
        let mut q = Q32::new(2.0, 3.0, 4.0, 5.0);
        q.set_one();
        assert!(q.is_one());
    }

    #[test]
    fn test_one_func() {
        // Test the `one` function
        assert_eq!(Q32::one(), Q32::new(1.0, 0.0, 0.0, 0.0));
    }

    #[test]
    fn test_i_func() {
        // Test the `i` function
        assert_eq!(Q64::i(), Q64::new(0.0, 1.0, 0.0, 0.0));
    }

    #[test]
    fn test_j_func() {
        // Test the `j` function
        assert_eq!(Q64::j(), Q64::new(0.0, 0.0, 1.0, 0.0));
    }

    #[test]
    fn test_k_func() {
        // Test the `k` function
        assert_eq!(Q64::k(), Q64::new(0.0, 0.0, 0.0, 1.0));
    }

    #[test]
    fn test_norm_sqr() {
        // Test the norm_sqr function
        assert_eq!(Q32::ZERO.norm_sqr(), 0.0);
        assert_eq!(Q64::ONE.norm_sqr(), 1.0);
        assert_eq!(Q32::I.norm_sqr(), 1.0);
        assert_eq!(Q64::J.norm_sqr(), 1.0);
        assert_eq!(Q32::K.norm_sqr(), 1.0);
        assert_eq!(Q64::new(-8.0, 4.0, 2.0, -1.0).norm_sqr(), 85.0);
        assert_eq!(Q32::new(1.0, -2.0, 3.0, -4.0).norm_sqr(), 30.0);
    }

    #[test]
    fn test_conj() {
        // Test the conj function
        assert_eq!(Q64::ONE.conj(), Q64::ONE);
        assert_eq!(Q32::I.conj(), -Q32::I);
        assert_eq!(Q64::J.conj(), -Q64::J);
        assert_eq!(Q32::K.conj(), -Q32::K);
        assert_eq!(
            Q64::new(-8.0, 4.0, 2.0, -1.0).conj(),
            Q64::new(-8.0, -4.0, -2.0, 1.0)
        );
        assert_eq!(
            Q32::new(1.0, -2.0, 3.0, -4.0).conj(),
            Q32::new(1.0, 2.0, -3.0, 4.0)
        );
    }

    #[test]
    fn test_inv_func() {
        // Test the inv function
        assert_eq!(Q64::ONE.inv(), Q64::ONE);
        assert_eq!(Q32::I.inv(), -Q32::I);
        assert_eq!(Q64::J.inv(), -Q64::J);
        assert_eq!(Q32::K.inv(), -Q32::K);
        assert_eq!(
            Q64::new(1.0, 1.0, -1.0, -1.0).inv(),
            Q64::new(0.25, -0.25, 0.25, 0.25)
        );
        assert_eq!(
            Q32::new(1.0, -2.0, 2.0, -4.0).inv(),
            Q32::new(0.04, 0.08, -0.08, 0.16)
        );
    }

    #[test]
    fn test_inv_trait_for_ref() {
        // Test the inv trait for references
        assert_eq!(Inv::inv(&Q64::ONE), Q64::ONE);
        assert_eq!(Inv::inv(&Q32::I), -Q32::I);
        assert_eq!(Inv::inv(&Q64::J), -Q64::J);
        assert_eq!(Inv::inv(&Q32::K), -Q32::K);
        assert_eq!(
            Inv::inv(&Q64::new(1.0, 1.0, -1.0, -1.0)),
            Q64::new(0.25, -0.25, 0.25, 0.25)
        );
        assert_eq!(
            Inv::inv(&Q32::new(1.0, -2.0, 2.0, -4.0)),
            Q32::new(0.04, 0.08, -0.08, 0.16)
        );
    }

    #[test]
    fn test_inv_trait_for_val() {
        // Test the inv trait
        assert_eq!(Inv::inv(Q64::ONE), Q64::ONE);
        assert_eq!(Inv::inv(Q32::I), -Q32::I);
        assert_eq!(Inv::inv(Q64::J), -Q64::J);
        assert_eq!(Inv::inv(Q32::K), -Q32::K);
        assert_eq!(
            Inv::inv(Q64::new(1.0, 1.0, -1.0, -1.0)),
            Q64::new(0.25, -0.25, 0.25, 0.25)
        );
        assert_eq!(
            Inv::inv(Q32::new(1.0, -2.0, 2.0, -4.0)),
            Q32::new(0.04, 0.08, -0.08, 0.16)
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_normal_values() {
        // Test the norm function for normal floating point values
        let q = Q64::new(1.0, 2.0, 3.0, 4.0);
        assert_eq!(q.norm(), 30.0f64.sqrt());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_zero_quaternion() {
        // Test the norm function for a zero quaternion
        let q = Q32::new(0.0, 0.0, 0.0, 0.0);
        assert_eq!(q.norm(), 0.0, "Norm of zero quaternion should be 0");
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_subnormal_values() {
        // Test the norm function for subnormal floating point values
        let s = f64::MIN_POSITIVE * 0.25;
        let q = Q64::new(s, s, s, s);
        assert!(
            (q.norm() - 2.0 * s).abs() <= 2.0 * s * f64::EPSILON,
            "Norm of subnormal is computed correctly"
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_large_values() {
        // Test the norm function for large floating point values
        let s = f64::MAX * 0.50;
        let q = Q64::new(s, s, s, s);
        assert!(
            (q.norm() - 2.0 * s).abs() <= 2.0 * s * f64::EPSILON,
            "Norm of large values is computed correctly"
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_infinite_values() {
        // Test the norm function for infinite floating point values
        let inf = f32::INFINITY;
        assert_eq!(Q32::new(inf, 1.0, 1.0, 1.0).norm(), inf);
        assert_eq!(Q32::new(1.0, inf, 1.0, 1.0).norm(), inf);
        assert_eq!(Q32::new(1.0, 1.0, inf, 1.0).norm(), inf);
        assert_eq!(Q32::new(1.0, 1.0, 1.0, inf).norm(), inf);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_norm_nan_values() {
        // Test the norm function for NaN floating point values
        let nan = f32::NAN;
        assert!(Q32::new(nan, 1.0, 1.0, 1.0).norm().is_nan());
        assert!(Q32::new(1.0, nan, 1.0, 1.0).norm().is_nan());
        assert!(Q32::new(1.0, 1.0, nan, 1.0).norm().is_nan());
        assert!(Q32::new(1.0, 1.0, 1.0, nan).norm().is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_normal_values() {
        // Test the fast_norm function for normal floating point values
        let q = Q64 {
            w: 1.1,
            x: 2.7,
            y: 3.4,
            z: 4.9,
        };
        assert_eq!(
            q.fast_norm(),
            q.norm(),
            "Fast norm is equal to norm for normal floating point values"
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_zero_quaternion() {
        // Test the fast_norm function for a zero quaternion
        assert_eq!(
            Q32::zero().fast_norm(),
            0.0,
            "Fast norm of zero quaternion should be 0"
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_infinite_values() {
        // Test the fast_norm function for infinite floating point values
        let inf = f32::INFINITY;
        assert_eq!(Q32::new(inf, 1.0, 1.0, 1.0).fast_norm(), inf);
        assert_eq!(Q32::new(1.0, inf, 1.0, 1.0).fast_norm(), inf);
        assert_eq!(Q32::new(1.0, 1.0, inf, 1.0).fast_norm(), inf);
        assert_eq!(Q32::new(1.0, 1.0, 1.0, inf).fast_norm(), inf);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_nan_values() {
        // Test the fast_norm function for NaN floating point values
        let nan = f32::NAN;
        assert!(Q32::new(nan, 1.0, 1.0, 1.0).fast_norm().is_nan());
        assert!(Q32::new(1.0, nan, 1.0, 1.0).fast_norm().is_nan());
        assert!(Q32::new(1.0, 1.0, nan, 1.0).fast_norm().is_nan());
        assert!(Q32::new(1.0, 1.0, 1.0, nan).fast_norm().is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_for_norm_sqr_underflow() {
        // Test the fast_norm function for underflowing norm_sqr
        let s = f64::MIN_POSITIVE;
        let q = Q64::new(s, s, s, s);
        assert_eq!(q.fast_norm(), 0.0);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_fast_norm_for_norm_sqr_overflow() {
        // Test the fast_norm function for overflowing norm_sqr
        let s = f32::MAX / 16.0;
        let q = Q32::new(s, s, s, s);
        assert_eq!(q.fast_norm(), f32::INFINITY);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize() {
        // Test the normalize function
        assert_eq!(Q64::ONE.normalize().unwrap(), UQ64::ONE);
        assert_eq!(Q32::I.normalize().unwrap(), UQ32::I);
        assert_eq!(Q64::J.normalize().unwrap(), UQ64::J);
        assert_eq!(Q32::K.normalize().unwrap(), UQ32::K);
        assert_eq!(
            Q64::new(9.0, 12.0, -12.0, -16.0)
                .normalize()
                .unwrap()
                .into_quaternion(),
            Q64::new(0.36, 0.48, -0.48, -0.64)
        );
        assert_eq!(
            Q32::new(-1.0, -1.0, 1.0, -1.0)
                .normalize()
                .unwrap()
                .into_quaternion(),
            Q32::new(-0.5, -0.5, 0.5, -0.5)
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize_zero() {
        // Test the normalize function for zero
        assert_eq!(Q64::ZERO.normalize(), None);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize_infinity() {
        // Test the normalize function for infinite quaternions
        assert_eq!(Q64::new(f64::INFINITY, 0.0, 0.0, 0.0).normalize(), None);
        assert_eq!(
            Q64::new(0.0, f64::NEG_INFINITY, 0.0, 0.0).normalize(),
            None
        );
        assert_eq!(
            Q64::new(0.0, 0.0, f64::NEG_INFINITY, 0.0).normalize(),
            None
        );
        assert_eq!(Q64::new(0.0, 0.0, 0.0, f64::INFINITY).normalize(), None);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize_nan() {
        // Test the normalize function for NaN quaternions
        assert_eq!(Q64::new(f64::NAN, 0.0, 0.0, 0.0).normalize(), None);
        assert_eq!(Q64::new(0.0, f64::NAN, 0.0, 0.0).normalize(), None);
        assert_eq!(Q64::new(0.0, 0.0, f64::NAN, 0.0).normalize(), None);
        assert_eq!(Q64::new(0.0, 0.0, 0.0, f64::NAN).normalize(), None);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize_infinity_and_nan() {
        // Test the normalize function for quaternions with infinite and NaN
        // values
        assert_eq!(
            Q64::new(f64::INFINITY, f64::NAN, 1.0, 0.0).normalize(),
            None
        );
        assert_eq!(
            Q64::new(1.0, 0.0, f64::INFINITY, f64::NAN).normalize(),
            None
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_normalize_subnormal() {
        // Test the normalize function for subnormal quaternions
        let s = f64::MIN_POSITIVE / 4.0;
        let q = Q64::new(s, s, s, s);
        assert_eq!(
            q.normalize().unwrap().into_inner(),
            Q64::new(0.5, 0.5, 0.5, 0.5)
        );
    }

    #[test]
    fn test_from_underlying_type_val() {
        // Test the From trait for values
        assert_eq!(Q64::from(-5.0), Q64::new(-5.0, 0.0, 0.0, 0.0));
        assert_eq!(Into::<Q32>::into(42.0), Q32::new(42.0, 0.0, 0.0, 0.0));
    }

    #[allow(clippy::needless_borrows_for_generic_args)]
    #[test]
    fn test_from_underlying_type_ref() {
        // Test the From trait for references
        assert_eq!(Q64::from(&-5.0), Q64::new(-5.0, 0.0, 0.0, 0.0));
        assert_eq!(Into::<Q32>::into(&42.0), Q32::new(42.0, 0.0, 0.0, 0.0));
    }

    #[test]
    fn test_from_unit_quaternion() {
        // Test the From trait for unit quaternions
        assert_eq!(Q32::from(UQ32::ONE), Q32::ONE);
        assert_eq!(Q64::from(UQ64::I), Q64::I);
        assert_eq!(Q32::from(UQ32::J), Q32::J);
        assert_eq!(Q64::from(UQ64::K), Q64::K);
    }

    #[allow(clippy::needless_borrows_for_generic_args)]
    #[test]
    fn test_from_unit_quaternion_ref() {
        // Test the From trait for references to unit quaternions
        assert_eq!(<&Q32 as From<&UQ32>>::from(&UQ32::ONE), &Q32::ONE);
        assert_eq!(<&Q64 as From<&UQ64>>::from(&UQ64::I), &Q64::I);
        assert_eq!(<&Q32 as From<&UQ32>>::from(&UQ32::J), &Q32::J);
        assert_eq!(<&Q64 as From<&UQ64>>::from(&UQ64::K), &Q64::K);
    }

    #[test]
    fn test_powu() {
        // Test the power function for unsigned integer exponents
        for q in [
            Q32::ONE,
            Q32::ZERO,
            Q32::I,
            Q32::new(1.0, 1.0, 1.0, 1.0),
            Q32::new(1.0, 2.0, -3.0, 4.0),
        ] {
            let mut expected = Q32::ONE;
            for e in 0..16 {
                assert_eq!(q.powu(e), expected);
                expected *= q;
            }
        }
    }

    #[test]
    fn test_powi() {
        // Test the power function for signed integer exponents
        for q in [
            Q32::ONE,
            Q32::I,
            Q32::new(1.0, 1.0, 1.0, 1.0),
            Q32::new(1.0, 2.0, -3.0, 4.0),
        ] {
            let mut expected = Q32::ONE;
            for e in 0..16 {
                assert_eq!(q.powi(e), expected);
                assert!(
                    (q.powi(-e) - expected.inv()).norm_sqr()
                        / expected.norm_sqr()
                        < f32::EPSILON
                );
                expected *= q;
            }
        }
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_zero_quaternion() {
        // Test the exponential function for the zero quaternion
        assert_eq!(Q32::ZERO.exp(), Q32::ONE);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_real_part_only() {
        // Test the exponential function for quaternions with real part only
        assert_eq!(Q32::ONE.exp(), core::f32::consts::E.into())
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_imaginary_part_only() {
        // Test the exponential function for quaternions with imaginary part only
        assert!(
            (Q64::I.exp() - Q64::new(1.0f64.cos(), 1.0f64.sin(), 0.0, 0.0))
                .norm()
                <= f64::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_complex_quaternion() {
        // Test the exponential function for a complex quaternion
        let q = Q32::new(1.0, 1.0, 1.0, 1.0);
        let exp_q = q.exp();
        let expected_norm = 1.0f32.exp();
        let angle = 3.0f32.sqrt();
        assert!(
            (exp_q
                - Q32::new(
                    expected_norm * angle.cos(),
                    expected_norm * angle.sin() / angle,
                    expected_norm * angle.sin() / angle,
                    expected_norm * angle.sin() / angle
                ))
            .norm()
                <= 2.0 * expected_norm * f32::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_negative_real_part() {
        // Test the exponential function for quaternions with negative real part
        let q = Q64::new(-1000.0, 0.0, f64::INFINITY, f64::NAN);
        let exp_q = q.exp();
        assert_eq!(exp_q, Q64::zero());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_nan_input() {
        // Test the exponential function for quaternions with NaN input
        let q = Q32::new(f32::NAN, 1.0, 1.0, 1.0);
        let exp_q = q.exp();
        assert!(exp_q.w.is_nan());
        assert!(exp_q.x.is_nan());
        assert!(exp_q.y.is_nan());
        assert!(exp_q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_large_imaginary_norm() {
        // Test the exponential function for quaternions with large imaginary norm
        let q = Q32::new(1.0, 1e30, 1e30, 1e30);
        let exp_q = q.exp();
        assert!(exp_q.w.is_nan());
        assert!(exp_q.x.is_nan());
        assert!(exp_q.y.is_nan());
        assert!(exp_q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_infinite_real_part() {
        // Test the exponential function for quaternions with infinite real part
        let inf = f64::INFINITY;
        let q = Quaternion::new(inf, 1.0, 1.0, 1.0);
        let exp_q = q.exp();
        assert_eq!(exp_q.w, -inf);
        assert_eq!(exp_q.x, inf);
        assert_eq!(exp_q.y, inf);
        assert_eq!(exp_q.z, inf);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_infinite_imaginary_part() {
        // Test the exponential function for quaternions with infinite
        // imaginary part
        let q = Q32::new(1.0, f32::INFINITY, 0.0, 0.0);
        let exp_q = q.exp();
        assert!(exp_q.w.is_nan());
        assert!(exp_q.x.is_nan());
        assert!(exp_q.y.is_nan());
        assert!(exp_q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_small_imaginary_norm() {
        // Test the exponential function for quaternions with small norm of the imaginary part
        let epsilon = f32::EPSILON;
        let q = Quaternion::new(0.5, epsilon, epsilon, epsilon);
        let exp_q = q.exp();
        let result_norm = q.w.exp();
        let expected_exp_q = Quaternion::new(
            result_norm,
            result_norm * q.x,
            result_norm * q.y,
            result_norm * q.z,
        );
        assert!((exp_q - expected_exp_q).norm() <= epsilon);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_infinite_result_angle_greater_than_90_degrees() {
        // Test the exponential function for quaternions with an angle greater
        // than 90 degrees
        let angle = f32::PI() * 0.75; // Angle > 90 degrees
        let q = Q32::new(f32::INFINITY, angle, 0.0, 0.0);
        let exp_q = q.exp();
        assert_eq!(exp_q.w, -f32::INFINITY);
        assert_eq!(exp_q.x, f32::INFINITY);
        assert_eq!(exp_q.y, 0.0);
        assert_eq!(exp_q.z, 0.0);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_exp_infinite_result_angle_greater_than_180_degrees() {
        // Test the exponential function for quaternions with an angle greater
        // than 180 degrees
        let angle = f64::PI() * 1.25; // Angle > 180 degrees
        let q = Q64::new(f64::INFINITY, 0.0, angle, 0.0);
        let exp_q = q.exp();
        assert_eq!(exp_q.w, -f64::INFINITY);
        assert_eq!(exp_q.x, 0.0);
        assert_eq!(exp_q.y, -f64::INFINITY);
        assert_eq!(exp_q.z, 0.0);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_zero_base_positive_real_quaternion() {
        // Test the exponential function for a quaternion with a zero base and a
        // positive real part
        assert_eq!(Q64::new(1.0, 0.0, 0.0, 0.0).expf(0.0), Q64::ZERO);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_zero_base_negative_real_quaternion() {
        // Test the exponential function for a quaternion with a zero base and a
        // negative real part
        assert_eq!(
            Q32::new(-1.0, 0.0, 0.0, 0.0).expf(0.0),
            f32::INFINITY.into()
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_infinity_base_positive_real_quaternion() {
        // Test the exponential function for a quaternion with an infinite base
        // and a positive real part
        let inf = f64::INFINITY;
        assert_eq!(Q64::new(1.0, 0.0, 0.0, 0.0).expf(inf), inf.into());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_infinity_base_negative_real_quaternion() {
        // Test the exponential function for a quaternion with an infinite base
        // and a negative real part
        assert_eq!(
            Q32::new(-1.0, 0.0, 0.0, 0.0).expf(f32::INFINITY),
            0.0f32.into()
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_negative_base() {
        // Test the expf power function for negative exponent and positive base
        let q = Q64::new(1.0, 0.0, 0.0, 0.0).expf(-1.0);
        assert!(q.w.is_nan());
        assert!(q.x.is_nan());
        assert!(q.y.is_nan());
        assert!(q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_nan_base() {
        // Test the expf power function for a NaN base
        let q = Q32::new(1.0, 0.0, 0.0, 0.0).expf(f32::NAN);
        assert!(q.w.is_nan());
        assert!(q.x.is_nan());
        assert!(q.y.is_nan());
        assert!(q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_finite_positive_base() {
        // Test the expf power function for a finite positive base
        let q = Q64::new(1.0, 2.0, 3.0, 4.0);
        let base = 2.0;
        let result = q.expf(base);
        let expected = (q * base.ln()).exp();
        assert!((result - expected).norm() <= expected.norm() * f64::EPSILON);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_nan_quaternion_component() {
        // Test the expf power function for a base with a NaN component
        let q = Q64::new(f64::NAN, 1.0, 1.0, 1.0).expf(3.0);
        assert!(q.w.is_nan());
        assert!(q.x.is_nan());
        assert!(q.y.is_nan());
        assert!(q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_infinity_quaternion_component() {
        // Test the expf power function for a base with an infinite component
        let q = Q32::new(1.0, f32::INFINITY, 1.0, 1.0).expf(2.0);
        assert!(q.w.is_nan());
        assert!(q.x.is_nan());
        assert!(q.y.is_nan());
        assert!(q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_infinite_real_component_with_t_greater_than_1() {
        // Test the expf power function for an infinite real base with a
        // finite real exponent greater than `1`
        let inf = f64::INFINITY;
        assert!(!Q64::new(inf, 0.0, 0.0, 0.0).expf(5.0).is_finite());
        assert!(!Q64::new(inf, 0.0, 0.0, 0.0).expf(5.0).has_nan());
        assert!(!Q64::new(inf, 1.0, 2.0, 3.0).expf(42.0).is_finite());
        assert!(!Q64::new(inf, 1.0, 2.0, 3.0).expf(42.0).has_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_expf_neg_infinite_real_component_with_t_between_0_and_1() {
        // Test the expf power function for a negative infinite real base with
        // an exponent between 0 and 1
        assert!(!Q32::new(f32::NEG_INFINITY, 0.0, 0.0, 0.0)
            .expf(0.5)
            .is_finite());
        assert!(!Q32::new(f32::NEG_INFINITY, 0.0, 0.0, 0.0)
            .expf(0.5)
            .has_nan());
        assert!(!Q32::new(f32::NEG_INFINITY, 1.0, 2.0, 3.0)
            .expf(0.75)
            .is_finite());
        assert!(!Q32::new(f32::NEG_INFINITY, 1.0, 2.0, 3.0)
            .expf(0.75)
            .has_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_zero_q_positive_t() {
        // Test the power function for a zero quaternion and a positive
        // exponent
        let q = Quaternion::zero();
        let t = 1.0;
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_negative_t() {
        // Test the power function for an infinite quaternion and a negative
        // exponent
        let q = Quaternion::new(f64::INFINITY, 0.0, 0.0, 0.0);
        let t = -1.0;
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_positive_t_not_too_large() {
        // Test the power function for an infinite quaternion and a positive
        // exponent
        let q = Quaternion::new(f64::INFINITY, 0.0, 0.0, 0.0);
        let t = 1.0;
        let result = q.powf(t);
        assert_eq!(result, f64::INFINITY.into());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_large_positive_t() {
        // Test the power function for an infinite quaternion and a very large
        // positive exponent
        let q = Quaternion::new(0.0, f64::INFINITY, 0.0, 0.0);
        let t = f64::MAX;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_infinite_t() {
        // Test the power function for an infinite quaternion and an infinite
        // exponent
        let q = Quaternion::new(f64::INFINITY, 0.0, 0.0, 0.0);
        let t = f64::INFINITY;
        let result = q.powf(t);
        assert_eq!(result, f64::INFINITY.into());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_infinite_t_not_positive() {
        // Test the power function for an infinite non-positive quaternion
        // and an infinite positive exponent
        let q = Quaternion::new(f64::INFINITY, 1.0, 0.0, 0.0);
        let t = f64::INFINITY;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_zero_q_negative_t() {
        // Test that the power function for a zero quaternion and a negative
        // exponent returns an infinite quaternion
        let q = Quaternion::zero();
        let t = -1.0;
        let result = q.powf(t);
        assert_eq!(result, f64::INFINITY.into());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_nan_q_or_t() {
        // Test the power function for a quaternion or exponent with a NaN
        // component.
        let q = Quaternion::new(0.0, 0.0, f64::NAN, 0.0);
        let t = 1.0;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());

        let q = Quaternion::one();
        let t = f64::NAN;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_infinite_q_zero_t() {
        // Test the power function for an infinite quaternion and a zero
        // exponent
        let q = Quaternion::new(f64::INFINITY, 0.0, 0.0, 0.0);
        let t = 0.0;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_zero_q_zero_t() {
        // Test the power function for a zero quaternion and a zero exponent
        let q = Q32::zero();
        let t = 0.0;
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_non_zero_q_positive_infinite_t() {
        // Test the power function for a non-zero finite quaternion and a
        // positive infinite exponent
        let q = Quaternion::new(2.0, 0.0, 0.0, 0.0);
        let t = f64::INFINITY;
        let result = q.powf(t);
        assert_eq!(result, f64::INFINITY.into());

        let q = Quaternion::new(0.5, 0.0, 0.0, 0.0);
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());

        let q = Quaternion::new(0.25, 0.25, 0.25, 0.25);
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());

        let q = Quaternion::new(1.0, 0.0, 0.0, 0.0);
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_powf_non_zero_q_negative_infinite_t() {
        // Test the power function for a non-zero finite quaternion and a
        // negative infinite exponent
        let q = Quaternion::new(2.0, 0.0, 0.0, 0.0);
        let t = f64::NEG_INFINITY;
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());

        let q = Quaternion::new(1.0, 0.0, 0.0, 1.0);
        let result = q.powf(t);
        assert_eq!(result, Quaternion::zero());

        let q = Quaternion::new(0.5, 0.0, 0.0, 0.0);
        let result = q.powf(t);
        assert_eq!(result, f64::INFINITY.into());

        let q = Quaternion::new(0.25, 0.25, 0.25, 0.25);
        let result = q.powf(t);
        assert!(result.is_all_nan());

        let q = Quaternion::new(1.0, 0.0, 0.0, 0.0);
        let result = q.powf(t);
        assert!(result.w.is_nan());
        assert!(result.x.is_nan());
        assert!(result.y.is_nan());
        assert!(result.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_is_finite_for_finite_values() {
        // Test the is_finite method for finite values
        let q = Q64::new(1.0, 2.0, 3.0, 4.0);
        assert!(q.is_finite());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_is_finite_for_zero() {
        // Test the is_finite method for the zero quaternion
        let q = Q32::zero();
        assert!(q.is_finite());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_is_finite_for_infinite_values() {
        // Test the is_finite method for infinite values
        let inf = f32::INFINITY;
        assert!(!Q32::new(inf, 1.0, 1.0, 1.0).is_finite());
        assert!(!Q32::new(1.0, inf, 1.0, 1.0).is_finite());
        assert!(!Q32::new(1.0, 1.0, inf, 1.0).is_finite());
        assert!(!Q32::new(1.0, 1.0, 1.0, inf).is_finite());
        assert!(!Q32::new(-inf, 1.0, 1.0, 1.0).is_finite());
        assert!(!Q32::new(1.0, -inf, 1.0, 1.0).is_finite());
        assert!(!Q32::new(1.0, 1.0, -inf, 1.0).is_finite());
        assert!(!Q32::new(1.0, 1.0, 1.0, -inf).is_finite());
        assert!(!Q32::new(inf, -inf, inf, -inf).is_finite());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_is_finite_for_nan_values() {
        // Test the is_finite method for nan values
        let nan = f64::NAN;
        assert!(!Q64::new(nan, 1.0, 1.0, 1.0).is_finite());
        assert!(!Q64::new(1.0, nan, 1.0, 1.0).is_finite());
        assert!(!Q64::new(1.0, 1.0, nan, 1.0).is_finite());
        assert!(!Q64::new(1.0, 1.0, 1.0, nan).is_finite());
        assert!(!Q64::new(-nan, 1.0, 1.0, 1.0).is_finite());
        assert!(!Q64::new(1.0, -nan, 1.0, 1.0).is_finite());
        assert!(!Q64::new(1.0, 1.0, -nan, 1.0).is_finite());
        assert!(!Q64::new(1.0, 1.0, 1.0, -nan).is_finite());
        assert!(!Q64::new(nan, -nan, nan, -nan).is_finite());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_has_nan() {
        // Test the has_nan method for nan values
        let nan = f64::NAN;
        let inf = f64::INFINITY;
        assert!(!Q64::new(0.0, 0.0, 0.0, 0.0).has_nan());
        assert!(!Q64::new(1.0, 1.0, 1.0, 1.0).has_nan());
        assert!(!Q64::new(inf, inf, inf, inf).has_nan());
        assert!(Q64::new(nan, 1.0, 1.0, 1.0).has_nan());
        assert!(Q64::new(1.0, nan, 1.0, 1.0).has_nan());
        assert!(Q64::new(1.0, 1.0, nan, 1.0).has_nan());
        assert!(Q64::new(1.0, 1.0, 1.0, nan).has_nan());
        assert!(Q64::new(-nan, 1.0, 1.0, 1.0).has_nan());
        assert!(Q64::new(1.0, -nan, 1.0, 1.0).has_nan());
        assert!(Q64::new(1.0, 1.0, -nan, 1.0).has_nan());
        assert!(Q64::new(1.0, 1.0, 1.0, -nan).has_nan());
        assert!(Q64::new(nan, -nan, nan, -nan).has_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_is_all_nan() {
        // Test the is_all_nan method for nan values
        let nan = f64::NAN;
        let inf = f64::INFINITY;
        assert!(!Q64::new(0.0, 0.0, 0.0, 0.0).is_all_nan());
        assert!(!Q64::new(1.0, 1.0, 1.0, 1.0).is_all_nan());
        assert!(!Q64::new(inf, inf, inf, inf).is_all_nan());
        assert!(!Q64::new(nan, 1.0, 1.0, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, nan, 1.0, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, 1.0, nan, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, 1.0, 1.0, nan).is_all_nan());
        assert!(!Q64::new(-nan, 1.0, 1.0, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, -nan, 1.0, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, 1.0, -nan, 1.0).is_all_nan());
        assert!(!Q64::new(1.0, 1.0, 1.0, -nan).is_all_nan());
        assert!(Q64::new(nan, nan, nan, nan).is_all_nan());
        assert!(Q64::new(-nan, -nan, -nan, -nan).is_all_nan());
        assert!(Q64::new(nan, -nan, nan, -nan).is_all_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_normal_case() {
        // Test a normal quaternion
        let q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
        let ln_q = q.ln();
        assert!((q.w - 30.0f64.ln() / 2.0) <= 4.0 * f64::EPSILON);
        assert!((ln_q.z / ln_q.x - q.z / q.x) <= 2.0 * f64::EPSILON);
        assert!((ln_q.y / ln_q.x - q.y / q.x) <= 2.0 * f64::EPSILON);
        assert!(
            (ln_q.x.hypot(ln_q.y.hypot(ln_q.z)) - 29.0f64.sqrt().atan())
                <= 4.0 * f64::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_positive_real_axis() {
        // Test close to the positive real axis
        let q = Quaternion::new(1.0, 1e-10, 1e-10, 1e-10);
        let ln_q = q.ln();
        let expected = Quaternion::new(0.0, 1e-10, 1e-10, 1e-10); // ln(1) = 0 and imaginary parts small
        assert!((ln_q - expected).norm() <= 1e-11);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_negative_real_axis() {
        // Test close to the negative real axis
        let q = Q32::new(-1.0, 0.0, 0.0, 0.0);
        let ln_q = q.ln();
        let expected = Q32::new(0.0, core::f32::consts::PI, 0.0, 0.0); // ln(-1) = pi*i
        assert!(
            (ln_q - expected).norm() <= core::f32::consts::PI * f32::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_zero() {
        // Test the zero quaternion
        let q = Q32::new(0.0, 0.0, 0.0, 0.0);
        let ln_q = q.ln();
        let expected = f32::NEG_INFINITY.into();
        assert_eq!(ln_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_negative_zero() {
        // Test the negative zero quaternion
        let q = Q64::new(-0.0, 0.0, 0.0, 0.0);
        let ln_q = q.ln();
        let expected = Q64::new(f64::NEG_INFINITY, 0.0, 0.0, 0.0);
        assert_eq!(ln_q.w, expected.w);
        assert_eq!(ln_q.x, expected.x);
        assert_eq!(ln_q.y, expected.y);
        assert_eq!(ln_q.z, expected.z);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_nan() {
        // Test a quaternion with NaN
        let q = Quaternion::new(f32::NAN, 1.0, 1.0, 1.0);
        let ln_q = q.ln();
        assert!(ln_q.w.is_nan());
        assert!(ln_q.x.is_nan());
        assert!(ln_q.y.is_nan());
        assert!(ln_q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_infinite() {
        // Test a quaternion with infinite components
        let q = Q32::new(f32::INFINITY, 1.0, 1.0, 1.0);
        let ln_q = q.ln();
        let expected = Quaternion::new(f32::INFINITY, 0.0, 0.0, 0.0); // Real part infinity, imaginary parts 0
        assert_eq!(ln_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_finite_and_infinite() {
        // Test a quaternion with finite and infinite components

        use num_traits::Signed;
        let q = Quaternion::new(1.0, f32::INFINITY, -f32::INFINITY, -1.0);
        let ln_q = q.ln();
        let expected = Quaternion::new(
            f32::INFINITY,
            core::f32::consts::PI / 8.0f32.sqrt(),
            -core::f32::consts::PI / 8.0f32.sqrt(),
            0.0,
        );
        assert_eq!(ln_q.w, expected.w);
        assert!((ln_q.x - expected.x).abs() <= 4.0f32 * f32::EPSILON);
        assert!((ln_q.y - expected.y).abs() <= 4.0f32 * f32::EPSILON);
        assert_eq!(ln_q.z, 0.0);
        assert!(ln_q.z.is_negative());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_negative_real_part_tiny_imaginary_part() {
        // Test a quaternion with a tiny imaginary part
        use core::f32;
        let q = Q32::new(-2.0, 346.0 * f32::EPSILON, 0.0, 0.0);
        let ln_q = q.ln();
        let expected =
            Q32::new(2.0f32.ln(), f32::consts::PI + q.x / q.w, 0.0, 0.0);
        assert!((ln_q - expected).norm() <= 8.0 * f32::EPSILON);

        let q = Q32::new(-3.0, f32::MIN_POSITIVE / 64.0, 0.0, 0.0);
        let ln_q = q.ln();
        let expected = Q32::new(3.0f32.ln(), f32::consts::PI, 0.0, 0.0);
        assert_eq!(ln_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_tiny_real_part_and_tiny_imaginary_part() {
        // Test a quaternion with a tiny real and imaginary part

        use core::f32;
        let w = f32::MIN_POSITIVE.sqrt();
        let q = Q32::new(w, 0.0, w / 2.0, 0.0);
        let ln_q = q.ln();
        let expected = Q32::new(
            (1.25 * f32::MIN_POSITIVE).ln() / 2.0,
            0.0,
            0.5f32.atan(),
            0.0,
        );
        assert_eq!(ln_q, expected);

        let w = f32::MIN_POSITIVE;
        let q = Q32::new(w, w, w, w);
        let ln_q = q.ln();
        let expected = Q32::new(
            (2.0 * f32::MIN_POSITIVE).ln(),
            f32::consts::PI / 27.0f32.sqrt(),
            f32::consts::PI / 27.0f32.sqrt(),
            f32::consts::PI / 27.0f32.sqrt(),
        );
        assert!(
            (ln_q - expected).norm() <= expected.norm() * 2.0 * f32::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_ln_very_large_inputs() {
        // Test the logarithm of very large inputs

        use core::f64;
        let q = Q64::new(f64::MAX, 0.0, 0.0, f64::MAX);
        let ln_q = q.ln();
        let expected = Q64::new(
            f64::MAX.ln() + 2.0f64.ln() / 2.0,
            0.0,
            0.0,
            f64::consts::PI / 4.0,
        );
        assert!(
            (ln_q - expected).norm() <= expected.norm() * 2.0 * f64::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_normal() {
        // Test the square root of a normal quaternion
        let q = Q64::new(1.0, 2.0, 3.0, 4.0);
        assert!(((q * q).sqrt() - q).norm() <= q.norm() * f64::EPSILON);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_zero() {
        // Test the square root of the zero quaternion
        assert_eq!(Q32::ZERO.sqrt(), Q32::ZERO);
        let zero = Q32::new(-0.0, 0.0, -0.0, -0.0);
        assert!(zero.sqrt().w.is_sign_positive());
        assert!(zero.sqrt().x.is_sign_positive());
        assert!(zero.sqrt().y.is_sign_negative());
        assert!(zero.sqrt().z.is_sign_negative());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_negative_real() {
        // Test the square root of a negative real quaternion
        let q = Q64::new(-4.0, -0.0, 0.0, 0.0);
        let sqrt_q = q.sqrt();
        let expected = Q64::new(0.0, -2.0, 0.0, 0.0);
        assert_eq!(sqrt_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_nan() {
        // Test the square root of a quaternion with NaN
        let q = Q32::new(f32::NAN, 0.0, 0.0, 0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w.is_nan());
        assert!(sqrt_q.x.is_nan());
        assert!(sqrt_q.y.is_nan());
        assert!(sqrt_q.z.is_nan());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_infinity() {
        // Test the square root of a quaternion with infinite components
        let q = Q64::new(f64::INFINITY, -0.0, -0.0, 0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w.is_infinite());
        assert!(sqrt_q.x.is_zero());
        assert!(sqrt_q.y.is_zero());
        assert!(sqrt_q.z.is_zero());
        assert!(sqrt_q.w.is_sign_positive());
        assert!(sqrt_q.x.is_sign_negative());
        assert!(sqrt_q.y.is_sign_negative());
        assert!(sqrt_q.z.is_sign_positive());

        let q = Q32::new(0.0, 0.0, -f32::INFINITY, -0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w.is_infinite());
        assert!(sqrt_q.x.is_zero());
        assert!(sqrt_q.y.is_infinite());
        assert!(sqrt_q.z.is_zero());
        assert!(sqrt_q.w.is_sign_positive());
        assert!(sqrt_q.x.is_sign_positive());
        assert!(sqrt_q.y.is_sign_negative());
        assert!(sqrt_q.z.is_sign_negative());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_negative_infinity_real() {
        // Test the square root of a quaternion with a negative infinite real
        // part
        let q = Quaternion::new(-f64::INFINITY, 0.0, -1.0, 0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w.is_zero());
        assert!(sqrt_q.x.is_infinite());
        assert!(sqrt_q.y.is_zero());
        assert!(sqrt_q.z.is_zero());
        assert!(sqrt_q.w.is_sign_positive());
        assert!(sqrt_q.x.is_sign_positive());
        assert!(sqrt_q.y.is_sign_negative());
        assert!(sqrt_q.z.is_sign_positive());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_commutativity_with_conjugate() {
        // Test the commutativity of the square root with the conjugate
        let q = Q32::new(1.0, 2.0, 3.0, 4.0);
        assert_eq!(q.conj().sqrt(), q.sqrt().conj());
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_subnormal_values() {
        // Test the square root of subnormal values
        let subnormal = f64::MIN_POSITIVE / 2.0;
        let q = Quaternion::new(subnormal, subnormal, subnormal, subnormal);
        let sqrt_q = q.sqrt();
        let norm_sqr = sqrt_q.norm_sqr();
        assert!(
            (norm_sqr - f64::MIN_POSITIVE).abs()
                <= 4.0 * subnormal * f64::EPSILON
        );
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_mixed_infinities() {
        // Test the square root of a quaternion with all infinite components
        // with different signs
        let q = Q32::new(
            -f32::INFINITY,
            -f32::INFINITY,
            f32::INFINITY,
            -f32::INFINITY,
        );
        let sqrt_q = q.sqrt();
        assert_eq!(sqrt_q.w, f32::INFINITY);
        assert_eq!(sqrt_q.x, -f32::INFINITY);
        assert_eq!(sqrt_q.y, f32::INFINITY);
        assert_eq!(sqrt_q.z, -f32::INFINITY);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_positive_real() {
        // Test the square root of a positive real quaternion
        let q = Q64::new(4.0, 0.0, 0.0, 0.0);
        let sqrt_q = q.sqrt();
        let expected = Q64::new(2.0, 0.0, 0.0, 0.0);
        assert_eq!(sqrt_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_purely_imaginary() {
        // Test the square root of a purely imaginary quaternion
        let q = Q32::new(0.0, 3.0, 4.0, 0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w > 0.0);
        assert!((sqrt_q * sqrt_q - q).norm() <= 2.0 * q.norm() * f32::EPSILON);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_negative_imaginary() {
        // Test the square root of a pure quaternion with negative imaginary
        // parts
        let q = Q64::new(0.0, -3.0, -4.0, 0.0);
        let sqrt_q = q.sqrt();
        assert!(sqrt_q.w > 0.0);
        assert!((sqrt_q * sqrt_q - q).norm() <= 16.0 * q.norm() * f64::EPSILON);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_negative_real_part_subnormal_imaginary_part() {
        // Test the square root of a quaternion with a negative real part and
        // subnormal imaginary parts
        let q = Q32::new(-1.0, f32::MIN_POSITIVE / 64.0, 0.0, 0.0);
        let sqrt_q = q.sqrt();
        let expected = Q32::new(q.x / 2.0, 1.0, 0.0, 0.0);
        assert_eq!(sqrt_q, expected);
    }

    #[cfg(any(feature = "std", feature = "libm"))]
    #[test]
    fn test_sqrt_for_overflowing_norm_sqr_of_input() {
        // Test the square root of a quaternion with an overflowing norm_sqr
        let n = f64::MAX / 2.0;
        let q = Q64::new(-n, n, n, n);
        let sqrt_q = q.sqrt();
        let sqrt_n = f64::MAX.sqrt() / 2.0;
        let expected = Q64::new(sqrt_n, sqrt_n, sqrt_n, sqrt_n);
        assert!((sqrt_q - expected).norm() <= expected.norm() * f64::EPSILON);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_quaternion() {
        // Create a sample quaternion
        let q = Q32::new(1.0, 2.0, 3.0, 4.0);

        // Serialize the quaternion to a JSON string
        let serialized =
            serde_json::to_string(&q).expect("Failed to serialize quaternion");

        // Deserialize the JSON string back into a quaternion
        let deserialized: Quaternion<f32> = serde_json::from_str(&serialized)
            .expect("Failed to deserialize quaternion");

        // Assert that the deserialized quaternion is equal to the original
        assert_eq!(q, deserialized);
    }
}