malachite-float 0.13.0

The arbitrary-precision floating-point type Float, with efficient algorithms partially derived from MPFR.
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
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
// Copyright © 2026 Mikhail Hogrefe
//
// Uses code adopted from the GNU MPFR Library.
//
//      Copyright © 2001-2025 Free Software Foundation, Inc.
//
//      Contributed by the Pascaline and Caramba projects, INRIA.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

// Port of MPFR's arctangent (`mpfr_atan`, `atan.c`). An input above 1 in magnitude is inverted and
// its arctangent taken from pi/2. The argument is then reduced by atan(x) = 2 atan((sqrt(1 + x^2)
// - 1)/x) until it is below about 1/sqrt(prec), and what remains is split into binary chunks, each
// of whose arctangent is summed by binary splitting of the series for atan(x)/x (with a table of
// the twenty most common small chunks for low precisions), the pieces being combined through
// atan(a) + atan(b) = atan((a + b)/(1 - ab)). The arctangent is bounded, so it never overflows; it
// underflows only for an input at the very bottom of the exponent range, where it is the input
// itself rounded toward zero, which MPFR's small-input shortcut decides directly.

use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
use crate::float::arithmetic::round_near_x::{
    LEADING_TERM_MIN_EXPONENT, round_rational_leading_term, small_input_shortcut,
};
use crate::float::arithmetic::sin::{
    SCALE, SCALED_INPUT_EXPONENT, UNDERFLOW_EXPONENT, scaled_underflow, underflowed,
};
use crate::float::arithmetic::tan::round_bracket_signed_by;
use crate::{Float, emulate_float_to_float_fn, emulate_rational_to_float_fn};
use alloc::vec;
use core::cmp::Ordering::{self, Equal, Greater};

use core::mem::take;
use malachite_base::num::arithmetic::traits::{
    Abs, Atan, AtanAssign, CeilingLogBase2, IsPowerOf2, Parity, PowerOf2, Reciprocal, Square,
    SquareAssign,
};
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::{
    NaN as NaNTrait, NegativeZero as NegativeZeroTrait, One, Zero as ZeroTrait,
};
use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_base::split_into_chunks_mut;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use malachite_nz::natural::arithmetic::float::round::float_can_round;
use malachite_nz::platform::Limb;
use malachite_q::Rational;

// For each pair (r, p), a 192-bit truncation of atan(x)/x for x = p/2^r, as three 64-bit words with
// the lowest first; these are the chunks that a precision of at most 192 bits meets most often, and
// that are the most expensive to sum. MPFR only compiles this table for 64-bit limbs; the values do
// not depend on the limb width, so it is used unconditionally here.
//
// In Sage: for p in range(1, 2^ceil(r/2)), with x = p/2^r, the words are floor(2^192*n(atan(x)/x,
// 300)).digits(2^64).
const ATAN_TABLE: [[u64; 3]; 20] = [
    [0x6e141587261cdf00, 0x6fe445ecbc3a8d03, 0xed63382b0dda7b45], // (1,1)
    [0xaa7fa90388b3836b, 0x6dc79ef5f7a217e5, 0xfadbafc96406eb15], // (2,1)
    [0x319c12cf59d4b2dc, 0xcb2792dc0e2e0d51, 0xffaaddb967ef4e36], // (4,1)
    [0x8b3957d95d9ad922, 0xc897989f3e888ef7, 0xfeadd4d5617b6e32], // (4,2)
    [0xc4e6abc8af62e439, 0x4eb9bf602625f0b4, 0xfd0fcdd343cac19b], // (4,3)
    [0x7c18baeb9bc95789, 0xb12afb6b6d4f7e16, 0xffffaaaaddddb94b], // (8,1)
    [0x6856a0171a2f001a, 0x62351fbbe60af47, 0xfffeaaadddd4b968],  // (8,2)
    [0x69164c094f49da06, 0xd517294f7373d07a, 0xfffd001032cb1179], // (8,3)
    [0x20ef65c10deef460, 0xe78c564015f76048, 0xfffaaadddb94d5bb], // (8,4)
    [0x3ce233aa002f0344, 0x9dd8ea342a65d4cc, 0xfff7ab27a1f32f95], // (8,5)
    [0xa37f403c7279c5cb, 0x13ab53a1c8db8497, 0xfff40103192ce74d], // (8,6)
    [0xe5a85657103c1aa8, 0xb8409e6c914191d3, 0xffefac8a9c40a26b], // (8,7)
    [0x806d0294c0db8816, 0x779d776dda8c6213, 0xffeaaddd4bb12542], // (8,8)
    [0x5545d1914ef21478, 0x3aea58d6660f5a12, 0xffe5051f0aebf73a], // (8,9)
    [0x6e47a91d015f4133, 0xc085ab6b490b7f02, 0xffdeb2787d4adac1], // (8,10)
    [0x4efc1f931f7ec9b3, 0xb7f43cd16195ef4b, 0xffd7b61702b09aad], // (8,11)
    [0xd27d1dbf55fed60d, 0xd812c11d7d473e5e, 0xffd0102cb3c1bfbe], // (8,12)
    [0xca629e927383fe97, 0x8c61aedf58e42206, 0xffc7c0f05db9d1b6], // (8,13)
    [0x4eff0b53d4e905b7, 0x28ac1e800ca31e9d, 0xffbec89d7dddd7e9], // (8,14)
    [0xb0a7931deec6fe60, 0xb46feea78588554b, 0xffb527743c8cdd8f], // (8,15)
];

// A table entry, in [1/2, 1), truncated to `prec` bits.
//
// This is `set_table` from atan.c, MPFR 4.2.2.
fn set_table(prec: u64, x: &[u64; 3]) -> Float {
    let n = (Natural::from(x[2]) << 128u32) | (Natural::from(x[1]) << 64u32) | Natural::from(x[0]);
    Float::from_rational_prec_round(Rational::from(n) >> 192u32, prec, Down).0
}

// Multiplies `xs[k - 1]` by `xs[k]` in place.
fn mul_prev(xs: &mut [Integer], k: usize) {
    let (lo, hi) = xs.split_at_mut(k);
    lo[k - 1] *= &hi[0];
}

// If x = p/2^r, computes an approximation to atan(x)/x using 2^m terms of the series, with an error
// of at most 1 ulp at precision `precy`. Assumes 0 < x < 1, so 1 <= p < 2^r. More precisely, p
// consists of the floor(r/2) bits of the binary expansion of a number 0 < s < 1: the bit of weight
// 2^-1 is for r = 1, so p <= 1; the bit of weight 2^-2 is for r = 2, so p <= 1; the two bits of
// weight 2^-3 and 2^-4 are for r = 4, so p <= 3; and in general p < 2^(r/2).
//
// With X = x^2 = p/2^r the series is 1 - X/3 + X^2/5 - ... + (-1)^k X^k/(2k+1) + ..., summed by
// binary splitting: P(a,b) = p if a+1 = b, else P(a,c) P(c,b); Q(a,b) = (2a+1) 2^r if a+1 = b
// (except Q(0,1) = 1), else Q(a,c) Q(c,b); S(a,b) = p (2a+1) if a+1 = b, else Q(c,b) S(a,c) +
// Q(a,c) P(a,c) S(c,b). Then atan(x)/x ~ S(0,i)/Q(0,i) for an i making (p/2^r)^i/i small enough.
// The factor 2^(r(b-a)) in Q(a,b) is implicit, and is applied when Q is used.
//
// This is `mpfr_atan_aux` from atan.c, MPFR 4.2.2.
fn atan_aux(mut p: Integer, mut r: u64, m: usize, precy: u64) -> Float {
    debug_assert!(p > 0u32);
    debug_assert!(m > 0);
    // tabulate values for small precision and small r, which are the most expensive to compute
    if precy <= 192 {
        let index = match r {
            // p has 1 bit: necessarily p = 1
            1 => Some(0),
            2 => Some(1),
            // p has at most 2 bits: 1 <= p <= 3
            4 => Some(1 + usize::exact_from(&p)),
            // p has at most 4 bits: 1 <= p <= 15
            8 => Some(4 + usize::exact_from(&p)),
            _ => None,
        };
        if let Some(index) = index {
            return set_table(precy, &ATAN_TABLE[index]);
        }
    }
    // From p to p^2, and r to 2r
    p.square_assign();
    r <<= 1;
    // Normalize p
    let n = p.trailing_zeros().unwrap();
    if n > 0 {
        p >>= n;
        r -= n;
    }
    // Since |p/2^r| < 1, and p is a nonzero integer, necessarily r > 0.
    debug_assert!(r > 0);
    // MPFR lays the three tables out in one array of 3(m+1) entries; the p = 1 loop can run one
    // step past 2^m terms, so S and Q get a spare slot each here
    let len = m + 2;
    let mut scratch = vec![Integer::ZERO; (len << 1) + m + 1];
    // ptoj[j] = p^(2^j)
    split_into_chunks_mut!(scratch, len, [s, q], ptoj);
    let mut log2_nb_terms = vec![0u64; len + 1];
    // accu[k] = mult[0] + ... + mult[k], where mult[j] is the number of bits of the corresponding
    // term S[j]/Q[j]
    let mut accu = vec![0i64; len + 1];
    let ri = i64::exact_from(r);
    let mut i = 0u64;
    let mut k = 0usize;
    let p_is_1 = p == 1u32;
    if p_is_1 {
        // special case p = 1: the i-th term being X^i/(2i+1) with X = 1/2^r, we can stop when r i >
        // precy, i.e. i > precy/r
        let mut n = u64::power_of_2(u64::exact_from(m));
        if precy / r <= n {
            n = precy / r + 1;
        }
        while i < n {
            q[k + 1] = Integer::from((i << 1) + 3);
            s[k] = (&q[k + 1] << r) - Integer::from((i << 1) + 1);
            q[k] = &q[k + 1] * Integer::from((i << 1) + 1);
            log2_nb_terms[k] = 1; // S[k]/Q[k] corresponds to 2 terms
            let mut j = (i + 2) >> 1;
            let mut l = 1u64;
            while j.even() {
                debug_assert!(k > 0);
                s[k] *= &q[k - 1];
                let mut t = &s[k - 1] * &q[k];
                t <<= r << l;
                t += &s[k];
                s[k - 1] = t;
                mul_prev(q, k);
                log2_nb_terms[k - 1] = l + 1;
                l += 1;
                j >>= 1;
                k -= 1;
            }
            i += 2;
            k += 1;
        }
    } else {
        // p != 1: precompute the ptoj table
        ptoj[0] = p.clone();
        for im in 1..=m {
            ptoj[im] = (&ptoj[im - 1]).square();
        }
        // main loop: the i-th term being X^i/(2i+1) with X = p/2^r, we can stop when p^i/2^(ri) <
        // 2^-precy, i.e. r i > precy + log2(p^i)
        let n = u64::power_of_2(u64::exact_from(m));
        let mut done = false;
        while i < n && !done {
            // initialize both S[k], Q[k] and S[k+1], Q[k+1]
            q[k + 1] = Integer::from((i << 1) + 3); // Q(i+1,i+2)
            s[k + 1] = &p * Integer::from((i << 1) + 1); // S(i+1,i+2)
            s[k] = (&q[k + 1] << r) - &s[k + 1]; // S(i,i+2)
            q[k] = &q[k + 1] * Integer::from((i << 1) + 1); // Q(i,i+2)
            log2_nb_terms[k] = 1; // S[k]/Q[k] corresponds to 2 terms
            let mut j = (i + 2) >> 1;
            let mut l = 1u64;
            while j.even() {
                // invariant: S[k-1]/Q[k-1] and S[k]/Q[k] correspond to 2^l terms each; combine them
                // into S[k-1]/Q[k-1]
                debug_assert!(k > 0);
                s[k] *= &q[k - 1];
                s[k] *= &ptoj[usize::exact_from(l)];
                let mut t = &s[k - 1] * &q[k];
                t <<= r << l;
                t += &s[k];
                s[k - 1] = t;
                mul_prev(q, k);
                log2_nb_terms[k - 1] = l + 1;
                // now S[k-1]/Q[k-1] corresponds to 2^(l+1) terms
                let mult = (ri << (l + 1))
                    - i64::exact_from(ptoj[usize::exact_from(l + 1)].significant_bits())
                    - 1;
                accu[k - 1] = if k == 1 { mult } else { accu[k - 2] + mult };
                if accu[k - 1] > i64::exact_from(precy) {
                    done = true;
                }
                l += 1;
                j >>= 1;
                k -= 1;
            }
            i += 2;
            k += 1;
        }
    }
    // we need to combine S[0]/Q[0] ... S[k-1]/Q[k-1]
    let mut h = 0u64; // number of terms accumulated in S[k]/Q[k]
    while k > 1 {
        k -= 1;
        // combine S[k-1]/Q[k-1] and S[k]/Q[k]
        s[k] *= &q[k - 1];
        if !p_is_1 {
            s[k] *= &ptoj[usize::exact_from(log2_nb_terms[k - 1])];
        }
        let mut t = &s[k - 1] * &q[k];
        h += u64::power_of_2(log2_nb_terms[k]);
        t <<= r * h;
        t += &s[k];
        s[k - 1] = t;
        mul_prev(q, k);
    }
    let mut s0 = take(&mut s[0]);
    let mut q0 = take(&mut q[0]);
    let precy_i = i64::exact_from(precy);
    let mut diff = i64::exact_from(s0.significant_bits()) - (precy_i << 1);
    let mut expo = diff;
    // a negative shift is a left shift, covering MPFR's mul_2exp branch
    s0 >>= diff;
    diff = i64::exact_from(q0.significant_bits()) - precy_i;
    expo -= diff;
    q0 >>= diff;
    s0 /= q0; // truncating division (both positive)
    // y = (S[0] rounded down to precy) * 2^(expo - r(i-1)); MPFR sets the mantissa via set_z then
    // overrides the exponent, which is exactly this scaling. The scaled value is atan(x)/x, of
    // ordinary size, so attaching the scaling before the conversion keeps every exponent in range.
    Float::from_rational_prec_round(
        Rational::from(s0) << (expo - ri * (i64::exact_from(i) - 1)),
        precy,
        Floor,
    )
    .0
}

// Rounds the sum of an alternating series x - c_1 x^3/3 + c_2 x^5/5 - ... for a nonzero `Rational`
// x with |x| <= 1/2, whose terms decrease in magnitude, so that successive partial sums bracket the
// sum. The coefficients start at c_0 = 1 and c_k is c_(k-1) times `ratio(k)`, or c_(k-1) itself if
// `ratio(k)` is `None`. With all coefficients 1 this is the arctangent; with ratio (2k - 1)/(2k) it
// is the inverse hyperbolic sine.
pub(crate) fn alternating_odd_series<F: Fn(u64) -> Option<Rational>>(
    x: &Rational,
    prec: u64,
    rm: RoundingMode,
    ratio: F,
) -> (Float, Ordering) {
    let negative = *x < 0u32;
    let ax = x.abs();
    let x2 = (&ax).square();
    // hi and lo are the partial sums with an odd and an even number of terms
    let mut power = ax.clone();
    let mut coefficient = Rational::ONE;
    let mut hi = ax;
    let mut lo = Rational::ZERO;
    let mut k = 1u64;
    loop {
        power *= &x2;
        if let Some(r) = ratio(k) {
            coefficient *= r;
        }
        let t = &coefficient * &power / Rational::from((k << 1) + 1);
        if k.odd() {
            lo = &hi - t;
        } else {
            hi = &lo + t;
        }
        if let Some(result) = round_bracket_signed_by(negative, lo.clone(), hi.clone(), prec, rm) {
            return result;
        }
        k += 1;
    }
}

// atan x for a tiny nonzero `Rational` x, bracketed by consecutive partial sums of its alternating
// series: x - x^3/3 < atan x < x - x^3/3 + x^5/5 < x, and so on, a bracket that narrows without
// bound; the arctangent is transcendental, so it eventually rounds unambiguously. This is the
// fallback for the tiny inputs that MPFR's small-input shortcut declines, which are those at the
// very bottom of the exponent range whose result underflows; the general algorithm would otherwise
// work at a precision of about 2^30 bits for them.
fn atan_series(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
    alternating_odd_series(x, prec, rm, |_| None)
}

// One step of `atan_rational_huge`: pi to w bits gives pi/2 to within 2^(1 - w), and [lo, hi]
// brackets atan(1/|x|), so pi/2 - atan(1/|x|) lies between the two ends below.
fn atan_huge_step(
    negative: bool,
    lo: &Rational,
    hi: &Rational,
    w: u64,
    prec: u64,
    rm: RoundingMode,
) -> Option<(Float, Ordering)> {
    // pi_lo <= pi <= pi_lo + 2^(2 - w)
    let pi_lo = Rational::exact_from(&Float::pi_prec_round(w, Floor).0);
    let pi_hi = &pi_lo + Rational::power_of_2(2 - i64::exact_from(w));
    round_bracket_signed_by(
        negative,
        (pi_lo >> 1u32) - hi,
        (pi_hi >> 1u32) - lo,
        prec,
        rm,
    )
}

// atan x for a `Rational` x beyond the top of the exponent range, where neither x nor 1/x is a
// `Float`: atan x = pi/2 - atan(1/x), and atan(1/x) is bracketed by the partial sums of its
// alternating series, so pi/2 minus that bracket settles the result.
fn atan_rational_huge(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
    let negative = *x < 0u32;
    // 0 < t < 2^(-2^30)
    let t = x.abs().reciprocal();
    let mut lo = Rational::ZERO;
    let mut hi = t.clone();
    let mut w = prec + 64;
    // The first bracket, atan(t) in (0, t), is already far narrower than an ulp of pi/2 for a t
    // this small, and needs none of the powers of t, each of which is as long as the input itself.
    if let Some(result) = atan_huge_step(negative, &lo, &hi, w, prec, rm) {
        return result;
    }
    let t2 = (&t).square();
    let mut term = t;
    let mut k = 1u64;
    loop {
        w <<= 1;
        term *= &t2;
        let d = &term / Rational::from((k << 1) + 1);
        if k.odd() {
            lo = &hi - d;
        } else {
            hi = &lo + d;
        }
        if let Some(result) = atan_huge_step(negative, &lo, &hi, w, prec, rm) {
            return result;
        }
        k += 1;
    }
}

// Computes atan(x) for a nonzero `Rational` x, rounded to precision `prec` with rounding mode `rm`.
// (x = 0 is handled by the caller.) The result is never exactly representable, so `rm` must not be
// `Exact`.
//
// The general case rounds the input once and takes its `Float` arctangent at a working precision.
// That is sound because the arctangent is 1-Lipschitz, so the half-ulp of the input carries to the
// result unmagnified, and because the result is never much smaller than the input: |atan t| > |t|/2
// for |t| <= 1, while for |t| > 1 the result lies in (pi/4, pi/2) and the input error is damped by
// 1/(1 + t^2). Both errors together stay below 2^(EXP(a) - m + 1). The two ends of the exponent
// range, where the input itself is not a `Float`, are bracketed instead.
pub(crate) fn atan_rational_helper(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
    assert_ne!(rm, Exact, "Inexact atan");
    let exp_x = x.floor_log_base_2_abs() + 1; // the MPFR-style exponent of x
    if exp_x < UNDERFLOW_EXPONENT {
        // |atan x| < |x| < 2^(MIN_EXPONENT - 2), below half the smallest positive `Float`, so the
        // result is zero or that `Float` by the rounding mode alone, and no 2^30-bit arithmetic is
        // needed
        return underflowed(*x > 0u32, prec, rm);
    }
    // atan(x) = x(1 - x^2/3 + ...), so |x| exceeds |atan x| by less than 2^(3 EXP(x) - 1). Once
    // that is below the distance from x to the nearest (prec + 1)-bit dyadic -- at least 2^(EXP(x)
    // - prec - 1)/d for a denominator of d, the two coinciding only when x is itself such a dyadic,
    // the exact and tie cases -- x's own rounding is the answer, nudged toward zero. The series
    // below would say the same, but its partial sums are formed exactly, and for a tiny x they are
    // dense `Rational`s of about 2 |EXP(x)| bits: 7 seconds for x = 2^-536870908, against
    // microseconds here. (`acot_rational` leans on this too, taking the arctangent of a
    // reciprocal.) Inputs at the bottom of the exponent range are left to the series below: there
    // the nudge and the tie test would be working with `Float`s that underflow.
    if exp_x > LEADING_TERM_MIN_EXPONENT
        && -(exp_x << 1) > i64::exact_from(prec + x.denominator_ref().significant_bits()) + 4
    {
        return round_rational_leading_term(x.abs(), *x > 0u32, false, prec, rm);
    }
    // For |x| <= 1/2, |x| - |x|^3/3 <= |atan x| <= |x| - |x|^3/3 + |x|^5/5, a bracket of relative
    // width below x^4, which decides the rounding once x^4 is below 2^-(prec + 3); a handful of
    // terms is cheaper than a `Float` arctangent at the working precision.
    if exp_x < 0 && -(exp_x << 2) > i64::exact_from(prec) + 3 {
        return atan_series(x, prec, rm);
    }
    if exp_x > Float::MAX_EXPONENT_I64 {
        return atan_rational_huge(x, prec, rm);
    }
    let mut m = prec + prec.ceiling_log_base_2() + 8;
    let mut increment = Limb::WIDTH;
    loop {
        let (f, o_f) = Float::from_rational_prec_ref(x, m);
        if o_f == Equal {
            // x is exactly representable at m bits, so its arctangent is simply the `Float` one
            return atan_prec_round_normal_ref(&f, prec, rm);
        }
        let a = (&f).atan();
        if float_can_round(a.significand_ref().unwrap(), m - 2, prec, rm) {
            return Float::from_float_prec_round(a, prec, rm);
        }
        m += increment;
        increment = m >> 1;
    }
}

// This is mpfr_atan from atan.c, MPFR 4.2.2, for a finite nonzero input, with a series bracket for
// the tiny inputs whose result underflows.
fn atan_prec_round_normal_ref(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
    assert_ne!(rm, Exact, "Inexact atan");
    let exp_x = i64::from(x.get_exponent().unwrap());
    // atan(x) = x - x^3/3 + x^5/5 ..., so the error is < 2^(3 EXP(x) - 1), and `EXP(x) - (3 EXP(x)
    // - 1)` = -2 EXP(x) + 1
    //
    // MPFR_FAST_COMPUTE_IF_SMALL_INPUT (atan, x, -2 * MPFR_GET_EXP (x), 1, 0, rnd_mode, {});
    // atan(x) = x - x^3/3 + ..., so the correction is below 2^(3 EXP(x) - 1) and carries the value
    // toward zero
    if let Some(result) = small_input_shortcut(x, -(exp_x << 1), 1, false, prec, rm) {
        return result;
    }
    let negative = *x < 0u32;
    let xp = x.clone().abs();
    // Other simple case: atan(±1) = ±pi/4
    let comparison = xp.partial_cmp(&1u32).unwrap();
    if comparison == Equal {
        let (pi, o) = Float::pi_prec_round(prec, if negative { -rm } else { rm });
        // exact
        let quarter = pi >> 2u32;
        return if negative {
            (-quarter, o.reverse())
        } else {
            (quarter, o)
        };
    }
    let mut realprec = prec + prec.ceiling_log_base_2() + 4;
    let mut increment = Limb::WIDTH;
    // If |x| < 1, we need more precision to be able to round
    let sup = if exp_x < 0 {
        u64::exact_from(2 - exp_x)
    } else {
        1
    };
    loop {
        // n0 = ceil(log(prec_requested + 2 + 1 + ln(2.4)/ln(2))/log(2))
        let n0 = (realprec + sup + 3).ceiling_log_base_2();
        // since realprec >= 4, n0 >= ceil(log2(8)) >= 3, so 3 n0 > 2
        let mut wp = realprec + sup + 1 + (3 * n0 - 2).ceiling_log_base_2();
        // The number of lost bits due to argument reduction is 9 - 2 EXP(sk), estimated by 9 + 2
        // ceil(log2(p)), since we manage that sk < 1/p. Below 100 bits the argument is not reduced.
        let (log2p, est_lost) = if prec > 100 {
            let log2p = (wp.ceiling_log_base_2() >> 1) - 3;
            (log2p, 9 + (log2p << 1))
        } else {
            (0, 0)
        };
        wp += est_lost;
        // use atan(xp) = pi/2 - atan(1/xp) for xp > 1; now 0 < sk <= 1
        let mut sk = if comparison == Greater {
            xp.reciprocal_prec_ref(wp).0
        } else {
            Float::from_float_prec_ref(&xp, wp).0
        };
        // Argument reduction: atan(x) = 2 atan((sqrt(1 + x^2) - 1)/x), applied until |sk| <
        // k/sqrt(p), where p is the target precision. It keeps 0 < sk <= 1, and is applied at least
        // once when sk = 1, after which sk < 1 (see atan.c for the interval-arithmetic argument).
        let mut lost = 0u64;
        let mut red = 0u64;
        let log2p_i = i64::exact_from(log2p);
        loop {
            let exp_sk = i64::from(sk.get_exponent().unwrap());
            if exp_sk <= -log2p_i {
                break;
            }
            lost = u64::exact_from(9 - (exp_sk << 1));
            let mut tmp = sk.square_prec_ref(wp).0;
            tmp.add_prec_assign_ref(&Float::ONE, wp);
            tmp.sqrt_prec_assign(wp);
            tmp.sub_prec_assign_ref(&Float::ONE, wp);
            sk = if red == 0 && comparison == Greater {
                // use xp = 1/sk
                tmp.mul_prec_val_ref(&xp, wp).0
            } else {
                tmp.div_prec_val_ref(&sk, wp).0
            };
            red += 1;
        }
        debug_assert!(sk < 1u32);
        // Assignation
        let mut arctgt = Float::ZERO;
        let mut twopoweri = 1u64;
        for i in 0..n0 {
            if sk == 0u32 {
                break;
            }
            // trunc(sk 2^twopoweri) as an integer; since the s_k are decreasing (see
            // algorithms.tex) and s_0 = min(|x|, 1/|x|) < 1, sk < 1
            let ukz = Integer::rounding_from(&(&sk << twopoweri), Down).0;
            if ukz != 0u32 {
                // tmp = ukz / 2^twopoweri
                let tmp = Float::from_integer_prec_ref(&ukz, wp).0 >> twopoweri;
                // atan(A_k)
                let tmp2 = atan_aux(ukz, twopoweri, usize::exact_from(n0 - i), wp)
                    .mul_prec_val_ref(&tmp, wp)
                    .0;
                // Addition
                arctgt.add_prec_assign(tmp2, wp);
                // Next iteration
                let tmp2 = sk.sub_prec_ref_ref(&tmp, wp).0;
                sk.mul_prec_assign_ref(&tmp, wp);
                sk.add_prec_assign_ref(&Float::ONE, wp);
                sk = tmp2.div_prec(sk, wp).0;
            }
            twopoweri <<= 1;
        }
        // Add the last step: atan(sk) ~= sk
        arctgt.add_prec_assign(sk, wp);
        // undo the argument reduction
        arctgt <<= red;
        if comparison == Greater {
            // atan(x) = pi/2 - atan(1/x) for x > 0
            arctgt = (Float::pi_prec(wp).0 >> 1u32).sub_prec(arctgt, wp).0;
        }
        debug_assert!(arctgt > 0u32);
        let err = i64::exact_from(realprec + est_lost) - i64::exact_from(lost);
        if err > 0
            && float_can_round(
                arctgt.significand_ref().unwrap(),
                u64::exact_from(err),
                prec,
                rm,
            )
        {
            return Float::from_float_prec_round(if negative { -arctgt } else { arctgt }, prec, rm);
        }
        realprec += increment;
        increment = realprec >> 1;
    }
}

// u/2^k with the sign of `positive`, rounded to `prec` with `rm`. The shift is exact, so the
// ternary value is the conversion's, reversed along with the sign.
pub(crate) fn scaled_unsigned(
    u: u64,
    k: u32,
    positive: bool,
    prec: u64,
    rm: RoundingMode,
) -> (Float, Ordering) {
    let (f, o) = Float::from_unsigned_prec_round(u, prec, if positive { rm } else { -rm });
    let f = f >> k;
    if positive { (f, o) } else { (-f, o.reverse()) }
}

// Computes atan(x) u/(2 pi) for a finite nonzero `Float` x and a nonzero u, rounded to precision
// `prec` with rounding mode `rm`. `rm` may be `Exact` only for |x| = 1, where the result is u/8.
//
// This is mpfr_atanu from atanu.c, MPFR 4.2.2. The quotient is formed with the numerator scaled up
// by 2^SCALE, since atan(x) u/(2 pi) can fall below the smallest positive `Float` for a tiny x and
// a small u, which MPFR, computing inside a temporarily extended exponent range, never sees; a
// result below it is then decided by the rounding mode alone, as in `sin_with_period`.
fn atan_with_period_prec_round_normal_ref(
    x: &Float,
    u: u64,
    prec: u64,
    rm: RoundingMode,
) -> (Float, Ordering) {
    let positive = *x > 0u32;
    let exp_x = i64::from(x.get_exponent().unwrap());
    // |x| = 1: atanu(1, u) = u/8, atanu(-1, u) = -u/8, both exact
    if exp_x == 1 && x.significand_ref().unwrap().is_power_of_2() {
        return scaled_unsigned(u, 3, positive, prec, rm);
    }
    // Only |x| = 1 can be rounded exactly
    assert_ne!(rm, Exact, "Inexact atan_with_period");
    // For x >= 1, pi/2 - 1/x < atan(x) < pi/2, so u/4 - u/(2 pi x) < atanu(x, u) < u/4, and the
    // relative difference from u/4 is below 2/(pi x) < 1/x <= 2^(1 - EXP(x)). Once that is at most
    // 2^(-prec - 2), the difference is at most a quarter ulp of u/4, hence at most half an ulp of
    // u/4 at the target precision, so the result rounds like the value one ulp below u/4 at a
    // working precision above the target's. Requiring EXP(x) >= 65 also gives x > 2u/pi, which
    // keeps that value above (u - 1)/4.
    if exp_x >= 65 && exp_x > i64::exact_from(prec) + 2 {
        let w = if prec <= 63 { 65 } else { prec + 2 };
        // exact, since w >= 64
        let mut t = Float::from_unsigned_prec_round(u, w, Exact).0;
        t.decrement();
        // the last bit of t is 1 and w exceeds the target precision, so t is not representable
        // there, which pins the ternary value below
        t >>= 2u32;
        return Float::from_float_prec_round(if positive { t } else { -t }, prec, rm);
    }
    arc_with_period_scale(
        // scaling by a power of 2 is exact, and atan(x) u 2^SCALE stays far below the top of the
        // range, since |atan x| < pi/2 and u < 2^64
        |w| x.atan_prec_round_ref(w, Up).0 << SCALE,
        u,
        positive,
        prec,
        rm,
    )
}

// The Ziv loop shared by the inverse trigonometric functions with a period: given a way to compute
// f(x) 2^SCALE rounded away from zero at a working precision, forms f(x) u/(2 pi). It is used by
// the `Float` and `Rational` arctangents and by the arcsine, whose error analyses agree.
//
// The numerator is scaled up by 2^SCALE throughout, since f(x) u/(2 pi) can fall below the smallest
// positive `Float` for a tiny x and a small u, which MPFR, computing inside a temporarily extended
// exponent range, never sees; a result below it is then decided by the rounding mode alone, as in
// `sin_with_period`. Scaling before the multiplication rather than after also lets a `Rational` x
// too small to be a `Float` at all reach the loop, which is why the shift belongs to the caller.
pub(crate) fn arc_with_period_scale<F: FnMut(u64) -> Float>(
    mut scaled_f: F,
    u: u64,
    positive: bool,
    prec: u64,
    rm: RoundingMode,
) -> (Float, Ordering) {
    let mut w = prec + prec.ceiling_log_base_2() + 10;
    let mut increment = Limb::WIDTH;
    let u_float = Float::from(u);
    loop {
        // In the error analysis below, each theta denotes a value with |theta| <= 2^(1 - w).
        //
        // t = f(x) 2^SCALE (1 + theta), nonzero since we rounded away from zero and x is not
        let mut t = scaled_f(w);
        // t = f(x) u 2^SCALE (1 + theta)^2
        t.mul_prec_round_assign_ref(&u_float, w, Up);
        // 2 pi rounded toward zero, so that the quotient rounds away
        let two_pi = Float::pi_prec_round(w, Down).0 << 1u32;
        // t = f(x) u 2^SCALE/(2 pi) (1 + theta)^4, whose relative error is below 2^(4 - w) since
        // |(1 + theta)^4 - 1| <= 8 |theta| for w >= 3
        t.div_prec_round_assign(two_pi, w, Up);
        if let Some(result) = scaled_underflow(&t, positive, prec, rm) {
            return result;
        }
        let t = t >> SCALE;
        if float_can_round(t.significand_ref().unwrap(), w - 4, prec, rm) {
            return Float::from_float_prec_round(t, prec, rm);
        }
        w += increment;
        increment = w >> 1;
    }
}

// Computes atan(x) u/(2 pi) for a nonzero `Rational` x and a nonzero u, rounded to precision `prec`
// with rounding mode `rm`. (x = 0 and u = 0 are handled by the caller.) `rm` may be `Exact` only
// for |x| = 1, where the result is u/8.
//
// The three branches match the `Float` case, with one addition: an x below the bottom of the
// exponent range is not a `Float`, but its arctangent is its own leading term, so the quotient is
// formed from x itself. That substitution neglects a relative x^2/3, which for such an x is below
// 2^(2 SCALED_INPUT_EXPONENT) and so far beneath any working precision the loop can reach. It is
// also needed rather than merely cheaper: `atan_rational_helper` reports such an x as an underflow,
// and a large u can lift the quotient back into the range, where that answer would be wrong.
pub(crate) fn atan_with_period_rational_helper(
    x: &Rational,
    u: u64,
    prec: u64,
    rm: RoundingMode,
) -> (Float, Ordering) {
    let positive = *x > 0u32;
    let exp_x = x.floor_log_base_2_abs() + 1; // the MPFR-style exponent of x
    // |x| = 1: atanu(1, u) = u/8, atanu(-1, u) = -u/8, both exact
    if exp_x == 1 && x.denominator_ref() == &1u32 {
        return scaled_unsigned(u, 3, positive, prec, rm);
    }
    // Only |x| = 1 can be rounded exactly
    assert_ne!(rm, Exact, "Inexact atan_with_period_rational");
    // as in the `Float` case, the result is one ulp below u/4 once x is large enough
    if exp_x >= 65 && exp_x > i64::exact_from(prec) + 2 {
        let w = if prec <= 63 { 65 } else { prec + 2 };
        // exact, since w >= 64
        let mut t = Float::from_unsigned_prec_round(u, w, Exact).0;
        t.decrement();
        t >>= 2u32;
        return Float::from_float_prec_round(if positive { t } else { -t }, prec, rm);
    }
    if exp_x <= SCALED_INPUT_EXPONENT {
        let scaled = x << SCALE;
        return arc_with_period_scale(
            |w| Float::from_rational_prec_round_ref(&scaled, w, Up).0,
            u,
            positive,
            prec,
            rm,
        );
    }
    arc_with_period_scale(
        |w| atan_rational_helper(x, w, Up).0 << SCALE,
        u,
        positive,
        prec,
        rm,
    )
}

impl Float {
    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the specified
    /// precision and with the specified rounding mode. The [`Float`] is taken by value. An
    /// [`Ordering`] is also returned, indicating whether the rounded arctangent is less than, equal
    /// to, or greater than the exact arctangent. Although `NaN`s are not comparable to any
    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,p,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// Special cases:
    /// - $f(\text{NaN},p,m)=\text{NaN}$
    /// - $f(\pm\infty,p,m)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,p,m)=\pm0.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
    ///   returned instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_prec`] instead. If you
    /// know that your target precision is the precision of the input, consider using
    /// [`Float::atan_round`] instead. If both of these things are true, consider using
    /// [`Float::atan`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$, or if
    /// `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(5, Floor);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(5, Ceiling);
    /// assert_eq!(c.to_string(), "0.812");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(5, Nearest);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(20, Floor);
    /// assert_eq!(c.to_string(), "0.78539753");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(20, Ceiling);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
    ///     .0
    ///     .atan_prec_round(20, Nearest);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_prec_round_ref(prec, rm)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the specified
    /// precision and with the specified rounding mode. The [`Float`] is taken by reference. An
    /// [`Ordering`] is also returned, indicating whether the rounded arctangent is less than, equal
    /// to, or greater than the exact arctangent. Although `NaN`s are not comparable to any
    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,p,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// Special cases:
    /// - $f(\text{NaN},p,m)=\text{NaN}$
    /// - $f(\pm\infty,p,m)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,p,m)=\pm0.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
    ///   returned instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_prec_ref`] instead. If
    /// you know that your target precision is the precision of the input, consider using
    /// [`Float::atan_round_ref`] instead. If both of these things are true, consider using
    /// `(&Float).atan()` instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$, or if
    /// `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(5, Floor);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(5, Ceiling);
    /// assert_eq!(c.to_string(), "0.812");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(5, Nearest);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(20, Floor);
    /// assert_eq!(c.to_string(), "0.78539753");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(20, Ceiling);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_round_ref(20, Nearest);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    /// ```
    pub fn atan_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        assert_ne!(prec, 0);
        match &self.0 {
            NaN => (Self::NAN, Equal),
            // atan(+infinity) = pi/2, atan(-infinity) = -pi/2
            Infinity { sign } => {
                assert_ne!(rm, Exact, "Inexact atan");
                let (pi, o) = Self::pi_prec_round(prec, if *sign { rm } else { -rm });
                // exact
                let half = pi >> 1u32;
                if *sign {
                    (half, o)
                } else {
                    (-half, o.reverse())
                }
            }
            // atan(+0) = +0, atan(-0) = -0
            Zero { .. } => (self.clone(), Equal),
            Finite { .. } => atan_prec_round_normal_ref(self, prec, rm),
        }
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the nearest
    /// value of the specified precision. The [`Float`] is taken by value. An [`Ordering`] is also
    /// returned, indicating whether the rounded arctangent is less than, equal to, or greater than
    /// the exact arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this
    /// function returns a `NaN` it also returns `Equal`.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// f(x,p) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// Special cases:
    /// - $f(\text{NaN},p)=\text{NaN}$
    /// - $f(\pm\infty,p)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,p)=1.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_prec_round`] instead. If you know that your target precision is the precision
    /// of the input, consider using [`Float::atan`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.atan_prec(5);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.atan_prec(20);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_prec(self, prec: u64) -> (Self, Ordering) {
        self.atan_prec_round(prec, Nearest)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the nearest
    /// value of the specified precision. The [`Float`] is taken by reference. An [`Ordering`] is
    /// also returned, indicating whether the rounded arctangent is less than, equal to, or greater
    /// than the exact arctangent. Although `NaN`s are not comparable to any [`Float`], whenever
    /// this function returns a `NaN` it also returns `Equal`.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// f(x,p) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// Special cases:
    /// - $f(\text{NaN},p)=\text{NaN}$
    /// - $f(\pm\infty,p)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,p)=1.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_prec_round_ref`] instead. If you know that your target precision is the
    /// precision of the input, consider using `(&Float).atan()` instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_ref(5);
    /// assert_eq!(c.to_string(), "0.781");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_prec_ref(20);
    /// assert_eq!(c.to_string(), "0.78539848");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_prec_ref(&self, prec: u64) -> (Self, Ordering) {
        self.atan_prec_round_ref(prec, Nearest)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result with the specified
    /// rounding mode. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating
    /// whether the rounded arctangent is less than, equal to, or greater than the exact arctangent.
    /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
    /// it also returns `Equal`.
    ///
    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
    /// description of the possible rounding modes.
    ///
    /// $$
    /// f(x,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$, where $p$ is the precision of the input.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
    ///
    /// If the output has a precision, it is the precision of the input.
    ///
    /// Special cases:
    /// - $f(\text{NaN},m)=\text{NaN}$
    /// - $f(\pm\infty,m)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,m)=1.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}}<f(x,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is returned
    ///   instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you want to specify an output precision, consider using [`Float::atan_prec_round`]
    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
    /// [`Float::atan`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^3 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`: an input
    /// above 1 in magnitude is first inverted, and the argument is then halved a logarithmic number
    /// of times and split into chunks whose arctangents are summed by binary splitting, all at a
    /// working precision of about $n$. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.atan_round(Floor);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084581983");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.atan_round(Ceiling);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084582062");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.atan_round(Nearest);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084581983");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_round(self, rm: RoundingMode) -> (Self, Ordering) {
        let prec = self.significant_bits();
        self.atan_prec_round(prec, rm)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result with the specified
    /// rounding mode. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
    /// returns a `NaN` it also returns `Equal`.
    ///
    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
    /// description of the possible rounding modes.
    ///
    /// $$
    /// f(x,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$, where $p$ is the precision of the input.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
    ///
    /// If the output has a precision, it is the precision of the input.
    ///
    /// Special cases:
    /// - $f(\text{NaN},m)=\text{NaN}$
    /// - $f(\pm\infty,m)=\pm\pi/2$, rounded
    /// - $f(\pm0.0,m)=1.0$
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}}<f(x,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is returned
    ///   instead.
    ///
    /// Underflow requires an input of magnitude $2^{-2^{30}}$, the smallest positive [`Float`],
    /// rounded toward zero: since $|\arctan x| < |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you want to specify an output precision, consider using [`Float::atan_prec_round_ref`]
    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
    /// `(&Float).atan()` instead.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^3 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`: an input
    /// above 1 in magnitude is first inverted, and the argument is then halved a logarithmic number
    /// of times and split into chunks whose arctangents are summed by binary splitting, all at a
    /// working precision of about $n$. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_round_ref(Floor);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084581983");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_round_ref(Ceiling);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084582062");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).atan_round_ref(Nearest);
    /// assert_eq!(c.to_string(), "0.78539816339744830961566084581983");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_prec_round_ref(self.significant_bits(), rm)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the specified
    /// precision and with the specified rounding mode. The [`Float`] is replaced by the result, and
    /// an [`Ordering`] is returned, indicating whether the rounded arctangent is less than, equal
    /// to, or greater than the exact arctangent. Although `NaN`s are not comparable to any
    /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// x \gets \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// See the [`Float::atan_prec_round`] documentation for information on special cases, overflow,
    /// and underflow.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_prec_assign`] instead.
    /// If you know that your target precision is the precision of the input, consider using
    /// [`Float::atan_round_assign`] instead. If both of these things are true, consider using
    /// [`Float::atan_assign`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$, or if
    /// `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(5, Floor), Less);
    /// assert_eq!(x.to_string(), "0.781");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(5, Ceiling), Greater);
    /// assert_eq!(x.to_string(), "0.812");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(5, Nearest), Less);
    /// assert_eq!(x.to_string(), "0.781");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(20, Floor), Less);
    /// assert_eq!(x.to_string(), "0.78539753");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(20, Ceiling), Greater);
    /// assert_eq!(x.to_string(), "0.78539848");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_round_assign(20, Nearest), Greater);
    /// assert_eq!(x.to_string(), "0.78539848");
    /// ```
    #[inline]
    pub fn atan_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
        let o;
        (*self, o) = self.atan_prec_round_ref(prec, rm);
        o
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result to the nearest
    /// value of the specified precision. The [`Float`] is replaced by the result, and an
    /// [`Ordering`] is returned, indicating whether the rounded arctangent is less than, equal to,
    /// or greater than the exact arctangent. Although `NaN`s are not comparable to any [`Float`],
    /// whenever this function sets a `NaN` it also returns `Equal`.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// x \gets \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// See the [`Float::atan_prec`] documentation for information on special cases, overflow, and
    /// underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_prec_round_assign`] instead. If you know that your target precision is the
    /// precision of the input, consider using [`Float::atan_assign`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: an input above 1 in magnitude is first inverted, and the argument
    /// is then halved a logarithmic number of times and split into chunks whose arctangents are
    /// summed by binary splitting, all at a working precision of about $n$; the summation is the
    /// first term, and the inversion of the $m$-bit input the second. The magnitude of the input
    /// does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_assign(5), Less);
    /// assert_eq!(x.to_string(), "0.781");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_prec_assign(20), Greater);
    /// assert_eq!(x.to_string(), "0.78539848");
    /// ```
    #[inline]
    pub fn atan_prec_assign(&mut self, prec: u64) -> Ordering {
        self.atan_prec_round_assign(prec, Nearest)
    }

    /// Computes $\arctan x$, the arctangent of a [`Float`], rounding the result with the specified
    /// rounding mode. The [`Float`] is replaced by the result, and an [`Ordering`] is returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
    /// a `NaN` it also returns `Equal`.
    ///
    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
    /// description of the possible rounding modes.
    ///
    /// $$
    /// x \gets \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan x|\rfloor-p+1}$, where $p$ is the precision of the input.
    /// - If $x$ is not NaN and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan
    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
    ///
    /// If the output has a precision, it is the precision of the input.
    ///
    /// See the [`Float::atan_round`] documentation for information on special cases, overflow, and
    /// underflow.
    ///
    /// If you want to specify an output precision, consider using [`Float::atan_prec_round_assign`]
    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
    /// [`Float::atan_assign`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n (\log n)^3 \log\log n)$
    ///
    /// $M(n) = O(n \log n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`: an input
    /// above 1 in magnitude is first inverted, and the argument is then halved a logarithmic number
    /// of times and split into chunks whose arctangents are summed by binary splitting, all at a
    /// working precision of about $n$. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` and `self` is nonzero and not NaN, since the arctangent of a
    /// finite nonzero [`Float`] is never exactly representable and neither is $\pm\pi/2$.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_round_assign(Floor), Less);
    /// assert_eq!(x.to_string(), "0.78539816339744830961566084581983");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_round_assign(Ceiling), Greater);
    /// assert_eq!(x.to_string(), "0.78539816339744830961566084582062");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// assert_eq!(x.atan_round_assign(Nearest), Less);
    /// assert_eq!(x.to_string(), "0.78539816339744830961566084581983");
    /// ```
    #[inline]
    pub fn atan_round_assign(&mut self, rm: RoundingMode) -> Ordering {
        let prec = self.significant_bits();
        self.atan_prec_round_assign(prec, rm)
    }
}

impl Float {
    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the specified precision and with the specified rounding mode. The
    /// [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent. Although
    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
    /// returns `Equal`.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,u,p,m) = \arctan(x)u/(2\pi)+\varepsilon.
    /// $$
    /// - If $x$ is NaN or zero, $u = 0$, or $|x|$ is 1 or infinite, $\varepsilon$ may be ignored or
    ///   assumed to be 0.
    /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan(x)u/(2\pi)|\rfloor-p+1}$.
    /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
    ///   |\arctan(x)u/(2\pi)|\rfloor-p}$.
    ///
    /// If the output has a precision, it is `prec`.
    ///
    /// Special cases:
    /// - $f(\text{NaN},u,p,m)=\text{NaN}$
    /// - $f(\pm\infty,u,p,m)=\pm u/4$, a quarter turn
    /// - $f(\pm0.0,u,p,m)=\pm0.0$
    /// - $f(x,0,p,m)=\pm0.0$, with the sign of $x$, so that the function stays odd
    /// - $f(\pm1,u,p,m)=\pm u/8$, an eighth of a turn
    ///
    /// The last three are the only exact cases, and the quarter and eighth turns are exact only
    /// when $p$ is large enough to hold them.
    ///
    /// Underflow:
    /// - If $0<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,u,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - The negative cases mirror these, since the function is odd.
    ///
    /// Overflow is not possible, since $|f(x,u,p,m)| < u/4 < 2^{62}$. Underflow requires a tiny $x$
    /// together with a small $u$, since the result is about $xu/(2\pi)$ there.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_with_period_prec`]
    /// instead. If you know that your target precision is the precision of the input, consider
    /// using [`Float::atan_with_period_round`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `self.significant_bits()`: the arctangent is taken at a working precision of about $n$ bits,
    /// which costs the first term, and is then scaled by $u/(2\pi)$, which needs $\pi$ to that many
    /// bits; the second term covers the $m$-bit input. The magnitude of the input does not drive
    /// the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision (which is the case unless $x$ is zero or NaN, $u$ is zero, or $|x|$
    /// is 1 or infinite and $p$ is large enough to hold a quarter or an eighth of a turn).
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::{One, Two};
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::ONE.atan_with_period_prec_round(360, 10, Exact);
    /// assert_eq!(t.to_string(), "45.000");
    /// assert_eq!(o, Equal);
    ///
    /// let (t, o) = Float::TWO.atan_with_period_prec_round(360, 10, Floor);
    /// assert_eq!(t.to_string(), "63.375");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = Float::TWO.atan_with_period_prec_round(360, 10, Ceiling);
    /// assert_eq!(t.to_string(), "63.438");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_with_period_prec_round(
        self,
        u: u64,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        self.atan_with_period_prec_round_ref(u, prec, rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the specified precision and with the specified rounding mode. The
    /// [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent. Although
    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
    /// returns `Equal`.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::{One, Two};
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = (&Float::ONE).atan_with_period_prec_round_ref(360, 10, Exact);
    /// assert_eq!(t.to_string(), "45.000");
    /// assert_eq!(o, Equal);
    ///
    /// let (t, o) = (&Float::TWO).atan_with_period_prec_round_ref(360, 10, Floor);
    /// assert_eq!(t.to_string(), "63.375");
    /// assert_eq!(o, Less);
    /// ```
    pub fn atan_with_period_prec_round_ref(
        &self,
        u: u64,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        assert_ne!(prec, 0);
        match &self.0 {
            NaN => (Self::NAN, Equal),
            // atanu(+infinity, u) = u/4, atanu(-infinity, u) = -u/4, a quarter turn
            Infinity { sign } => {
                if u == 0 {
                    (
                        if *sign {
                            Self::ZERO
                        } else {
                            Self::NEGATIVE_ZERO
                        },
                        Equal,
                    )
                } else {
                    scaled_unsigned(u, 2, *sign, prec, rm)
                }
            }
            // atanu(±0.0, u) = ±0.0, even for u = 0
            Zero { .. } => (self.clone(), Equal),
            Finite { .. } => {
                if u == 0 {
                    // atanu(x, 0) = 0 with the sign of x, which agrees with the x = 0 case and
                    // keeps the function odd
                    (
                        if *self < 0u32 {
                            Self::NEGATIVE_ZERO
                        } else {
                            Self::ZERO
                        },
                        Equal,
                    )
                } else {
                    atan_with_period_prec_round_normal_ref(self, u, prec, rm)
                }
            }
        }
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
    /// by value. An [`Ordering`] is also returned, indicating whether the rounded arctangent is
    /// less than, equal to, or greater than the exact arctangent. Although `NaN`s are not
    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way with `Nearest`.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_with_period_prec_round`] instead.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::{One, Two};
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::ONE.atan_with_period_prec(360, 10);
    /// assert_eq!(t.to_string(), "45.000");
    /// assert_eq!(o, Equal);
    ///
    /// let (t, o) = Float::TWO.atan_with_period_prec(360, 10);
    /// assert_eq!(t.to_string(), "63.438");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_with_period_prec(self, u: u64, prec: u64) -> (Self, Ordering) {
        self.atan_with_period_prec_round(u, prec, Nearest)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
    /// by reference. An [`Ordering`] is also returned, indicating whether the rounded arctangent is
    /// less than, equal to, or greater than the exact arctangent. Although `NaN`s are not
    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// See [`Float::atan_with_period_prec`] and [`Float::atan_with_period_prec_round`]; this
    /// function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::Two;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = (&Float::TWO).atan_with_period_prec_ref(360, 10);
    /// assert_eq!(t.to_string(), "63.438");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_with_period_prec_ref(&self, u: u64, prec: u64) -> (Self, Ordering) {
        self.atan_with_period_prec_round_ref(u, prec, Nearest)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the precision of the input and with the specified rounding mode. The
    /// [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent. Although
    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
    /// returns `Equal`.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
    /// the precision of the input.
    ///
    /// If you want to specify an output precision, consider using
    /// [`Float::atan_with_period_prec_round`] instead.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
    /// the input.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// // the output takes the input's precision, here 10 bits
    /// let (t, o) = Float::from_unsigned_prec(2u32, 10)
    ///     .0
    ///     .atan_with_period_round(360, Floor);
    /// assert_eq!(t.to_string(), "63.375");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_with_period_round(self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
        let prec = self.significant_bits();
        self.atan_with_period_prec_round(u, prec, rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn,
    /// rounding the result to the precision of the input and with the specified rounding mode. The
    /// [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent. Although
    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
    /// returns `Equal`.
    ///
    /// See [`Float::atan_with_period_round`] and [`Float::atan_with_period_prec_round`]; this
    /// function behaves the same way.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
    /// the input.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// // the output takes the input's precision, here 10 bits
    /// let x = Float::from_unsigned_prec(2u32, 10).0;
    /// let (t, o) = (&x).atan_with_period_round_ref(360, Floor);
    /// assert_eq!(t.to_string(), "63.375");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_with_period_round_ref(&self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_with_period_prec_round_ref(u, self.significant_bits(), rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn
    /// (so that `u = 360` is degrees), rounding the result to the precision of the input and to the
    /// nearest [`Float`]. The [`Float`] is taken by value.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
    /// the precision of the input and `rm` equal to `Nearest`.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_with_period_round`] instead. If you want to specify an output precision,
    /// consider using [`Float::atan_with_period_prec`]. If you want both of these things, consider
    /// using [`Float::atan_with_period_prec_round`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let t = Float::from_unsigned_prec(2u32, 10).0.atan_with_period(360);
    /// assert_eq!(t.to_string(), "63.438");
    /// ```
    #[inline]
    pub fn atan_with_period(self, u: u64) -> Self {
        let prec = self.significant_bits();
        self.atan_with_period_prec(u, prec).0
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn
    /// (so that `u = 360` is degrees), rounding the result to the precision of the input and to the
    /// nearest [`Float`]. The [`Float`] is taken by reference.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
    /// the precision of the input and `rm` equal to `Nearest`.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_with_period_round_ref`] instead. If you want to specify an output precision,
    /// consider using [`Float::atan_with_period_prec_ref`]. If you want both of these things,
    /// consider using [`Float::atan_with_period_prec_round_ref`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let t = (&Float::from_unsigned_prec(2u32, 10).0).atan_with_period_ref(360);
    /// assert_eq!(t.to_string(), "63.438");
    /// ```
    #[inline]
    pub fn atan_with_period_ref(&self, u: u64) -> Self {
        self.atan_with_period_prec_ref(u, self.significant_bits()).0
    }

    /// Replaces a [`Float`] measured in $u$ths of a turn with its arctangent, rounding the result
    /// to the specified precision and with the specified rounding mode. An [`Ordering`] is
    /// returned, indicating whether the rounded arctangent is less than, equal to, or greater than
    /// the exact arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this
    /// function sets a `NaN` it also returns `Equal`.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::Two;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::TWO;
    /// let o = x.atan_with_period_prec_round_assign(360, 10, Floor);
    /// assert_eq!(x.to_string(), "63.375");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_with_period_prec_round_assign(
        &mut self,
        u: u64,
        prec: u64,
        rm: RoundingMode,
    ) -> Ordering {
        let (t, o) = self.atan_with_period_prec_round_ref(u, prec, rm);
        *self = t;
        o
    }

    /// Replaces a [`Float`] measured in $u$ths of a turn with its arctangent, rounding the result
    /// to the nearest value of the specified precision. An [`Ordering`] is returned, indicating
    /// whether the rounded arctangent is less than, equal to, or greater than the exact arctangent.
    /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets a `NaN` it
    /// also returns `Equal`.
    ///
    /// See [`Float::atan_with_period_prec`] and [`Float::atan_with_period_prec_round`]; this
    /// function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::Two;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::TWO;
    /// let o = x.atan_with_period_prec_assign(360, 10);
    /// assert_eq!(x.to_string(), "63.438");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_with_period_prec_assign(&mut self, u: u64, prec: u64) -> Ordering {
        self.atan_with_period_prec_round_assign(u, prec, Nearest)
    }

    /// Replaces a [`Float`] measured in $u$ths of a turn with its arctangent, rounding the result
    /// to the precision of the input and with the specified rounding mode. An [`Ordering`] is
    /// returned, indicating whether the rounded arctangent is less than, equal to, or greater than
    /// the exact arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this
    /// function sets a `NaN` it also returns `Equal`.
    ///
    /// See [`Float::atan_with_period_round`] and [`Float::atan_with_period_prec_round`]; this
    /// function behaves the same way.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
    /// the input.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// // the output takes the input's precision, here 10 bits
    /// let mut x = Float::from_unsigned_prec(2u32, 10).0;
    /// let o = x.atan_with_period_round_assign(360, Floor);
    /// assert_eq!(x.to_string(), "63.375");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_with_period_round_assign(&mut self, u: u64, rm: RoundingMode) -> Ordering {
        let prec = self.significant_bits();
        self.atan_with_period_prec_round_assign(u, prec, rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Float`] measured in $u$ths of a turn
    /// (so that `u = 360` is degrees), rounding the result to the precision of the input and to the
    /// nearest [`Float`]. The [`Float`] is replaced by the result.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// See [`Float::atan_with_period_prec_round`] for the error bounds, the special and closed-form
    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
    /// the precision of the input and `rm` equal to `Nearest`.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_with_period_round_assign`] instead. If you want to specify an output
    /// precision, consider using [`Float::atan_with_period_prec_assign`]. If you want both of these
    /// things, consider using [`Float::atan_with_period_prec_round_assign`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let mut x = Float::from_unsigned_prec(2u32, 10).0;
    /// x.atan_with_period_assign(360);
    /// assert_eq!(x.to_string(), "63.438");
    /// ```
    #[inline]
    pub fn atan_with_period_assign(&mut self, u: u64) {
        let prec = self.significant_bits();
        self.atan_with_period_prec_assign(u, prec);
    }
}

impl Float {
    /// Computes $\arctan x$, the arctangent of a [`Rational`], rounding the result to the specified
    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
    /// [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,p,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p+1}$.
    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$.
    ///
    /// These bounds do not apply when the result underflows; see below.
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,p,m)=0$.
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
    ///   returned instead.
    ///
    /// Underflow requires an input of magnitude about $2^{-2^{30}}$ or less: since $|\arctan x| <
    /// |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_rational_prec`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the input is rounded once to a working precision of about $n$ bits
    /// and its [`Float`] arctangent taken there, which costs the first term; the rounding of the
    /// $m$-bit input is the second. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision (which is the case for every nonzero input).
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::atan_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Floor);
    /// assert_eq!(c.to_string(), "0.531");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::atan_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Ceiling);
    /// assert_eq!(c.to_string(), "0.562");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) = Float::atan_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Floor);
    /// assert_eq!(c.to_string(), "0.54041862");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::atan_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Ceiling);
    /// assert_eq!(c.to_string(), "0.54041958");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_rational_prec_round(x: Rational, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        Self::atan_rational_prec_round_ref(&x, prec, rm)
    }

    /// Computes $\arctan x$, the arctangent of a [`Rational`], rounding the result to the specified
    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
    /// [`Rational`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
    /// rounded arctangent is less than, equal to, or greater than the exact arctangent.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,p,m) = \arctan x+\varepsilon.
    /// $$
    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p+1}$.
    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$.
    ///
    /// These bounds do not apply when the result underflows.
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,p,m)=0$.
    ///
    /// See the [`Float::atan_rational_prec_round`] documentation for information on overflow and
    /// underflow.
    ///
    /// If you know you'll be using `Nearest`, consider using [`Float::atan_rational_prec_ref`]
    /// instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the input is rounded once to a working precision of about $n$ bits
    /// and its [`Float`] arctangent taken there, which costs the first term; the rounding of the
    /// $m$-bit input is the second. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision (which is the case for every nonzero input).
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) =
    ///     Float::atan_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 5, Floor);
    /// assert_eq!(c.to_string(), "0.531");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) =
    ///     Float::atan_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 5, Ceiling);
    /// assert_eq!(c.to_string(), "0.562");
    /// assert_eq!(o, Greater);
    ///
    /// let (c, o) =
    ///     Float::atan_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 20, Floor);
    /// assert_eq!(c.to_string(), "0.54041862");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) =
    ///     Float::atan_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 20, Ceiling);
    /// assert_eq!(c.to_string(), "0.54041958");
    /// assert_eq!(o, Greater);
    /// ```
    pub fn atan_rational_prec_round_ref(
        x: &Rational,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        assert_ne!(prec, 0);
        if *x == 0u32 {
            // atan(0) = 0, exactly
            return (Self::ZERO, Equal);
        }
        atan_rational_helper(x, prec, rm)
    }

    /// Computes $\arctan x$, the arctangent of a [`Rational`], rounding the result to the nearest
    /// value of the specified precision and returning the result as a [`Float`]. The [`Rational`]
    /// is taken by value. An [`Ordering`] is also returned, indicating whether the rounded
    /// arctangent is less than, equal to, or greater than the exact arctangent.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// f(x,p) = \arctan x+\varepsilon,
    /// $$
    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$ (unless the result
    /// underflows; see below).
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,p)=0$.
    ///
    /// Overflow and underflow:
    /// - Since $|\arctan x| < \pi/2$, the result never overflows.
    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
    ///
    /// Underflow requires an input of magnitude about $2^{-2^{30}}$ or less: since $|\arctan x| <
    /// |x|$ for nonzero $x$, no other input can reach it.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_rational_prec_round`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the input is rounded once to a working precision of about $n$ bits
    /// and its [`Float`] arctangent taken there, which costs the first term; the rounding of the
    /// $m$-bit input is the second. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::atan_rational_prec(Rational::from_unsigneds(3u8, 5), 5);
    /// assert_eq!(c.to_string(), "0.531");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::atan_rational_prec(Rational::from_unsigneds(3u8, 5), 20);
    /// assert_eq!(c.to_string(), "0.54041958");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
        Self::atan_rational_prec_round_ref(&x, prec, Nearest)
    }

    /// Computes $\arctan x$, the arctangent of a [`Rational`], rounding the result to the nearest
    /// value of the specified precision and returning the result as a [`Float`]. The [`Rational`]
    /// is taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
    /// arctangent is less than, equal to, or greater than the exact arctangent.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// f(x,p) = \arctan x+\varepsilon,
    /// $$
    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$ (unless the result
    /// underflows).
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,p)=0$.
    ///
    /// See the [`Float::atan_rational_prec`] documentation for information on overflow and
    /// underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_rational_prec_round_ref`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the input is rounded once to a working precision of about $n$ bits
    /// and its [`Float`] arctangent taken there, which costs the first term; the rounding of the
    /// $m$-bit input is the second. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (c, o) = Float::atan_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 5);
    /// assert_eq!(c.to_string(), "0.531");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::atan_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 20);
    /// assert_eq!(c.to_string(), "0.54041958");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
        Self::atan_rational_prec_round_ref(x, prec, Nearest)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Rational`] measured in $u$ths of a
    /// turn, rounding the result to the specified precision and with the specified rounding mode
    /// and returning the result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`]
    /// is also returned, indicating whether the rounded arctangent is less than, equal to, or
    /// greater than the exact arctangent.
    ///
    /// See [`RoundingMode`] for a description of the possible rounding modes.
    ///
    /// $$
    /// f(x,u,p,m) = \arctan(x)u/(2\pi)+\varepsilon.
    /// $$
    /// - If $x$ is zero, $u = 0$, or $|x|$ is 1, $\varepsilon$ may be ignored or assumed to be 0.
    /// - Otherwise, if $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
    ///   |\arctan(x)u/(2\pi)|\rfloor-p+1}$.
    /// - Otherwise, if $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
    ///   |\arctan(x)u/(2\pi)|\rfloor-p}$.
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,u,p,m)=0.0$
    /// - $f(x,0,p,m)=\pm0.0$, with the sign of $x$, so that the function stays odd
    /// - $f(\pm1,u,p,m)=\pm u/8$, an eighth of a turn
    ///
    /// These are the only exact cases, and the eighth turn is exact only when $p$ is large enough
    /// to hold it.
    ///
    /// Underflow:
    /// - If $0<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
    /// - If $0<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - If $0<f(x,u,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
    /// - If $2^{-2^{30}-1}<f(x,u,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
    ///   instead.
    /// - The negative cases mirror these, since the function is odd.
    ///
    /// Overflow is not possible, since $|f(x,u,p,m)| < u/4 < 2^{62}$. Underflow requires a tiny $x$
    /// together with a small $u$, since the result is about $xu/(2\pi)$ there. Unlike the [`Float`]
    /// case, $x$ itself may be far below the bottom of the exponent range.
    ///
    /// If you know you'll be using `Nearest`, consider using
    /// [`Float::atan_with_period_rational_prec`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the arctangent is taken at a working precision of about $n$ bits and
    /// scaled by $u/(2\pi)$, which needs $\pi$ to that many bits, and both cost the first term; the
    /// second covers the $m$-bit input. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision (which is the case unless $x$ is zero, $u$ is zero, or $|x|$ is 1
    /// and $p$ is large enough to hold an eighth of a turn).
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::One;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::atan_with_period_rational_prec_round(Rational::ONE, 360, 10, Exact);
    /// assert_eq!(t.to_string(), "45.000");
    /// assert_eq!(o, Equal);
    ///
    /// let (t, o) = Float::atan_with_period_rational_prec_round(
    ///     Rational::from_unsigneds(3u8, 5),
    ///     360,
    ///     10,
    ///     Floor,
    /// );
    /// assert_eq!(t.to_string(), "30.938");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = Float::atan_with_period_rational_prec_round(
    ///     Rational::from_unsigneds(3u8, 5),
    ///     360,
    ///     10,
    ///     Ceiling,
    /// );
    /// assert_eq!(t.to_string(), "30.969");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_with_period_rational_prec_round(
        x: Rational,
        u: u64,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_round_ref(&x, u, prec, rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Rational`] measured in $u$ths of a
    /// turn, rounding the result to the specified precision and with the specified rounding mode
    /// and returning the result as a [`Float`]. The [`Rational`] is taken by reference. An
    /// [`Ordering`] is also returned, indicating whether the rounded arctangent is less than, equal
    /// to, or greater than the exact arctangent.
    ///
    /// See [`Float::atan_with_period_rational_prec_round`] for the error bounds, the special cases,
    /// underflow, and the complexity; this function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::One;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) =
    ///     Float::atan_with_period_rational_prec_round_ref(&Rational::ONE, 360, 10, Exact);
    /// assert_eq!(t.to_string(), "45.000");
    /// assert_eq!(o, Equal);
    ///
    /// let (t, o) = Float::atan_with_period_rational_prec_round_ref(
    ///     &Rational::from_unsigneds(3u8, 5),
    ///     360,
    ///     10,
    ///     Floor,
    /// );
    /// assert_eq!(t.to_string(), "30.938");
    /// assert_eq!(o, Less);
    /// ```
    pub fn atan_with_period_rational_prec_round_ref(
        x: &Rational,
        u: u64,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        assert_ne!(prec, 0);
        // atanu(0, u) = 0, and atanu(x, 0) = 0 with the sign of x, so that the function stays odd;
        // a `Rational` zero has no sign, so the first case gives a positive zero
        if *x == 0u32 || u == 0 {
            return (
                if *x < 0u32 {
                    Self::NEGATIVE_ZERO
                } else {
                    Self::ZERO
                },
                Equal,
            );
        }
        atan_with_period_rational_helper(x, u, prec, rm)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Rational`] measured in $u$ths of a
    /// turn, rounding the result to the nearest value of the specified precision and returning the
    /// result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the specified precision, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// $$
    /// f(x,u,p) = \arctan(x)u/(2\pi)+\varepsilon,
    /// $$
    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |\arctan(x)u/(2\pi)|\rfloor-p}$ (unless the
    /// result underflows, or is one of the exact cases below).
    ///
    /// The output has precision `prec`.
    ///
    /// Special cases:
    /// - $f(0,u,p)=0.0$
    /// - $f(x,0,p)=\pm0.0$, with the sign of $x$, so that the function stays odd
    /// - $f(\pm1,u,p)=\pm u/8$, an eighth of a turn
    ///
    /// See the [`Float::atan_with_period_rational_prec_round`] documentation for information on
    /// overflow and underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_with_period_rational_prec_round`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
    ///
    /// $M(n, m) = O((n+m) \log (n+m))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
    /// `x.significant_bits()`: the arctangent is taken at a working precision of about $n$ bits and
    /// scaled by $u/(2\pi)$, which needs $\pi$ to that many bits, and both cost the first term; the
    /// second covers the $m$-bit input. The magnitude of the input does not drive the cost.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) =
    ///     Float::atan_with_period_rational_prec(Rational::from_unsigneds(3u8, 5), 360, 10);
    /// assert_eq!(t.to_string(), "30.969");
    /// assert_eq!(o, Greater);
    ///
    /// let (t, o) =
    ///     Float::atan_with_period_rational_prec(Rational::from_unsigneds(3u8, 5), 360, 20);
    /// assert_eq!(t.to_string(), "30.963745");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_with_period_rational_prec(x: Rational, u: u64, prec: u64) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_round_ref(&x, u, prec, Nearest)
    }

    /// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Rational`] measured in $u$ths of a
    /// turn, rounding the result to the nearest value of the specified precision and returning the
    /// result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also
    /// returned, indicating whether the rounded arctangent is less than, equal to, or greater than
    /// the exact arctangent.
    ///
    /// See [`Float::atan_with_period_rational_prec`] for the error bounds, the special cases,
    /// underflow, and the complexity; this function behaves the same way.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) =
    ///     Float::atan_with_period_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 360, 10);
    /// assert_eq!(t.to_string(), "30.969");
    /// assert_eq!(o, Greater);
    ///
    /// let (t, o) =
    ///     Float::atan_with_period_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 360, 20);
    /// assert_eq!(t.to_string(), "30.963745");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_with_period_rational_prec_ref(x: &Rational, u: u64, prec: u64) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_round_ref(x, u, prec, Nearest)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
    /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded arctangent
    /// is less than, equal to, or greater than the exact arctangent. Although `NaN`s are not
    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_prec_round`]
    /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
    /// infinite input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every
    /// precision, since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those
    /// are the only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::One;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_prec_round(10, Floor);
    /// assert_eq!(t.to_string(), "0.031677");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_prec_round(10, Ceiling);
    /// assert_eq!(t.to_string(), "0.031738");
    /// assert_eq!(o, Greater);
    ///
    /// // an input of 1 gives an eighth of a turn, which is a quarter of a half-turn, exactly
    /// let (t, o) = Float::ONE.atan_pi_prec_round(10, Exact);
    /// assert_eq!(t.to_string(), "0.25000");
    /// assert_eq!(o, Equal);
    /// ```
    #[inline]
    pub fn atan_pi_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_with_period_prec_round(2, prec, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
    /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
    /// arctangent is less than, equal to, or greater than the exact arctangent. Although `NaN`s are
    /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
    /// `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see
    /// [`Float::atan_with_period_prec_round_ref`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An infinite input gives $\pm1/2$ and an input
    /// of $\pm1$ gives $\pm1/4$, both exact at every precision, since a half and a quarter need
    /// only one bit; a zero input gives $\pm0.0$. Those are the only exact cases. Overflow is not
    /// possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::One;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_prec_round_ref(10, Floor);
    /// assert_eq!(t.to_string(), "0.031677");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_prec_round_ref(10, Ceiling);
    /// assert_eq!(t.to_string(), "0.031738");
    /// assert_eq!(o, Greater);
    ///
    /// // an input of 1 gives an eighth of a turn, which is a quarter of a half-turn, exactly
    /// let (t, o) = (&Float::ONE).atan_pi_prec_round_ref(10, Exact);
    /// assert_eq!(t.to_string(), "0.25000");
    /// assert_eq!(o, Equal);
    /// ```
    #[inline]
    pub fn atan_pi_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_with_period_prec_round_ref(2, prec, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the nearest value of the specified precision. The [`Float`] is taken by value.
    /// An [`Ordering`] is also returned, indicating whether the rounded arctangent is less than,
    /// equal to, or greater than the exact arctangent. Although `NaN`s are not comparable to any
    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_prec`] for the
    /// error bounds, the special cases, underflow, and the complexity, with $u = 2$. An infinite
    /// input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every precision,
    /// since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those are the
    /// only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_prec(10);
    /// assert_eq!(t.to_string(), "0.031738");
    /// assert_eq!(o, Greater);
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_prec(53);
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_pi_prec(self, prec: u64) -> (Self, Ordering) {
        self.atan_with_period_prec(2, prec)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the nearest value of the specified precision. The [`Float`] is taken by
    /// reference. An [`Ordering`] is also returned, indicating whether the rounded arctangent is
    /// less than, equal to, or greater than the exact arctangent. Although `NaN`s are not
    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_prec_ref`] for
    /// the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
    /// infinite input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every
    /// precision, since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those
    /// are the only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_prec_ref(10);
    /// assert_eq!(t.to_string(), "0.031738");
    /// assert_eq!(o, Greater);
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_prec_ref(53);
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_pi_prec_ref(&self, prec: u64) -> (Self, Ordering) {
        self.atan_with_period_prec_ref(2, prec)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result with the specified rounding mode. The precision of the output is the precision of
    /// the input. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating
    /// whether the rounded arctangent is less than, equal to, or greater than the exact arctangent.
    /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
    /// it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_round`] for the
    /// error bounds, the special cases, underflow, and the complexity, with $u = 2$. An infinite
    /// input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every precision,
    /// since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those are the
    /// only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
    /// precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_round(Floor);
    /// assert_eq!(t.to_string(), "0.031725517430553560");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = Float::from(0.1f64).atan_pi_round(Nearest);
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_pi_round(self, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_with_period_round(2, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result with the specified rounding mode. The precision of the output is the precision of
    /// the input. The [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating
    /// whether the rounded arctangent is less than, equal to, or greater than the exact arctangent.
    /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
    /// it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_round_ref`] for
    /// the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
    /// infinite input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every
    /// precision, since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those
    /// are the only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
    /// precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_round_ref(Floor);
    /// assert_eq!(t.to_string(), "0.031725517430553560");
    /// assert_eq!(o, Less);
    ///
    /// let (t, o) = (Float::from(0.1f64)).atan_pi_round_ref(Nearest);
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_pi_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
        self.atan_with_period_round_ref(2, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the precision of the input and to the nearest [`Float`]. The [`Float`] is
    /// taken by value.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period`] for the error
    /// bounds, the special cases, underflow, and the complexity, with $u = 2$. An infinite input
    /// gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every precision, since a
    /// half and a quarter need only one bit; a zero input gives $\pm0.0$. Those are the only exact
    /// cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_pi_round`] instead. If you want to specify an output precision, consider using
    /// [`Float::atan_pi_prec`]. If you want both of these things, consider using
    /// [`Float::atan_pi_prec_round`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let t = Float::from(0.1f64).atan_pi();
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    ///
    /// assert_eq!(Float::from(0.5f64).atan_pi().to_string(), "0.12");
    /// ```
    #[inline]
    pub fn atan_pi(self) -> Self {
        let prec = self.significant_bits();
        self.atan_pi_prec(prec).0
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the precision of the input and to the nearest [`Float`]. The [`Float`] is
    /// taken by reference.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period`] for the error
    /// bounds, the special cases, underflow, and the complexity, with $u = 2$. An infinite input
    /// gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every precision, since a
    /// half and a quarter need only one bit; a zero input gives $\pm0.0$. Those are the only exact
    /// cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_pi_round_ref`] instead. If you want to specify an output precision, consider
    /// using [`Float::atan_pi_prec_ref`]. If you want both of these things, consider using
    /// [`Float::atan_pi_prec_round_ref`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let t = (&Float::from(0.1f64)).atan_pi_ref();
    /// assert_eq!(t.to_string(), "0.031725517430553574");
    /// ```
    #[inline]
    pub fn atan_pi_ref(&self) -> Self {
        self.atan_pi_prec_ref(self.significant_bits()).0
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
    /// replaced by the result, and an [`Ordering`] is returned, indicating whether the rounded
    /// arctangent is less than, equal to, or greater than the exact arctangent. Although `NaN`s are
    /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
    /// `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see
    /// [`Float::atan_with_period_prec_round_assign`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An infinite input gives $\pm1/2$ and an input
    /// of $\pm1$ gives $\pm1/4$, both exact at every precision, since a half and a quarter need
    /// only one bit; a zero input gives $\pm0.0$. Those are the only exact cases. Overflow is not
    /// possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from(0.1f64);
    /// assert_eq!(x.atan_pi_prec_round_assign(10, Floor), Less);
    /// assert_eq!(x.to_string(), "0.031677");
    ///
    /// let mut x = Float::from(0.1f64);
    /// assert_eq!(x.atan_pi_prec_round_assign(10, Ceiling), Greater);
    /// assert_eq!(x.to_string(), "0.031738");
    /// ```
    #[inline]
    pub fn atan_pi_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
        self.atan_with_period_prec_round_assign(2, prec, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the nearest value of the specified precision. The [`Float`] is replaced by the
    /// result, and an [`Ordering`] is returned, indicating whether the rounded arctangent is less
    /// than, equal to, or greater than the exact arctangent. Although `NaN`s are not comparable to
    /// any [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_prec_assign`]
    /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
    /// infinite input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every
    /// precision, since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those
    /// are the only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from(0.1f64);
    /// assert_eq!(x.atan_pi_prec_assign(10), Greater);
    /// assert_eq!(x.to_string(), "0.031738");
    /// ```
    #[inline]
    pub fn atan_pi_prec_assign(&mut self, prec: u64) -> Ordering {
        self.atan_with_period_prec_assign(2, prec)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result with the specified rounding mode. The precision of the output is the precision of
    /// the input. The [`Float`] is replaced by the result, and an [`Ordering`] is returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
    /// a `NaN` it also returns `Equal`.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period_round_assign`]
    /// for the error bounds, the special cases, underflow, and the complexity, with $u = 2$. An
    /// infinite input gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every
    /// precision, since a half and a quarter need only one bit; a zero input gives $\pm0.0$. Those
    /// are the only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
    /// precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// let mut x = Float::from(0.1f64);
    /// assert_eq!(x.atan_pi_round_assign(Floor), Less);
    /// assert_eq!(x.to_string(), "0.031725517430553560");
    /// ```
    #[inline]
    pub fn atan_pi_round_assign(&mut self, rm: RoundingMode) -> Ordering {
        self.atan_with_period_round_assign(2, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Float`] measured in half-turns, rounding
    /// the result to the precision of the input and to the nearest [`Float`]. The [`Float`] is
    /// replaced by the result.
    ///
    /// If the arctangent is equidistant from two [`Float`]s with the precision of the input, the
    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
    /// description of the `Nearest` rounding mode.
    ///
    /// This is `atan_with_period` with a period of 2: see [`Float::atan_with_period`] for the error
    /// bounds, the special cases, underflow, and the complexity, with $u = 2$. An infinite input
    /// gives $\pm1/2$ and an input of $\pm1$ gives $\pm1/4$, both exact at every precision, since a
    /// half and a quarter need only one bit; a zero input gives $\pm0.0$. Those are the only exact
    /// cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_pi_round_assign`] instead. If you want to specify an output precision,
    /// consider using [`Float::atan_pi_prec_assign`]. If you want both of these things, consider
    /// using [`Float::atan_pi_prec_round_assign`].
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    ///
    /// let mut x = Float::from(0.1f64);
    /// x.atan_pi_assign();
    /// assert_eq!(x.to_string(), "0.031725517430553574");
    /// ```
    #[inline]
    pub fn atan_pi_assign(&mut self) {
        let prec = self.significant_bits();
        self.atan_pi_prec_assign(prec);
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Rational`] measured in half-turns, rounding
    /// the result to the specified precision and with the specified rounding mode and returning the
    /// result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent.
    ///
    /// This is `atan_with_period_rational` with a period of 2: see
    /// [`Float::atan_with_period_rational_prec_round`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An input of $\pm1$ gives $\pm1/4$ and a zero
    /// input gives $0.0$, both exact at every precision; those are the only exact cases. Overflow
    /// is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::basic::traits::One;
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) =
    ///     Float::atan_pi_rational_prec_round(Rational::from_unsigneds(1u8, 7), 10, Floor);
    /// assert_eq!(t.to_string(), "0.045166");
    /// assert_eq!(o, Less);
    ///
    /// // an input of 1 gives a quarter of a half-turn, exactly
    /// let (t, o) = Float::atan_pi_rational_prec_round(Rational::ONE, 10, Exact);
    /// assert_eq!(t.to_string(), "0.25000");
    /// assert_eq!(o, Equal);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_pi_rational_prec_round(
        x: Rational,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_round_ref(&x, 2, prec, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Rational`] measured in half-turns, rounding
    /// the result to the specified precision and with the specified rounding mode and returning the
    /// result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also
    /// returned, indicating whether the rounded arctangent is less than, equal to, or greater than
    /// the exact arctangent.
    ///
    /// This is `atan_with_period_rational` with a period of 2: see
    /// [`Float::atan_with_period_rational_prec_round_ref`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An input of $\pm1$ gives $\pm1/4$ and a zero
    /// input gives $0.0$, both exact at every precision; those are the only exact cases. Overflow
    /// is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
    /// with the given precision.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) =
    ///     Float::atan_pi_rational_prec_round_ref(&Rational::from_unsigneds(1u8, 7), 10, Ceiling);
    /// assert_eq!(t.to_string(), "0.045227");
    /// assert_eq!(o, Greater);
    /// ```
    #[inline]
    pub fn atan_pi_rational_prec_round_ref(
        x: &Rational,
        prec: u64,
        rm: RoundingMode,
    ) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_round_ref(x, 2, prec, rm)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Rational`] measured in half-turns, rounding
    /// the result to the nearest value of the specified precision and returning the result as a
    /// [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating
    /// whether the rounded arctangent is less than, equal to, or greater than the exact arctangent.
    ///
    /// This is `atan_with_period_rational` with a period of 2: see
    /// [`Float::atan_with_period_rational_prec`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An input of $\pm1$ gives $\pm1/4$ and a zero
    /// input gives $0.0$, both exact at every precision; those are the only exact cases. Overflow
    /// is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::atan_pi_rational_prec(Rational::from_unsigneds(1u8, 7), 53);
    /// assert_eq!(t.to_string(), "0.045167235300866547");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    pub fn atan_pi_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_ref(&x, 2, prec)
    }

    /// Computes $\arctan(x)/\pi$, the arctangent of a [`Rational`] measured in half-turns, rounding
    /// the result to the nearest value of the specified precision and returning the result as a
    /// [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also returned,
    /// indicating whether the rounded arctangent is less than, equal to, or greater than the exact
    /// arctangent.
    ///
    /// This is `atan_with_period_rational` with a period of 2: see
    /// [`Float::atan_with_period_rational_prec_ref`] for the error bounds, the special cases,
    /// underflow, and the complexity, with $u = 2$. An input of $\pm1$ gives $\pm1/4$ and a zero
    /// input gives $0.0$, both exact at every precision; those are the only exact cases. Overflow
    /// is not possible, since $|\arctan(x)/\pi| < 1/2$.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use malachite_q::Rational;
    /// use std::cmp::Ordering::*;
    ///
    /// let (t, o) = Float::atan_pi_rational_prec_ref(&Rational::from_unsigneds(1u8, 7), 53);
    /// assert_eq!(t.to_string(), "0.045167235300866547");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn atan_pi_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
        Self::atan_with_period_rational_prec_ref(x, 2, prec)
    }
}

impl Atan for Float {
    type Output = Self;

    /// Computes $\arctan x$, the arctangent of a [`Float`], taking it by value.
    ///
    /// If the output has a precision, it is the precision of the input. If the arctangent is
    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
    /// rounding mode.
    ///
    /// $$
    /// f(x) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$, where
    ///   $p$ is the precision of the input.
    ///
    /// Special cases:
    /// - $f(\text{NaN})=\text{NaN}$
    /// - $f(\pm\infty)=\pm\pi/2$, rounded
    /// - $f(\pm0.0)=\pm0.0$
    ///
    /// See the [`Float::atan_round`] documentation for information on overflow and underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_round`] instead. If you want to specify the output precision, consider using
    /// [`Float::atan_prec`]. If you want both of these things, consider using
    /// [`Float::atan_prec_round`].
    ///
    /// # Worst-case complexity
    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
    ///
    /// $M(n, e) = O((n+e) \log (n+e))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
    /// e$ bits. Unlike most functions, `atan` therefore gets slower as the magnitude of its input
    /// grows, not just as the precision does.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::arithmetic::traits::Atan;
    /// use malachite_base::num::basic::traits::*;
    /// use malachite_float::Float;
    ///
    /// assert!(Float::NAN.atan().is_nan());
    /// // an infinity has a precision of 1, so pi/2 rounds to 2
    /// assert_eq!(Float::INFINITY.atan().to_string(), "2.0");
    /// assert_eq!(Float::NEGATIVE_INFINITY.atan().to_string(), "-2.0");
    /// assert_eq!(Float::ZERO.atan().to_string(), "0.0");
    /// assert_eq!(Float::NEGATIVE_ZERO.atan().to_string(), "-0.0");
    /// assert_eq!(
    ///     Float::from_unsigned_prec(1u32, 100).0.atan().to_string(),
    ///     "0.78539816339744830961566084581983"
    /// );
    /// assert_eq!(
    ///     Float::from_unsigned_prec(100u32, 100).0.atan().to_string(),
    ///     "1.5607966601082313810249815754304"
    /// );
    /// ```
    #[inline]
    fn atan(self) -> Self {
        let prec = self.significant_bits();
        self.atan_prec_round(prec, Nearest).0
    }
}

impl Atan for &Float {
    type Output = Float;

    /// Computes $\arctan x$, the arctangent of a [`Float`], taking it by reference.
    ///
    /// If the output has a precision, it is the precision of the input. If the arctangent is
    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
    /// rounding mode.
    ///
    /// $$
    /// f(x) = \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$, where
    ///   $p$ is the precision of the input.
    ///
    /// Special cases:
    /// - $f(\text{NaN})=\text{NaN}$
    /// - $f(\pm\infty)=\pm\pi/2$, rounded
    /// - $f(\pm0.0)=\pm0.0$
    ///
    /// See the [`Float::atan_round`] documentation for information on overflow and underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_round_ref`] instead. If you want to specify the output precision, consider
    /// using [`Float::atan_prec_ref`]. If you want both of these things, consider using
    /// [`Float::atan_prec_round_ref`].
    ///
    /// # Worst-case complexity
    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
    ///
    /// $M(n, e) = O((n+e) \log (n+e))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
    /// e$ bits. Unlike most functions, `atan` therefore gets slower as the magnitude of its input
    /// grows, not just as the precision does.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::arithmetic::traits::Atan;
    /// use malachite_base::num::basic::traits::*;
    /// use malachite_float::Float;
    ///
    /// assert!(Float::NAN.atan().is_nan());
    /// // an infinity has a precision of 1, so pi/2 rounds to 2
    /// assert_eq!(Float::INFINITY.atan().to_string(), "2.0");
    /// assert_eq!(Float::NEGATIVE_INFINITY.atan().to_string(), "-2.0");
    /// assert_eq!(Float::ZERO.atan().to_string(), "0.0");
    /// assert_eq!(Float::NEGATIVE_ZERO.atan().to_string(), "-0.0");
    /// assert_eq!(
    ///     (&Float::from_unsigned_prec(1u32, 100).0).atan().to_string(),
    ///     "0.78539816339744830961566084581983"
    /// );
    /// assert_eq!(
    ///     (&Float::from_unsigned_prec(100u32, 100).0)
    ///         .atan()
    ///         .to_string(),
    ///     "1.5607966601082313810249815754304"
    /// );
    /// ```
    #[inline]
    fn atan(self) -> Float {
        self.atan_prec_round_ref(self.significant_bits(), Nearest).0
    }
}

impl AtanAssign for Float {
    /// Computes $\arctan x$, the arctangent of a [`Float`], in place.
    ///
    /// If the output has a precision, it is the precision of the input. If the arctangent is
    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
    /// rounding mode.
    ///
    /// $$
    /// x \gets \arctan x+\varepsilon.
    /// $$
    /// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
    /// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$, where
    ///   $p$ is the precision of the input.
    ///
    /// See the [`Float::atan`] documentation for information on special cases, overflow, and
    /// underflow.
    ///
    /// If you want to use a rounding mode other than `Nearest`, consider using
    /// [`Float::atan_round_assign`] instead. If you want to specify the output precision, consider
    /// using [`Float::atan_prec_assign`]. If you want both of these things, consider using
    /// [`Float::atan_prec_round_assign`].
    ///
    /// # Worst-case complexity
    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
    ///
    /// $M(n, e) = O((n+e) \log (n+e))$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
    /// e$ bits. Unlike most functions, `atan` therefore gets slower as the magnitude of its input
    /// grows, not just as the precision does.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::num::arithmetic::traits::AtanAssign;
    /// use malachite_base::num::basic::traits::*;
    /// use malachite_float::Float;
    ///
    /// let mut x = Float::NAN;
    /// x.atan_assign();
    /// assert!(x.is_nan());
    ///
    /// let mut x = Float::INFINITY;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "2.0");
    ///
    /// let mut x = Float::NEGATIVE_INFINITY;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "-2.0");
    ///
    /// let mut x = Float::ZERO;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "0.0");
    ///
    /// let mut x = Float::NEGATIVE_ZERO;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "-0.0");
    ///
    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "0.78539816339744830961566084581983");
    ///
    /// let mut x = Float::from_unsigned_prec(100u32, 100).0;
    /// x.atan_assign();
    /// assert_eq!(x.to_string(), "1.5607966601082313810249815754304");
    /// ```
    #[inline]
    fn atan_assign(&mut self) {
        let prec = self.significant_bits();
        self.atan_prec_round_assign(prec, Nearest);
    }
}

/// Computes $\arctan x$, the arctangent of a primitive float. Using this function is more accurate
/// than using the default `atan` function or the one provided by `libm`.
///
/// $$
/// f(x) = \arctan x+\varepsilon.
/// $$
/// - If $x$ is NaN, $\varepsilon$ may be ignored or assumed to be 0.
/// - If $x$ is not NaN, then $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$, where $p$ is
///   the precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
///
/// Special cases:
/// - $f(\text{NaN})=\text{NaN}$
/// - $f(\pm\infty)=\pm\pi/2$, rounded
/// - $f(\pm0.0)=\pm0.0$
///
/// Overflow is not possible, since the result lies in $(-\pi/2, \pi/2)$. The result is subnormal
/// only when $x$ is, and then it is $x$ itself, since $|\arctan x - x| < |x|^3/3$.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::NegativeInfinity;
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan;
///
/// assert!(primitive_float_atan(f32::NAN).is_nan());
/// assert_eq!(
///     NiceFloat(primitive_float_atan(f32::INFINITY)),
///     NiceFloat(core::f32::consts::FRAC_PI_2)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan(f32::NEGATIVE_INFINITY)),
///     NiceFloat(-core::f32::consts::FRAC_PI_2)
/// );
/// assert_eq!(NiceFloat(primitive_float_atan(0.0f32)), NiceFloat(0.0));
/// assert_eq!(NiceFloat(primitive_float_atan(-0.0f32)), NiceFloat(-0.0));
/// assert_eq!(
///     NiceFloat(primitive_float_atan(1.0f32)),
///     NiceFloat(core::f32::consts::FRAC_PI_4)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan(1.0f64)),
///     NiceFloat(core::f64::consts::FRAC_PI_4)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan<T: PrimitiveFloat>(x: T) -> T
where
    Float: From<T> + PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    emulate_float_to_float_fn(Float::atan_prec, x)
}

/// Computes $\arctan x$, the arctangent of a [`Rational`], returning the result as a primitive
/// float.
///
/// $$
/// f(x) = \arctan x+\varepsilon,
/// $$
/// where $|\varepsilon| < 2^{\lfloor\log_2 |\arctan x|\rfloor-p}$, and $p$ is the precision of the
/// output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
///
/// Special cases:
/// - $f(0)=0$
///
/// Overflow is not possible, since the result lies in $(-\pi/2, \pi/2)$. The result underflows, to
/// a subnormal or to zero, only when $x$ is tiny, since $|\arctan x| < |x|$ and $\arctan x$ is very
/// close to $x$ there.
///
/// # Worst-case complexity
/// $T(m) = O(m (\log m)^2 \log\log m)$
///
/// $M(m) = O(m \log m)$
///
/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan_rational;
/// use malachite_q::Rational;
///
/// assert_eq!(
///     NiceFloat(primitive_float_atan_rational::<f64>(&Rational::ZERO)),
///     NiceFloat(0.0)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_rational::<f64>(
///         &Rational::from_unsigneds(1u8, 3)
///     )),
///     NiceFloat(0.3217505543966422)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_rational::<f32>(
///         &Rational::from_unsigneds(1u8, 3)
///     )),
///     NiceFloat(0.32175055)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_rational::<f64>(&Rational::from(10000))),
///     NiceFloat(1.5706963267952299)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan_rational<T: PrimitiveFloat>(x: &Rational) -> T
where
    Float: PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    emulate_rational_to_float_fn(Float::atan_rational_prec_ref, x)
}

/// Computes $\arctan(x)u/(2\pi)$, the arctangent of a primitive float measured in $u$ths of a turn
/// (so that `u = 360` gives degrees).
///
/// $$
/// f(x,u) = \arctan(x)u/(2\pi)+\varepsilon.
/// $$
/// - If $x$ is NaN or zero, $u = 0$, or $|x|$ is 1 or infinite, $\varepsilon$ may be ignored or
///   assumed to be 0.
/// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\arctan(x)u/(2\pi)|\rfloor-p}$, where $p$ is the
///   precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
///
/// Special cases:
/// - $f(\text{NaN},u)=\text{NaN}$
/// - $f(\pm\infty,u)=\pm u/4$, a quarter turn
/// - $f(\pm0.0,u)=\pm0.0$
/// - $f(x,0)=\pm0.0$, with the sign of $x$, so that the function stays odd
/// - $f(\pm1,u)=\pm u/8$, an eighth of a turn
///
/// Overflow is not possible, since $|f(x,u)| < u/4 < 2^{62}$. The result is subnormal, or zero,
/// only when $x$ is tiny and $u$ is small, since the result is about $xu/(2\pi)$ there.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::NegativeInfinity;
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan_with_period;
///
/// assert!(primitive_float_atan_with_period(f32::NAN, 360).is_nan());
/// // an infinite input is a quarter turn
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period(f32::INFINITY, 360)),
///     NiceFloat(90.0)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period(
///         f32::NEGATIVE_INFINITY,
///         360
///     )),
///     NiceFloat(-90.0)
/// );
/// // an input of 1 is an eighth of a turn
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period(1.0f32, 360)),
///     NiceFloat(45.0)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period(2.0f32, 360)),
///     NiceFloat(63.434948)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period(2.0f64, 360)),
///     NiceFloat(63.43494882292201)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan_with_period<T: PrimitiveFloat>(x: T, u: u64) -> T
where
    Float: From<T> + PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    emulate_float_to_float_fn(|x, prec| Float::atan_with_period_prec(x, u, prec), x)
}

/// Computes $\arctan(x)u/(2\pi)$, the arctangent of a [`Rational`] measured in $u$ths of a turn (so
/// that `u = 360` gives degrees), returning the result as a primitive float.
///
/// $$
/// f(x,u) = \arctan(x)u/(2\pi)+\varepsilon.
/// $$
/// - If $x$ is zero, $u = 0$, or $|x|$ is 1, $\varepsilon$ may be ignored or assumed to be 0.
/// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\arctan(x)u/(2\pi)|\rfloor-p}$, where $p$ is the
///   precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
///
/// Special cases:
/// - $f(0,u)=0.0$
/// - $f(x,0)=\pm0.0$, with the sign of $x$, so that the function stays odd
/// - $f(\pm1,u)=\pm u/8$, an eighth of a turn
///
/// Overflow is not possible, since $|f(x,u)| < u/4 < 2^{62}$. The result is subnormal, or zero,
/// only when $x$ is tiny and $u$ is small, since the result is about $xu/(2\pi)$ there.
///
/// # Worst-case complexity
/// $T(m) = O(m \log m \log\log m)$
///
/// $M(m) = O(m \log m)$
///
/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{One, Zero};
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan_with_period_rational;
/// use malachite_q::Rational;
///
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period_rational::<f64>(
///         &Rational::ZERO,
///         360
///     )),
///     NiceFloat(0.0)
/// );
/// // an input of 1 is an eighth of a turn
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period_rational::<f64>(
///         &Rational::ONE,
///         360
///     )),
///     NiceFloat(45.0)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period_rational::<f64>(
///         &Rational::from_unsigneds(1u8, 3),
///         360
///     )),
///     NiceFloat(18.43494882292201)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_with_period_rational::<f32>(
///         &Rational::from_unsigneds(1u8, 3),
///         360
///     )),
///     NiceFloat(18.434948)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan_with_period_rational<T: PrimitiveFloat>(x: &Rational, u: u64) -> T
where
    Float: PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    emulate_rational_to_float_fn(
        |x, prec| Float::atan_with_period_rational_prec_ref(x, u, prec),
        x,
    )
}

/// Computes $\arctan(x)/\pi$, the arctangent of a primitive float measured in half-turns.
///
/// This is `primitive_float_atan_with_period` with a period of 2: see
/// [`primitive_float_atan_with_period`] for the error bound and the special cases, with $u = 2$. An
/// infinite input gives exactly $\pm1/2$, an input of $\pm1$ exactly $\pm1/4$, and a zero input
/// exactly $\pm0.0$; those are the only exact cases. Overflow is not possible, since
/// $|\arctan(x)/\pi| < 1/2$, and the result is subnormal, or zero, only for a subnormal input.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::NegativeInfinity;
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan_pi;
///
/// assert!(primitive_float_atan_pi(f32::NAN).is_nan());
/// // an infinite input is half a turn
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi(f32::INFINITY)),
///     NiceFloat(0.5)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi(f32::NEGATIVE_INFINITY)),
///     NiceFloat(-0.5)
/// );
/// // an input of 1 is a quarter of a half-turn
/// assert_eq!(NiceFloat(primitive_float_atan_pi(1.0f32)), NiceFloat(0.25));
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi(0.1f32)),
///     NiceFloat(0.03172552)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi(0.1f64)),
///     NiceFloat(0.031725517430553574)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan_pi<T: PrimitiveFloat>(x: T) -> T
where
    Float: From<T> + PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    primitive_float_atan_with_period(x, 2)
}

/// Computes $\arctan(x)/\pi$, the arctangent of a [`Rational`] measured in half-turns, returning
/// the result as a primitive float.
///
/// This is `primitive_float_atan_with_period_rational` with a period of 2: see
/// [`primitive_float_atan_with_period_rational`] for the error bound and the special cases, with $u
/// = 2$. An input of $\pm1$ gives exactly $\pm1/4$ and a zero input exactly $0.0$; those are the
/// only exact cases. Overflow is not possible, since $|\arctan(x)/\pi| < 1/2$.
///
/// # Worst-case complexity
/// $T(m) = O(m \log m \log\log m)$
///
/// $M(m) = O(m \log m)$
///
/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::basic::traits::{One, Zero};
/// use malachite_base::num::float::NiceFloat;
/// use malachite_float::float::arithmetic::atan::primitive_float_atan_pi_rational;
/// use malachite_q::Rational;
///
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi_rational::<f64>(&Rational::ZERO)),
///     NiceFloat(0.0)
/// );
/// // an input of 1 is a quarter of a half-turn
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi_rational::<f64>(&Rational::ONE)),
///     NiceFloat(0.25)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi_rational::<f64>(
///         &Rational::from_unsigneds(1u8, 3)
///     )),
///     NiceFloat(0.10241638234956672)
/// );
/// assert_eq!(
///     NiceFloat(primitive_float_atan_pi_rational::<f32>(
///         &Rational::from_unsigneds(1u8, 3)
///     )),
///     NiceFloat(0.10241638)
/// );
/// ```
#[inline]
#[allow(clippy::type_repetition_in_bounds)]
pub fn primitive_float_atan_pi_rational<T: PrimitiveFloat>(x: &Rational) -> T
where
    Float: PartialOrd<T>,
    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
    primitive_float_atan_with_period_rational(x, 2)
}