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
|
% \iffalse meta-comment
%
%% File: l3fp-basics.dtx Copyright (C) 2011-2014,2016 The LaTeX3 Project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version. The latest version
%% of this license is in the file
%%
%% http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "l3kernel bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%% http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%% Snapshots taken from the repository represent work in progress and may
%% not work or may contain conflicting material! We therefore ask
%% people _not_ to put them into distributions, archives, etc. without
%% prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%%
%
%<*driver>
\documentclass[full]{l3doc}
\GetIdInfo$Id: l3fp-basics.dtx 6441 2016-03-24 10:11:59Z joseph $
{L3 Floating-point arithmetic}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{l3fp-basics} package\thanks{This file
% has version number \ExplFileVersion, last
% revised \ExplFileDate.}\\
% Floating point arithmetic}
% \author{^^A
% The \LaTeX3 Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
% \date{Released \ExplFileDate}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3fp-basics} Implementation}
%
% \begin{macrocode}
%<*initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=fp>
% \end{macrocode}
%
% The \pkg{l3fp-basics} module implements addition, subtraction,
% multiplication, and division of two floating points, and the absolute
% value and sign-changing operations on one floating point.
% All operations implemented in this module yield the outcome of
% rounding the infinitely precise result of the operation to the
% nearest floating point.
%
% Some algorithms used below end up being quite similar to some
% described in \enquote{What Every Computer Scientist Should Know About
% Floating Point Arithmetic}, by David Goldberg, which can be found at
% \texttt{http://cr.yp.to/2005-590/goldberg.pdf}.
%
% \subsection{Common to several operations}
%
% \begin{macro}[aux, EXP]
% {
% \@@_basics_pack_low:NNNNNw,
% \@@_basics_pack_high:NNNNNw,
% \@@_basics_pack_high_carry:w
% }
% Addition and multiplication of significands are done in two steps:
% first compute a (more or less) exact result, then round and pack
% digits in the final (braced) form. These functions take care of the
% packing, with special attention given to the case where rounding has
% caused a carry. Since rounding can only shift the final digit by
% $1$, a carry always produces an exact power of $10$. Thus,
% \cs{@@_basics_pack_high_carry:w} is always followed by four times
% |{0000}|.
% \begin{macrocode}
\cs_new:Npn \@@_basics_pack_low:NNNNNw #1 #2#3#4#5 #6;
{ + #1 - \c_one ; {#2#3#4#5} {#6} ; }
\cs_new:Npn \@@_basics_pack_high:NNNNNw #1 #2#3#4#5 #6;
{
\if_meaning:w 2 #1
\@@_basics_pack_high_carry:w
\fi:
; {#2#3#4#5} {#6}
}
\cs_new:Npn \@@_basics_pack_high_carry:w \fi: ; #1
{ \fi: + \c_one ; {1000} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]
% {
% \@@_basics_pack_weird_low:NNNNw,
% \@@_basics_pack_weird_high:NNNNNNNNw
% }
% I don't fully understand those functions, used for additions and
% divisions. Hence the name.
% \begin{macrocode}
\cs_new:Npn \@@_basics_pack_weird_low:NNNNw #1 #2#3#4 #5;
{
\if_meaning:w 2 #1
+ \c_one
\fi:
\__int_eval_end:
#2#3#4; {#5} ;
}
\cs_new:Npn \@@_basics_pack_weird_high:NNNNNNNNw
1 #1#2#3#4 #5#6#7#8 #9; { ; {#1#2#3#4} {#5#6#7#8} {#9} }
% \end{macrocode}
% \end{macro}
%
% \subsection{Addition and subtraction}
%
% We define here two functions, \cs{@@_-_o:ww} and \cs{@@_+_o:ww}, which
% perform the subtraction and addition of their two floating point
% operands, and expand the tokens following the result once.
%
% A more obscure function, \cs{@@_add_big_i_o:wNww}, is used in
% \pkg{l3fp-expo}.
%
% The logic goes as follows:
% \begin{itemize}
% \item \cs{@@_-_o:ww} calls \cs{@@_+_o:ww} to do the work, with the
% sign of the second operand flipped;
% \item \cs{@@_+_o:ww} dispatches depending on the type of floating
% point, calling specialized auxiliaries;
% \item in all cases except summing two normal floating point numbers,
% we return one or the other operands depending on the signs, or
% detect an invalid operation in the case of $\infty - \infty$;
% \item for normal floating point numbers, compare the signs;
% \item to add two floating point numbers of the same sign or of
% opposite signs, shift the significand of the smaller one to match the
% bigger one, perform the addition or subtraction of significands,
% check for a carry, round, and pack using the
% |\__fp_basics_pack_...| functions.
% \end{itemize}
% The trickiest part is to round correctly when adding or subtracting
% normal floating point numbers.
%
% \subsubsection{Sign, exponent, and special numbers}
%
% \begin{macro}[int, EXP]{\@@_-_o:ww}
% A previous version of this function grabbed its two operands,
% changed the sign of the second, and called \cs{@@_+_o:ww}. However,
% for efficiency reasons, the operands were swapped in the process,
% which means that error messages ended up wrong. Now, the
% \cs{@@_+_o:ww} auxiliary has a hook: it takes one argument between
% the first \cs{s_@@} and \cs{@@_chk:w}, which is applied to the sign
% of the second operand. Positioning the hook there means that
% \cs{@@_+_o:ww} can still check that it was followed by \cs{s_@@} and
% not arbitrary junk.
% \begin{macrocode}
\cs_new_nopar:cpx { @@_-_o:ww } \s_@@
{
\exp_not:c { @@_+_o:ww }
\exp_not:n { \s_@@ \@@_neg_sign:N }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, EXP]{\@@_+_o:ww}
% This function is either called directly with an empty |#1| to
% compute an addition, or it is called by \cs{@@_-_o:ww} with
% \cs{@@_neg_sign:N} as |#1| to compute a subtraction (equivalent to
% changing the \meta{sign_2} of the second operand). If the
% \meta{types} |#2| and |#4| are the same, dispatch to case |#2| ($0$,
% $1$, $2$, or $3$), where we call specialized functions: thanks to
% \cs{__int_value:w}, those receive the tweaked \meta{sign_2}
% (expansion of |#1#5|) as an argument. If the \meta{types} are
% distinct, the result is simply the floating point number with the
% highest \meta{type}. Since case $3$ (used for two \texttt{nan})
% also picks the first operand, we can also use it when \meta{type_1}
% is greater than \meta{type_2}. Also note that we don't need to
% worry about \meta{sign_2} in that case since the second operand is
% discarded.
% \begin{macrocode}
\cs_new:cpn { @@_+_o:ww }
\s_@@ #1 \@@_chk:w #2 #3 ; \s_@@ \@@_chk:w #4 #5
{
\if_case:w
\if_meaning:w #2 #4
#2 \exp_stop_f:
\else:
\if_int_compare:w #2 > #4 \exp_stop_f:
\c_three
\else:
\c_minus_one
\fi:
\fi:
\exp_after:wN \@@_add_zeros_o:Nww \__int_value:w
\or: \exp_after:wN \@@_add_normal_o:Nww \__int_value:w
\or: \exp_after:wN \@@_add_inf_o:Nww \__int_value:w
\or: \@@_case_return_i_o:ww
\else: \exp_after:wN \@@_add_return_ii_o:Nww \__int_value:w
\fi:
#1 #5
\s_@@ \@@_chk:w #2 #3 ;
\s_@@ \@@_chk:w #4 #5
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_add_return_ii_o:Nww}
% Ignore the first operand, and return the second, but using the sign
% |#1| rather than |#4|. As usual, expand after the floating point.
% \begin{macrocode}
\cs_new:Npn \@@_add_return_ii_o:Nww #1 #2 ; \s_@@ \@@_chk:w #3 #4
{ \@@_exp_after_o:w \s_@@ \@@_chk:w #3 #1 }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_add_zeros_o:Nww}
% Adding two zeros yields \cs{c_zero_fp}, except if both zeros were
% $-0$.
% \begin{macrocode}
\cs_new:Npn \@@_add_zeros_o:Nww #1 \s_@@ \@@_chk:w 0 #2
{
\if_int_compare:w #2 #1 = 20 \exp_stop_f:
\exp_after:wN \@@_add_return_ii_o:Nww
\else:
\@@_case_return_i_o:ww
\fi:
#1
\s_@@ \@@_chk:w 0 #2
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_add_inf_o:Nww}
% If both infinities have the same sign, just return that infinity,
% otherwise, it is an invalid operation. We find out if that invalid
% operation is an addition or a subtraction by testing whether the
% tweaked \meta{sign_2} (|#1|) and the \meta{sign_2} (|#4|) are
% identical.
% \begin{macrocode}
\cs_new:Npn \@@_add_inf_o:Nww
#1 \s_@@ \@@_chk:w 2 #2 #3; \s_@@ \@@_chk:w 2 #4
{
\if_meaning:w #1 #2
\@@_case_return_i_o:ww
\else:
\@@_case_use:nw
{
\if_meaning:w #1 #4
\exp_after:wN \@@_invalid_operation_o:Nww
\exp_after:wN +
\else:
\exp_after:wN \@@_invalid_operation_o:Nww
\exp_after:wN -
\fi:
}
\fi:
\s_@@ \@@_chk:w 2 #2 #3;
\s_@@ \@@_chk:w 2 #4
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_add_normal_o:Nww}
% \begin{quote}
% \cs{@@_add_normal_o:Nww} \meta{sign_2}
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_1}
% \meta{exp_1} \meta{body_1} |;|
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{initial sign_2}
% \meta{exp_2} \meta{body_2} |;|
% \end{quote}
% We now have two normal numbers to add, and we have to check signs
% and exponents more carefully before performing the addition.
% \begin{macrocode}
\cs_new:Npn \@@_add_normal_o:Nww #1 \s_@@ \@@_chk:w 1 #2
{
\if_meaning:w #1#2
\exp_after:wN \@@_add_npos_o:NnwNnw
\else:
\exp_after:wN \@@_sub_npos_o:NnwNnw
\fi:
#2
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Absolute addition}
%
% In this subsection, we perform the addition of two positive normal
% numbers.
%
% \begin{macro}[aux, EXP]{\@@_add_npos_o:NnwNnw}
% \begin{quote}
% \cs{@@_add_npos_o:NnwNnw} \meta{sign_1} \meta{exp_1} \meta{body_1}
% |;| \cs{s_@@} \cs{@@_chk:w} |1| \meta{initial sign_2} \meta{exp_2}
% \meta{body_2} |;|
% \end{quote}
% Since we are doing an addition, the final sign is \meta{sign_1}.
% Start an \cs{__int_eval:w}, responsible for computing the exponent:
% the result, and the \meta{final sign} are then given to
% \cs{@@_sanitize:Nw} which checks for overflow. The exponent is
% computed as the largest exponent |#2| or |#5|, incremented if there
% is a carry. To add the significands, we decimate the smaller number by
% the difference between the exponents. This is done by
% \cs{@@_add_big_i:wNww} or \cs{@@_add_big_ii:wNww}. We need to bring
% the final sign with us in the midst of the calculation to round
% properly at the end.
% \begin{macrocode}
\cs_new:Npn \@@_add_npos_o:NnwNnw #1#2#3 ; \s_@@ \@@_chk:w 1 #4 #5
{
\exp_after:wN \@@_sanitize:Nw
\exp_after:wN #1
\__int_value:w \__int_eval:w
\if_int_compare:w #2 > #5 \exp_stop_f:
#2
\exp_after:wN \@@_add_big_i_o:wNww \__int_value:w -
\else:
#5
\exp_after:wN \@@_add_big_ii_o:wNww \__int_value:w
\fi:
\__int_eval:w #5 - #2 ; #1 #3;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, rEXP]{\@@_add_big_i_o:wNww}
% \begin{macro}[aux, rEXP]{\@@_add_big_ii_o:wNww}
% \begin{quote}
% \cs{@@_add_big_i_o:wNww} \meta{shift} |;| \meta{final sign}
% \meta{body_1} |;| \meta{body_2} |;|
% \end{quote}
% Shift the significand of the small number, then add with
% \cs{@@_add_significand_o:NnnwnnnnN}.
% \begin{macrocode}
\cs_new:Npn \@@_add_big_i_o:wNww #1; #2 #3; #4;
{
\@@_decimate:nNnnnn {#1}
\@@_add_significand_o:NnnwnnnnN
#4
#3
#2
}
\cs_new:Npn \@@_add_big_ii_o:wNww #1; #2 #3; #4;
{
\@@_decimate:nNnnnn {#1}
\@@_add_significand_o:NnnwnnnnN
#3
#4
#2
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_add_significand_o:NnnwnnnnN}
% \begin{macro}[aux, rEXP]
% {\@@_add_significand_pack:NNNNNNN, \@@_add_significand_test_o:N}
% \begin{quote}\raggedright
% \cs{@@_add_significand_o:NnnwnnnnN}
% \meta{rounding digit}
% \Arg{Y'_1} \Arg{Y'_2} \meta{extra-digits} |;|
% \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
% \meta{final sign}
% \end{quote}
% To round properly, we must know at which digit the rounding
% should occur. This requires to know whether the addition
% produces an overall carry or not. Thus, we do the computation
% now and check for a carry, then go back and do the rounding.
% The rounding may cause a carry in very rare cases such as
% $0.99\cdots 95 \to 1.00\cdots 0$, but this situation always
% give an exact power of $10$, for which it is easy to correct
% the result at the end.
% \begin{macrocode}
\cs_new:Npn \@@_add_significand_o:NnnwnnnnN #1 #2#3 #4; #5#6#7#8
{
\exp_after:wN \@@_add_significand_test_o:N
\__int_value:w \__int_eval:w 1#5#6 + #2
\exp_after:wN \@@_add_significand_pack:NNNNNNN
\__int_value:w \__int_eval:w 1#7#8 + #3 ; #1
}
\cs_new:Npn \@@_add_significand_pack:NNNNNNN #1 #2#3#4#5#6#7
{
\if_meaning:w 2 #1
+ \c_one
\fi:
; #2 #3 #4 #5 #6 #7 ;
}
\cs_new:Npn \@@_add_significand_test_o:N #1
{
\if_meaning:w 2 #1
\exp_after:wN \@@_add_significand_carry_o:wwwNN
\else:
\exp_after:wN \@@_add_significand_no_carry_o:wwwNN
\fi:
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_add_significand_no_carry_o:wwwNN}
% \begin{quote}
% \cs{@@_add_significand_no_carry_o:wwwNN}
% \meta{8d} |;| \meta{6d} |;| \meta{2d} |;|
% \meta{rounding digit} \meta{sign}
% \end{quote}
% If there's no carry, grab all the digits again and round. The
% packing function \cs{@@_basics_pack_high:NNNNNw} takes care of the
% case where rounding brings a carry.
% \begin{macrocode}
\cs_new:Npn \@@_add_significand_no_carry_o:wwwNN
#1; #2; #3#4 ; #5#6
{
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1 #1
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 1 #2 #3#4
+ \@@_round:NNN #6 #4 #5
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_add_significand_carry_o:wwwNN}
% \begin{quote}
% \cs{@@_add_significand_carry_o:wwwNN}
% \meta{8d} |;| \meta{6d} |;| \meta{2d} |;|
% \meta{rounding digit} \meta{sign}
% \end{quote}
% The case where there is a carry is very similar. Rounding can even
% raise the first digit from $1$ to $2$, but we don't care.
% \begin{macrocode}
\cs_new:Npn \@@_add_significand_carry_o:wwwNN
#1; #2; #3#4; #5#6
{
+ \c_one
\exp_after:wN \@@_basics_pack_weird_high:NNNNNNNNw
\__int_value:w \__int_eval:w 1 1 #1
\exp_after:wN \@@_basics_pack_weird_low:NNNNw
\__int_value:w \__int_eval:w 1 #2#3 +
\exp_after:wN \@@_round:NNN
\exp_after:wN #6
\exp_after:wN #3
\__int_value:w \@@_round_digit:Nw #4 #5 ;
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Absolute subtraction}
%
% \begin{macro}[aux, EXP]{\@@_sub_npos_o:NnwNnw}
% \begin{macro}[aux, EXP]{\@@_sub_eq_o:Nnwnw, \@@_sub_npos_ii_o:Nnwnw}
% \begin{quote}
% \cs{@@_sub_npos_o:NnwNnw}
% \meta{sign_1} \meta{exp_1} \meta{body_1} |;|
% \cs{s_@@} \cs{@@_chk:w} |1|
% \meta{initial sign_2} \meta{exp_2} \meta{body_2} |;|
% \end{quote}
% Rounding properly in some modes requires to know what the sign of
% the result will be. Thus, we start by comparing the exponents and
% significands. If the numbers coincide, return zero. If the second
% number is larger, swap the numbers and call
% \cs{@@_sub_npos_i_o:Nnwnw} with the opposite of \meta{sign_1}.
% \begin{macrocode}
\cs_new:Npn \@@_sub_npos_o:NnwNnw #1#2#3; \s_@@ \@@_chk:w 1 #4#5#6;
{
\if_case:w \@@_compare_npos:nwnw {#2} #3; {#5} #6; \exp_stop_f:
\exp_after:wN \@@_sub_eq_o:Nnwnw
\or:
\exp_after:wN \@@_sub_npos_i_o:Nnwnw
\else:
\exp_after:wN \@@_sub_npos_ii_o:Nnwnw
\fi:
#1 {#2} #3; {#5} #6;
}
\cs_new:Npn \@@_sub_eq_o:Nnwnw #1#2; #3; { \exp_after:wN \c_zero_fp }
\cs_new:Npn \@@_sub_npos_ii_o:Nnwnw #1 #2; #3;
{
\exp_after:wN \@@_sub_npos_i_o:Nnwnw
\__int_value:w \__int_eval:w \c_two - #1 \__int_eval_end:
#3; #2;
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_sub_npos_i_o:Nnwnw}
% After the computation is done, \cs{@@_sanitize:Nw} checks for
% overflow/underflow. It expects the \meta{final sign} and the
% \meta{exponent} (delimited by |;|). Start an integer expression for
% the exponent, which starts with the exponent of the largest number,
% and may be decreased if the two numbers are very close. If the two
% numbers have the same exponent, call the \texttt{near} auxiliary.
% Otherwise, decimate $y$, then call the \texttt{far} auxiliary to
% evaluate the difference between the two significands. Note that we
% decimate by $1$ less than one could expect.
% \begin{macrocode}
\cs_new:Npn \@@_sub_npos_i_o:Nnwnw #1 #2#3; #4#5;
{
\exp_after:wN \@@_sanitize:Nw
\exp_after:wN #1
\__int_value:w \__int_eval:w
#2
\if_int_compare:w #2 = #4 \exp_stop_f:
\exp_after:wN \@@_sub_back_near_o:nnnnnnnnN
\else:
\exp_after:wN \@@_decimate:nNnnnn \exp_after:wN
{ \__int_value:w \__int_eval:w #2 - #4 - \c_one \exp_after:wN }
\exp_after:wN \@@_sub_back_far_o:NnnwnnnnN
\fi:
#5
#3
#1
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sub_back_near_o:nnnnnnnnN}
% \begin{macro}[aux, rEXP]
% {\@@_sub_back_near_pack:NNNNNNw, \@@_sub_back_near_after:wNNNNw}
% \begin{quote}
% \cs{@@_sub_back_near_o:nnnnnnnnN}
% \Arg{Y_1} \Arg{Y_2} \Arg{Y_3} \Arg{Y_4}
% \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
% \meta{final sign}
% \end{quote}
% In this case, the subtraction is exact, so we discard the
% \meta{final sign} |#9|. The very large shifts of $10^{9}$ and
% $1.1\cdot10^{9}$ are unnecessary here, but allow the auxiliaries to
% be reused later. Each integer expression produces a $10$ digit
% result. If the resulting $16$ digits start with a $0$, then we need
% to shift the group, padding with trailing zeros.
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_near_o:nnnnnnnnN #1#2#3#4 #5#6#7#8 #9
{
\exp_after:wN \@@_sub_back_near_after:wNNNNw
\__int_value:w \__int_eval:w 10#5#6 - #1#2 - \c_eleven
\exp_after:wN \@@_sub_back_near_pack:NNNNNNw
\__int_value:w \__int_eval:w 11#7#8 - #3#4 \exp_after:wN ;
}
\cs_new:Npn \@@_sub_back_near_pack:NNNNNNw #1#2#3#4#5#6#7 ;
{ + #1#2 ; {#3#4#5#6} {#7} ; }
\cs_new:Npn \@@_sub_back_near_after:wNNNNw 10 #1#2#3#4 #5 ;
{
\if_meaning:w 0 #1
\exp_after:wN \@@_sub_back_shift:wnnnn
\fi:
; {#1#2#3#4} {#5}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sub_back_shift:wnnnn}
% \begin{macro}[aux, rEXP]
% {
% \@@_sub_back_shift_ii:ww,
% \@@_sub_back_shift_iii:NNNNNNNNw,
% \@@_sub_back_shift_iv:nnnnw
% }
% \begin{quote}
% \cs{@@_sub_back_shift:wnnnn} |;|
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} |;|
% \end{quote}
% This function is called with $\meta{Z_1}\leq 999$. Act with
% \tn{number} to trim leading zeros from \meta{Z_1} \meta{Z_2} (we
% don't do all four blocks at once, since non-zero blocks would then
% overflow \TeX{}'s integers). If the first two blocks are zero, the
% auxiliary receives an empty |#1| and trims |#2#30| from leading
% zeros, yielding a total shift between $7$ and~$16$ to the exponent.
% Otherwise we get the shift from |#1| alone, yielding a result
% between $1$ and~$6$. Once the exponent is taken care of, trim
% leading zeros from |#1#2#3| (when |#1| is empty, the space before
% |#2#3| is ignored), get four blocks of $4$~digits and finally clean
% up. Trailing zeros are added so that digits can be grabbed safely.
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_shift:wnnnn ; #1#2
{
\exp_after:wN \@@_sub_back_shift_ii:ww
\__int_value:w #1 #2 0 ;
}
\cs_new:Npn \@@_sub_back_shift_ii:ww #1 0 ; #2#3 ;
{
\if_meaning:w @ #1 @
- \c_seven
- \exp_after:wN \use_i:nnn
\exp_after:wN \@@_sub_back_shift_iii:NNNNNNNNw
\__int_value:w #2#3 0 ~ 123456789;
\else:
- \@@_sub_back_shift_iii:NNNNNNNNw #1 123456789;
\fi:
\exp_after:wN \@@_pack_twice_four:wNNNNNNNN
\exp_after:wN \@@_pack_twice_four:wNNNNNNNN
\exp_after:wN \@@_sub_back_shift_iv:nnnnw
\exp_after:wN ;
\__int_value:w
#1 ~ #2#3 0 ~ 0000 0000 0000 000 ;
}
\cs_new:Npn \@@_sub_back_shift_iii:NNNNNNNNw #1#2#3#4#5#6#7#8#9; {#8}
\cs_new:Npn \@@_sub_back_shift_iv:nnnnw #1 ; #2 ; { ; #1 ; }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sub_back_far_o:NnnwnnnnN}
% \begin{quote}\raggedright
% \cs{@@_sub_back_far_o:NnnwnnnnN}
% \meta{rounding} \Arg{Y'_1} \Arg{Y'_2} \meta{extra-digits} |;|
% \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
% \meta{final sign}
% \end{quote}
% If the difference is greater than $10^{\meta{expo_x}}$, call the
% \texttt{very_far} auxiliary. If the result is less than
% $10^{\meta{expo_x}}$, call the \texttt{not_far} auxiliary. If it is
% too close a call to know yet, namely if $1 \meta{Y'_1} \meta{Y'_2} =
% \meta{X_1} \meta{X_2} \meta{X_3} \meta{X_4} 0$, then call the
% \texttt{quite_far} auxiliary. We use the odd combination of space
% and semi-colon delimiters to allow the \texttt{not_far} auxiliary to
% grab each piece individually, the \texttt{very_far} auxiliary to use
% \cs{@@_pack_eight:wNNNNNNNN}, and the \texttt{quite_far} to ignore
% the significands easily (using the |;| delimiter).
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_far_o:NnnwnnnnN #1 #2#3 #4; #5#6#7#8
{
\if_case:w
\if_int_compare:w 1 #2 = #5#6 \use_i:nnnn #7 \exp_stop_f:
\if_int_compare:w #3 = \use_none:n #7#8 0 \exp_stop_f:
\c_zero
\else:
\if_int_compare:w #3 > \use_none:n #7#8 0 - \fi: \c_one
\fi:
\else:
\if_int_compare:w 1 #2 > #5#6 \use_i:nnnn #7 - \fi: \c_one
\fi:
\exp_after:wN \@@_sub_back_quite_far_o:wwNN
\or: \exp_after:wN \@@_sub_back_very_far_o:wwwwNN
\else: \exp_after:wN \@@_sub_back_not_far_o:wwwwNN
\fi:
#2 ~ #3 ; #5 #6 ~ #7 #8 ; #1
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_sub_back_quite_far_o:wwNN}
% \begin{macro}[aux, EXP]{\@@_sub_back_quite_far_ii:NN}
% The easiest case is when $x-y$ is extremely close to a power of
% $10$, namely the first digit of $x$ is $1$, and all others vanish
% when subtracting $y$. Then the \meta{rounding} |#3| and the
% \meta{final sign} |#4| control whether we get $1$ or $0.9999 9999
% 9999 9999$. In the usual round-to-nearest mode, we will get $1$
% whenever the \meta{rounding} digit is less than or equal to $5$
% (remember that the \meta{rounding} digit is only equal to $5$ if
% there was no further non-zero digit).
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_quite_far_o:wwNN #1; #2; #3#4
{
\exp_after:wN \@@_sub_back_quite_far_ii:NN
\exp_after:wN #3
\exp_after:wN #4
}
\cs_new:Npn \@@_sub_back_quite_far_ii:NN #1#2
{
\if_case:w \@@_round_neg:NNN #2 0 #1
\exp_after:wN \use_i:nn
\else:
\exp_after:wN \use_ii:nn
\fi:
{ ; {1000} {0000} {0000} {0000} ; }
{ - \c_one ; {9999} {9999} {9999} {9999} ; }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sub_back_not_far_o:wwwwNN}
% In the present case, $x$ and $y$ have different exponents, but
% $y$~is large enough that $x-y$ has a smaller exponent than~$x$.
% Decrement the exponent (with |- \c_one|). Then proceed in a way
% similar to the \texttt{near} auxiliaries seen earlier, but
% multiplying $x$ by~$10$ (|#30| and |#40| below), and with the added
% quirk that the \meta{rounding} digit has to be taken into account.
% Namely, we may have to decrease the result by one unit if
% \cs{@@_round_neg:NNN} returns~$1$. This function expects the
% \meta{final sign}~|#6|, the last digit of |1100000000+#40-#2|, and
% the \meta{rounding} digit. Instead of redoing the computation for
% the second argument, we note that \cs{@@_round_neg:NNN} only cares
% about its parity, which is identical to that of the last digit
% of~|#2|.
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_not_far_o:wwwwNN #1 ~ #2; #3 ~ #4; #5#6
{
- \c_one
\exp_after:wN \@@_sub_back_near_after:wNNNNw
\__int_value:w \__int_eval:w 1#30 - #1 - \c_eleven
\exp_after:wN \@@_sub_back_near_pack:NNNNNNw
\__int_value:w \__int_eval:w 11 0000 0000 + #40 - #2
- \exp_after:wN \@@_round_neg:NNN
\exp_after:wN #6
\use_none:nnnnnnn #2 #5
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_sub_back_very_far_o:wwwwNN}
% \begin{macro}[aux, EXP]{\@@_sub_back_very_far_ii_o:nnNwwNN}
% The case where $x-y$ and $x$ have the same exponent is a bit more
% tricky, mostly because it cannot reuse the same auxiliaries. Shift
% the $y$~significand by adding a leading~$0$. Then the logic is similar
% to the \texttt{not_far} functions above. Rounding is a bit more
% complicated: we have two \meta{rounding} digits |#3| and |#6| (from
% the decimation, and from the new shift) to take into account, and
% getting the parity of the main result requires a computation. The
% first \cs{__int_value:w} triggers the second one because the number
% is unfinished; we can thus not use $0$ in place of $2$ there.
% \begin{macrocode}
\cs_new:Npn \@@_sub_back_very_far_o:wwwwNN #1#2#3#4#5#6#7
{
\@@_pack_eight:wNNNNNNNN
\@@_sub_back_very_far_ii_o:nnNwwNN
{ 0 #1#2#3 #4#5#6#7 }
;
}
\cs_new:Npn \@@_sub_back_very_far_ii_o:nnNwwNN #1#2 ; #3 ; #4 ~ #5; #6#7
{
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1#4 - #1 - \c_one
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 2#5 - #2
- \exp_after:wN \@@_round_neg:NNN
\exp_after:wN #7
\__int_value:w
\if_int_odd:w \__int_eval:w #5 - #2 \__int_eval_end:
1 \else: 2 \fi:
\__int_value:w \@@_round_digit:Nw #3 #6 ;
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Multiplication}
%
% \subsubsection{Signs, and special numbers}
%
% \begin{macro}[int, EXP]{\@@_*_o:ww}
% We go through an auxiliary, which is common with \cs{@@_/_o:ww}.
% The first argument is the operation, used for the invalid operation
% exception. The second is inserted in a formula to dispatch cases
% slightly differently between multiplication and division. The third
% is the operation for normal floating points. The fourth is there
% for extra cases needed in \cs{@@_/_o:ww}.
% \begin{macrocode}
\cs_new_nopar:cpn { @@_*_o:ww }
{
\@@_mul_cases_o:NnNnww
*
{ - \c_two + }
\@@_mul_npos_o:Nww
{ }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, EXP]{\@@_mul_cases_o:nNnnww}
% Split into $10$ cases ($12$ for division).
% If both numbers are normal, go to case $0$
% (same sign) or case $1$ (opposite signs): in both cases, call
% \cs{@@_mul_npos_o:Nww} to do the work. If the first operand is
% \texttt{nan}, go to case $2$, in which the second operand is
% discarded; if the second operand is \texttt{nan}, go to case $3$, in
% which the first operand is discarded (note the weird interaction
% with the final test on signs). Then we separate the case where the
% first number is normal and the second is zero: this goes to cases
% $4$ and $5$ for multiplication, $10$ and $11$ for division.
% Otherwise, we do a computation which
% dispatches the products $0\times 0 = 0\times 1 = 1\times 0 = 0$ to
% case $4$ or $5$ depending on the combined sign, the products
% $0\times\infty$ and $\infty\times0$ to case $6$ or $7$ (invalid
% operation), and the products $1\times\infty = \infty\times1 =
% \infty\times\infty = \infty$ to cases $8$ and $9$. Note that the
% code for these two cases (which return $\pm\infty$) is inserted as
% argument |#4|, because it differs in the case of divisions.
% \begin{macrocode}
\cs_new:Npn \@@_mul_cases_o:NnNnww
#1#2#3#4 \s_@@ \@@_chk:w #5#6#7; \s_@@ \@@_chk:w #8#9
{
\if_case:w \__int_eval:w
\if_int_compare:w #5 #8 = \c_eleven
\c_one
\else:
\if_meaning:w 3 #8
\c_three
\else:
\if_meaning:w 3 #5
\c_two
\else:
\if_int_compare:w #5 #8 = \c_ten
\c_nine #2 - \c_two
\else:
(#5 #2 #8) / \c_two * \c_two + \c_seven
\fi:
\fi:
\fi:
\fi:
\if_meaning:w #6 #9 - \c_one \fi:
\__int_eval_end:
\@@_case_use:nw { #3 0 }
\or: \@@_case_use:nw { #3 2 }
\or: \@@_case_return_i_o:ww
\or: \@@_case_return_ii_o:ww
\or: \@@_case_return_o:Nww \c_zero_fp
\or: \@@_case_return_o:Nww \c_minus_zero_fp
\or: \@@_case_use:nw { \@@_invalid_operation_o:Nww #1 }
\or: \@@_case_use:nw { \@@_invalid_operation_o:Nww #1 }
\or: \@@_case_return_o:Nww \c_inf_fp
\or: \@@_case_return_o:Nww \c_minus_inf_fp
#4
\fi:
\s_@@ \@@_chk:w #5 #6 #7;
\s_@@ \@@_chk:w #8 #9
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Absolute multiplication}
%
% In this subsection, we perform the multiplication
% of two positive normal numbers.
%
% \begin{macro}[int, EXP]{\@@_mul_npos_o:Nww}
% \begin{quote}
% \cs{@@_mul_npos_o:Nww} \meta{final sign}
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_1} \Arg{exp_1} \meta{body_1} |;|
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_2} \Arg{exp_2} \meta{body_2} |;|
% \end{quote}
% After the computation, \cs{@@_sanitize:Nw} checks for overflow or
% underflow. As we did for addition, \cs{__int_eval:w} computes the
% exponent, catching any shift coming from the computation in the
% significand. The \meta{final sign} is needed to do the rounding
% properly in the significand computation. We setup the post-expansion
% here, triggered by \cs{@@_mul_significand_o:nnnnNnnnn}.
% \begin{macrocode}
\cs_new:Npn \@@_mul_npos_o:Nww
#1 \s_@@ \@@_chk:w #2 #3 #4 #5 ; \s_@@ \@@_chk:w #6 #7 #8 #9 ;
{
\exp_after:wN \@@_sanitize:Nw
\exp_after:wN #1
\__int_value:w \__int_eval:w
#4 + #8
\@@_mul_significand_o:nnnnNnnnn #5 #1 #9
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_mul_significand_o:nnnnNnnnn}
% \begin{macro}[aux, EXP]
% {\@@_mul_significand_drop:NNNNNw, \@@_mul_significand_keep:NNNNNw}
% \begin{quote}
% \cs{@@_mul_significand_o:nnnnNnnnn}
% \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \meta{sign}
% \Arg{Y_1} \Arg{Y_2} \Arg{Y_3} \Arg{Y_4}
% \end{quote}
% Note the three semicolons at the end of the definition. One is for
% the last \cs{@@_mul_significand_drop:NNNNNw}; one is for
% \cs{@@_round_digit:Nw} later on; and one, preceded by
% \cs{exp_after:wN}, which is correctly expanded (within an
% \cs{__int_eval:w}), is used by \cs{@@_basics_pack_low:NNNNNw}.
%
% The product of two $16$ digit integers has $31$ or $32$ digits,
% but it is impossible to know which one before computing. The place
% where we round depends on that number of digits, and may depend
% on all digits until the last in some rare cases. The approach is
% thus to compute the $5$ first blocks of $4$ digits (the first one
% is between $100$ and $9999$ inclusive), and a compact version of
% the remaining $3$ blocks. Afterwards, the number of digits is
% known, and we can do the rounding within yet another set of
% \cs{__int_eval:w}.
% \begin{macrocode}
\cs_new:Npn \@@_mul_significand_o:nnnnNnnnn #1#2#3#4 #5 #6#7#8#9
{
\exp_after:wN \@@_mul_significand_test_f:NNN
\exp_after:wN #5
\__int_value:w \__int_eval:w 99990000 + #1*#6 +
\exp_after:wN \@@_mul_significand_keep:NNNNNw
\__int_value:w \__int_eval:w 99990000 + #1*#7 + #2*#6 +
\exp_after:wN \@@_mul_significand_keep:NNNNNw
\__int_value:w \__int_eval:w 99990000 + #1*#8 + #2*#7 + #3*#6 +
\exp_after:wN \@@_mul_significand_drop:NNNNNw
\__int_value:w \__int_eval:w 99990000 + #1*#9 + #2*#8 + #3*#7 + #4*#6 +
\exp_after:wN \@@_mul_significand_drop:NNNNNw
\__int_value:w \__int_eval:w 99990000 + #2*#9 + #3*#8 + #4*#7 +
\exp_after:wN \@@_mul_significand_drop:NNNNNw
\__int_value:w \__int_eval:w 99990000 + #3*#9 + #4*#8 +
\exp_after:wN \@@_mul_significand_drop:NNNNNw
\__int_value:w \__int_eval:w 100000000 + #4*#9 ;
; \exp_after:wN ;
}
\cs_new:Npn \@@_mul_significand_drop:NNNNNw #1#2#3#4#5 #6;
{ #1#2#3#4#5 ; + #6 }
\cs_new:Npn \@@_mul_significand_keep:NNNNNw #1#2#3#4#5 #6;
{ #1#2#3#4#5 ; #6 ; }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_mul_significand_test_f:NNN}
% \begin{quote}
% \cs{@@_mul_significand_test_f:NNN} \meta{sign} |1|
% \meta{digits 1--8} |;| \meta{digits 9--12} |;| \meta{digits 13--16} |;|
% |+| \meta{digits 17--20} |+| \meta{digits 21--24}
% |+| \meta{digits 25--28} |+| \meta{digits 29--32} |;|
% \cs{exp_after:wN} |;|
% \end{quote}
% If the \meta{digit 1} is non-zero, then for rounding we only care
% about the digits $16$ and $17$, and whether further digits are zero
% or not (check for exact ties). On the other hand, if \meta{digit 1}
% is zero, we care about digits $17$ and $18$, and whether further
% digits are zero.
% \begin{macrocode}
\cs_new:Npn \@@_mul_significand_test_f:NNN #1 #2 #3
{
\if_meaning:w 0 #3
\exp_after:wN \@@_mul_significand_small_f:NNwwwN
\else:
\exp_after:wN \@@_mul_significand_large_f:NwwNNNN
\fi:
#1 #3
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_mul_significand_large_f:NwwNNNN}
% In this branch, \meta{digit 1} is non-zero. The result is thus
% \meta{digits 1--16}, plus some rounding which depends on the digits
% $16$, $17$, and whether all subsequent digits are zero or not.
% Here, \cs{@@_round_digit:Nw} takes digits $17$ and further (as an
% integer expression), and replaces it by a \meta{rounding digit},
% suitable for \cs{@@_round:NNN}.
% \begin{macrocode}
\cs_new:Npn \@@_mul_significand_large_f:NwwNNNN #1 #2; #3; #4#5#6#7; +
{
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1#2
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 1#3#4#5#6#7
+ \exp_after:wN \@@_round:NNN
\exp_after:wN #1
\exp_after:wN #7
\__int_value:w \@@_round_digit:Nw
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_mul_significand_small_f:NNwwwN}
% In this branch, \meta{digit 1} is zero. Our result will thus be
% \meta{digits 2--17}, plus some rounding which depends on the digits
% $17$, $18$, and whether all subsequent digits are zero or not.
% The $8$ digits |1#3| are followed, after expansion of the
% \texttt{small_pack} auxiliary, by the next digit, to form a $9$
% digit number.
% \begin{macrocode}
\cs_new:Npn \@@_mul_significand_small_f:NNwwwN #1 #2#3; #4#5; #6; + #7
{
- \c_one
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1#3#4
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 1#5#6#7
+ \exp_after:wN \@@_round:NNN
\exp_after:wN #1
\exp_after:wN #7
\__int_value:w \@@_round_digit:Nw
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Division}
%
% \subsubsection{Signs, and special numbers}
%
% Time is now ripe to tackle the hardest of the four elementary
% operations: division.
%
% \begin{macro}[int, EXP]{\@@_/_o:ww}
% Filtering special floating point is very similar to what we did for
% multiplications, with a few variations. Invalid operation
% exceptions display |/| rather than |*|. In the formula for
% dispatch, we replace |- \c_two +| by |-|. The case of normal
% numbers is treated using \cs{@@_div_npos_o:Nww} rather than
% \cs{@@_mul_npos_o:Nww}. There are two additional cases: if the
% first operand is normal and the second is a zero, then the division
% by zero exception is raised: cases $10$ and $11$ of the
% \cs{if_case:w} construction in \cs{@@_mul_cases_o:NnNnww} are
% provided as the fourth argument here.
% \begin{macrocode}
\cs_new_nopar:cpn { @@_/_o:ww }
{
\@@_mul_cases_o:NnNnww
/
{ - }
\@@_div_npos_o:Nww
{
\or:
\@@_case_use:nw
{ \@@_division_by_zero_o:NNww \c_inf_fp / }
\or:
\@@_case_use:nw
{ \@@_division_by_zero_o:NNww \c_minus_inf_fp / }
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_div_npos_o:Nww}
% \begin{quote}
% \cs{@@_div_npos_o:Nww} \meta{final sign}
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_A} \Arg{exp A}
% \Arg{A_1} \Arg{A_2} \Arg{A_3} \Arg{A_4} |;|
% \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_Z} \Arg{exp Z}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} |;|
% \end{quote}
% We want to compute $A/Z$. As for multiplication,
% \cs{@@_sanitize:Nw} checks for overflow or underflow; we provide it
% with the \meta{final sign}, and an integer expression in which we
% compute the exponent. We set up the arguments of
% \cs{@@_div_significand_i_o:wnnw}, namely an integer \meta{y} obtained
% by adding $1$ to the first $5$ digits of $Z$ (explanation given soon
% below), then the four \Arg{A_{i}}, then the four \Arg{Z_{i}}, a
% semi-colon, and the \meta{final sign}, used for rounding at the end.
% \begin{macrocode}
\cs_new:Npn \@@_div_npos_o:Nww
#1 \s_@@ \@@_chk:w 1 #2 #3 #4 ; \s_@@ \@@_chk:w 1 #5 #6 #7#8#9;
{
\exp_after:wN \@@_sanitize:Nw
\exp_after:wN #1
\__int_value:w \__int_eval:w
#3 - #6
\exp_after:wN \@@_div_significand_i_o:wnnw
\__int_value:w \__int_eval:w #7 \use_i:nnnn #8 + \c_one ;
#4
{#7}{#8}#9 ;
#1
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Work plan}
%
% In this subsection, we explain how to avoid overflowing \TeX{}'s
% integers when performing the division of two positive normal numbers.
%
% We are given two numbers, $A=0.A_{1}A_{2}A_{3}A_{4}$ and
% $Z=0.Z_{1}Z_{2}Z_{3}Z_{4}$, in blocks of $4$ digits, and we know that
% the first digits of $A_{1}$ and of $Z_{1}$ are non-zero. To compute
% $A/Z$, we proceed as follows.
% \begin{itemize}
% \item Find an integer $Q_{A} \simeq 10^{4} A / Z$.
% \item Replace $A$ by $B = 10^{4} A - Q_{A} Z$.
% \item Find an integer $Q_{B} \simeq 10^{4} B / Z$.
% \item Replace $B$ by $C = 10^{4} B - Q_{B} Z$.
% \item Find an integer $Q_{C} \simeq 10^{4} C / Z$.
% \item Replace $C$ by $D = 10^{4} C - Q_{C} Z$.
% \item Find an integer $Q_{D} \simeq 10^{4} D / Z$.
% \item Consider $E = 10^{4} D - Q_{D} Z$, and ensure
% correct rounding.
% \end{itemize}
% The result is then $Q = 10^{-4} Q_{A} + 10^{-8} Q_{B} + 10^{-12} Q_{C}
% + 10^{-16} Q_{D} + \text{rounding}$. Since the $Q_{i}$ are integers,
% $B$, $C$, $D$, and~$E$ are all exact multiples of $10^{-16}$, in other
% words, computing with $16$ digits after the decimal separator yields
% exact results. The problem will be overflow: in general $B$, $C$,
% $D$, and $E$ may be greater than $1$.
%
% Unfortunately, things are not as easy as they seem. In particular, we
% want all intermediate steps to be positive, since negative results
% would require extra calculations at the end. This requires that
% $Q_{A} \leq 10^{4} A / Z$ \emph{etc.} A reasonable attempt would be
% to define $Q_{A}$ as
% \begin{equation*}
% \cs{int_eval:n} \left\{
% \frac{ A_{1} A_{2} }{ Z_{1} + 1 } - 1 \right\}
% \leq 10^{4} \frac{A}{Z}
% \end{equation*}
% Subtracting $1$ at the end takes care of the fact that \eTeX{}'s
% \cs{__int_eval:w} rounds divisions instead of truncating (really,
% $1/2$ would be sufficient, but we work with integers). We add $1$ to
% $Z_{1}$ because $Z_{1} \leq 10^{4}Z < Z_{1}+1$ and we need $Q_{A}$ to
% be an underestimate. However, we are now underestimating $Q_{A}$ too
% much: it can be wrong by up to $100$, for instance when $Z = 0.1$ and
% $A \simeq 1$. Then $B$ could take values up to $10$ (maybe more), and
% a few steps down the line, we would run into arithmetic overflow,
% since \TeX{} can only handle integers less than roughly $2\cdot
% 10^{9}$.
%
% A better formula is to take
% \begin{equation*}
% Q_{A} = \cs{int_eval:n} \left\{
% \frac{ 10 \cdot A_{1} A_{2} }
% { \left\lfloor 10^{-3} \cdot Z_{1} Z_{2} \right\rfloor + 1 }
% - 1 \right\}.
% \end{equation*}
% This is always less than $10^{9} A / (10^{5} Z)$, as we wanted. In
% words, we take the $5$ first digits of $Z$ into account, and the $8$
% first digits of $A$, using $0$ as a $9$-th digit rather than the true
% digit for efficiency reasons. We shall prove that using this formula
% to define all the $Q_{i}$ avoids any overflow. For convenience, let
% us denote
% \begin{equation*}
% y = \left\lfloor 10^{-3} \cdot Z_{1} Z_{2} \right\rfloor + 1,
% \end{equation*}
% so that, taking into account the fact that \eTeX{} rounds ties away
% from zero,
% \begin{align*}
% Q_{A}
% &= \left\lfloor \frac{A_{1}A_{2}0}{y} - \frac{1}{2} \right\rfloor
% \\
% &>\frac{A_{1}A_{2}0}{y} - \frac{3}{2}.
% \end{align*}
% Note that $10^{4}<y\leq 10^{5}$, and $999 \leq Q_{A} \leq 99989$.
% Also note that this formula does not cause an overflow as long as $A <
% (2^{31}-1) / 10^{9} \simeq 2.147\cdots$, since the numerator involves an
% integer slightly smaller than $10^{9} A$.
%
% Let us bound $B$:
% \begin{align*}
% 10^{5} B
% &=
% A_{1}A_{2}0 + 10 \cdot 0.A_{3}A_{4}
% - 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4} \cdot Q_{A}
% \\
% &<
% A_{1}A_{2}0
% \cdot \left( 1 - 10 \cdot \frac{Z_{1}.Z_{2}Z_{3}Z_{4}}{y} \right)
% + \frac{3}{2} \cdot 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4} + 10
% \\
% &\leq
% \frac{A_{1}A_{2}0 \cdot (y - 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4})}{y}
% + \frac{3}{2} y + 10
% \\
% &\leq
% \frac{A_{1}A_{2}0\cdot 1}{y} + \frac{3}{2} y + 10
% \leq
% \frac{10^{9} A}{y} + 1.6\cdot y.
% \end{align*}
% At the last step, we hide $10$ into the second term for later
% convenience. The same reasoning yields
% \begin{align*}
% 10^{5} B &< 10^{9} A/y + 1.6 y, \\
% 10^{5} C &< 10^{9} B/y + 1.6 y, \\
% 10^{5} D &< 10^{9} C/y + 1.6 y, \\
% 10^{5} E &< 10^{9} D/y + 1.6 y. \\
% \end{align*}
% The goal is now to prove that none of $B$, $C$, $D$, and $E$ can go
% beyond $(2^{31}-1) / 10^{9} = 2.147\cdots$.
%
% Combining the various inequalities together with $A<1$, we get
% \begin{align*}
% 10^{5} B &< 10^{9}/y + 1.6 y, \\
% 10^{5} C &< 10^{13}/y^{2} + 1.6 (y + 10^{4}), \\
% 10^{5} D &< 10^{17}/y^{3} + 1.6 (y + 10^{4} + 10^{8}/y), \\
% 10^{5} E &< 10^{21}/y^{4} + 1.6 (y + 10^{4} + 10^{8}/y + 10^{12}/y^{2}). \\
% \end{align*}
% All of those bounds are convex functions of $y$ (since every power of
% $y$ involved is convex, and the coefficients are positive), and thus
% maximal at one of the end-points of the allowed range $10^{4} < y \leq
% 10^{5}$. Thus,
% \begin{align*}
% 10^{5} B &< \mathrm{max} ( 1.16\cdot 10^{5}, 1.7 \cdot 10^{5}), \\
% 10^{5} C &< \mathrm{max} ( 1.32\cdot 10^{5}, 1.77 \cdot 10^{5}), \\
% 10^{5} D &< \mathrm{max} ( 1.48\cdot 10^{5}, 1.777 \cdot 10^{5}), \\
% 10^{5} E &< \mathrm{max} ( 1.64\cdot 10^{5}, 1.7777 \cdot 10^{5}). \\
% \end{align*}
% All of those bounds are less than $2.147\cdot 10^{5}$, and we are thus
% within \TeX{}'s bounds in all cases!
%
% We will later need to have a bound on the $Q_{i}$. Their definitions
% imply that $Q_{A} < 10^{9} A/y - 1/2 < 10^{5} A$ and similarly for the
% other $Q_{i}$. Thus, all of them are less than $177770$.
%
% The last step is to ensure correct rounding. We have
% \begin{equation*}
% A/Z = \sum_{i=1}^{4} \left(10^{-4i} Q_{i}\right) + 10^{-16} E/Z
% \end{equation*}
% exactly. Furthermore, we know that the result will be in $[0.1,10)$,
% hence will be rounded to a multiple of $10^{-16}$ or of $10^{-15}$, so
% we only need to know the integer part of $E/Z$, and a
% \enquote{rounding} digit encoding the rest. Equivalently, we need to
% find the integer part of $2E/Z$, and determine whether it was an
% exact integer or not (this serves to detect ties). Since
% \begin{equation*}
% \frac{2E}{Z} = 2\frac{10^{5} E}{10^{5} Z}
% \leq 2\frac{10^{5} E}{10^{4}} < 36,
% \end{equation*}
% this integer part is between $0$ and $35$ inclusive. We let \eTeX{}
% round
% \begin{equation*}
% P = \cs{int_eval:n} \left\{
% \frac{2\cdot E_{1}E_{2}}{Z_{1}Z_{2}} \right\},
% \end{equation*}
% which differs from $2E/Z$ by at most
% \begin{equation*}
% \frac{1}{2}
% + 2 \left\lvert \frac{E}{Z} - \frac{E}{10^{-8} Z_{1}Z_{2}}\right\rvert
% + 2 \left\lvert \frac{10^{8} E - E_{1}E_{2}}{Z_{1}Z_{2}}\right\rvert
% < 1,
% \end{equation*}
% ($1/2$ comes from \eTeX{}'s rounding) because each absolute value is
% less than $10^{-7}$. Thus $P$ is either the correct integer part, or
% is off by $1$; furthermore, if $2 E / Z$ is an integer, $P = 2 E / Z$.
% We will check the sign of $2 E - P Z$. If it is negative, then $E / Z
% \in \big((P - 1) / 2, P / 2\big)$. If it is zero, then $E / Z = P /
% 2$. If it is positive, then $E / Z \in \big(P / 2, (P - 1) / 2\big)$.
% In each case, we know how to round to an integer, depending on the
% parity of $P$, and the rounding mode.
%
% \subsubsection{Implementing the significand division}
%
% \begin{macro}[aux, rEXP]{\@@_div_significand_i_o:wnnw}
% \begin{quote}
% \cs{@@_div_significand_i_o:wnnw} \meta{y} |;|
% \Arg{A_1} \Arg{A_2} \Arg{A_3} \Arg{A_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} |;| \meta{sign}
% \end{quote}
% Compute $10^{6} + Q_{A}$ (a $7$~digit number thanks to the shift),
% unbrace \meta{A_1} and \meta{A_2}, and prepare the
% \meta{continuation} arguments for $4$ consecutive calls to
% \cs{@@_div_significand_calc:wwnnnnnnn}. Each of these calls will need
% \meta{y} (|#1|), and it turns out that we need post-expansion there,
% hence the \cs{__int_value:w}. Here, |#4| is six brace groups, which
% give the six first |n|-type arguments of the \texttt{calc} function.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_i_o:wnnw #1 ; #2#3 #4 ;
{
\exp_after:wN \@@_div_significand_test_o:w
\__int_value:w \__int_eval:w
\exp_after:wN \@@_div_significand_calc:wwnnnnnnn
\__int_value:w \__int_eval:w 999999 + #2 #3 0 / #1 ;
#2 #3 ;
#4
{ \exp_after:wN \@@_div_significand_ii:wwn \__int_value:w #1 }
{ \exp_after:wN \@@_div_significand_ii:wwn \__int_value:w #1 }
{ \exp_after:wN \@@_div_significand_ii:wwn \__int_value:w #1 }
{ \exp_after:wN \@@_div_significand_iii:wwnnnnn \__int_value:w #1 }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, rEXP]{\@@_div_significand_calc:wwnnnnnnn}
% \begin{macro}[aux, rEXP]
% {
% \@@_div_significand_calc_i:wwnnnnnnn,
% \@@_div_significand_calc_ii:wwnnnnnnn,
% }
% \begin{quote}
% \cs{@@_div_significand_calc:wwnnnnnnn} \meta{$10^{6}+{}$Q_{A}} |;|
% \meta{A_1} \meta{A_2} |;| \Arg{A_3} \Arg{A_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
% \Arg{continuation}
% \end{quote}
% expands to
% \begin{quote}
% \meta{$10^{6}+{}$Q_{A}} \meta{continuation} |;|
% \meta{B_1} \meta{B_2} |;| \Arg{B_3} \Arg{B_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
% \end{quote}
% where $B = 10^{4} A - Q_{A} \cdot Z$. This function is also used to
% compute $C$, $D$, $E$ (with the input shifted accordingly), and is
% used in \pkg{l3fp-expo}.
%
% We know that $0<Q_{A}<1.8\cdot 10^{5}$, so the product of $Q_{A}$
% with each $Z_{i}$ is within \TeX{}'s bounds. However, it is a
% little bit too large for our purposes: we would not be able to use
% the usual trick of adding a large power of $10$ to ensure that the
% number of digits is fixed.
%
% The bound on $Q_{A}$, implies that $10^{6}+Q_{A}$ starts with the
% digit $1$, followed by $0$ or $1$. We test, and call different
% auxiliaries for the two cases. An earlier implementation did the
% tests within the computation, but since we added a
% \meta{continuation}, this is not possible because the macro has $9$
% parameters.
%
% The result we want is then (the overall power of $10$ is arbitrary):
% \begin{align*}
% &10^{-4} ( \#2 - \#1 \cdot \#5 - 10 \cdot \meta{i} \cdot \#5\#6 )
% + 10^{-8} ( \#3 - \#1 \cdot \#6 - 10 \cdot \meta{i} \cdot \#7 ) \\
% &+ 10^{-12}( \#4 - \#1 \cdot \#7 - 10 \cdot \meta{i} \cdot \#8 )
% + 10^{-16}( - \#1 \cdot \#8 ),
% \end{align*}
% where \meta{i} stands for the $10^{5}$ digit of $Q_{A}$, which is
% $0$ or~$1$, and $\#1$, $\#2$, \emph{etc.\@} are the parameters of
% either auxiliary. The factors of $10$ come from the fact that
% $Q_{A} = 10\cdot 10^{4} \cdot \meta{i} + \#1$. As usual, to combine
% all the terms, we need to choose some shifts which must ensure that
% the number of digits of the second, third, and fourth terms are each
% fixed. Here, the positive contributions are at most $10^{8}$ and
% the negative contributions can go up to $10^{9}$. Indeed, for the
% auxiliary with $\meta{i}=1$, |#1| is at most $80000$, leading to
% contributions of at worse $-8\cdot 10^{8}4$, while the other
% negative term is very small $<10^{6}$ (except in the first
% expression, where we don't care about the number of digits); for the
% auxiliary with $\meta{i}=0$, |#1| can go up to $99999$, but there is
% no other negative term. Hence, a good choice is $2\cdot 10^{9}$,
% which produces totals in the range $[10^{9}, 2.1\cdot 10^{9}]$. We
% are flirting with \TeX{}'s limits once more.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_calc:wwnnnnnnn 1#1
{
\if_meaning:w 1 #1
\exp_after:wN \@@_div_significand_calc_i:wwnnnnnnn
\else:
\exp_after:wN \@@_div_significand_calc_ii:wwnnnnnnn
\fi:
}
\cs_new:Npn \@@_div_significand_calc_i:wwnnnnnnn #1; #2;#3#4 #5#6#7#8 #9
{
1 1 #1
#9 \exp_after:wN ;
\__int_value:w \__int_eval:w \c_@@_Bigg_leading_shift_int
+ #2 - #1 * #5 - #5#60
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_middle_shift_int
+ #3 - #1 * #6 - #70
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_middle_shift_int
+ #4 - #1 * #7 - #80
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_trailing_shift_int
- #1 * #8 ;
{#5}{#6}{#7}{#8}
}
\cs_new:Npn \@@_div_significand_calc_ii:wwnnnnnnn #1; #2;#3#4 #5#6#7#8 #9
{
1 0 #1
#9 \exp_after:wN ;
\__int_value:w \__int_eval:w \c_@@_Bigg_leading_shift_int
+ #2 - #1 * #5
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_middle_shift_int
+ #3 - #1 * #6
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_middle_shift_int
+ #4 - #1 * #7
\exp_after:wN \@@_pack_Bigg:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_Bigg_trailing_shift_int
- #1 * #8 ;
{#5}{#6}{#7}{#8}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_div_significand_ii:wwn}
% \begin{quote}
% \cs{@@_div_significand_ii:wwn} \meta{y} |;|
% \meta{B_1} |;| \Arg{B_2} \Arg{B_3} \Arg{B_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
% \meta{continuations} \meta{sign}
% \end{quote}
% Compute $Q_{B}$ by evaluating $\meta{B_1}\meta{B_2}0 / y - 1$. The
% result will be output to the left, in an \cs{__int_eval:w} which we
% start now. Once that is evaluated (and the other $Q_{i}$ also,
% since later expansions are triggered by this one), a packing
% auxiliary takes care of placing the digits of $Q_{B}$ in an
% appropriate way for the final addition to obtain $Q$. This
% auxiliary is also used to compute $Q_{C}$ and $Q_{D}$ with the
% inputs $C$ and $D$ instead of $B$.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_ii:wwn #1; #2;#3
{
\exp_after:wN \@@_div_significand_pack:NNN
\__int_value:w \__int_eval:w
\exp_after:wN \@@_div_significand_calc:wwnnnnnnn
\__int_value:w \__int_eval:w 999999 + #2 #3 0 / #1 ; #2 #3 ;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_div_significand_iii:wwnnnnn}
% \begin{quote}
% \cs{@@_div_significand_iii:wwnnnnn} \meta{y} |;|
% \meta{E_1} |;| \Arg{E_2} \Arg{E_3} \Arg{E_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \meta{sign}
% \end{quote}
% We compute $P \simeq 2E/Z$ by rounding $2 E_{1} E_{2}/Z_{1}Z_{2}$.
% Note the first $0$, which multiplies $Q_{D}$ by $10$: we will later
% add (roughly) $5\cdot P$, which amounts to adding $P/2 \simeq E/Z$
% to $Q_{D}$, the appropriate correction from a hypothetical $Q_{E}$.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_iii:wwnnnnn #1; #2;#3#4#5 #6#7
{
0
\exp_after:wN \@@_div_significand_iv:wwnnnnnnn
\__int_value:w \__int_eval:w (\c_two * #2 #3) / #6 #7 ; % <- P
#2 ; {#3} {#4} {#5}
{#6} {#7}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]
% {
% \@@_div_significand_iv:wwnnnnnnn,
% \@@_div_significand_v:NNw,
% \@@_div_significand_vi:Nw
% }
% \begin{quote}
% \cs{@@_div_significand_iv:wwnnnnnnn} \meta{P} |;|
% \meta{E_1} |;| \Arg{E_2} \Arg{E_3} \Arg{E_4}
% \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \meta{sign}
% \end{quote}
% This adds to the current expression ($10^{7} + 10\cdot Q_{D}$) a
% contribution of $5 \cdot P + \operatorname{sign}(T)$ with $T = 2 E -
% P Z$. This amounts to adding $P / 2$ to $Q_{D}$, with an extra
% \meta{rounding} digit. This \meta{rounding} digit is $0$ or $5$ if
% $T$ does not contribute, \emph{i.e.,} if $0 = T = 2 E - P Z$, in
% other words if $10^{16} A / Z$ is an integer or half-integer.
% Otherwise it is in the appropriate range, $[1,4]$ or $[6,9]$. This
% is precise enough for rounding purposes (in any mode).
%
% It seems an overkill to compute $T$ exactly as I do here, but I see
% no faster way right now.
%
% Once more, we need to be careful and show that the calculation
% $\#1\cdot\#6\#7$ below does not cause an overflow: naively, $P$ can
% be up to $35$, and $\#6\#7$ up to $10^{8}$, but both cannot happen
% simultaneously. To show that things are fine, we split in two
% (non-disjoint) cases.
% \begin{itemize}
% \item For $P < 10$, the product obeys $P\cdot\#6\#7 < 10^{8} \cdot P
% < 10^{9} $.
% \item For large $P\geq 3$, the rounding error on $P$, which is at
% most $1$, is less than a factor of $2$, hence $P\leq 4E/Z$. Also,
% $\#6\#7 \leq 10^{8} \cdot Z$, hence $P\cdot \#6\#7 \leq 4E\cdot
% 10^{8} < 10^{9}$.
% \end{itemize}
% Both inequalities could be made tighter if needed.
%
% Note however that $P\cdot \#8\#9$ may overflow, since the two
% factors are now independent, and the result may reach $3.5\cdot
% 10^{9}$. Thus we compute the two lower levels separately. The rest
% is standard, except that we use |+| as a separator (ending integer
% expressions explicitly). $T$ is negative if the first character is
% |-|, it is positive if the first character is neither |0| nor |-|.
% It is also positive if the first character is |0| and second
% argument of \cs{@@_div_significand_vi:Nw}, a sum of several terms, is
% also zero. Otherwise, there was an exact agreement: $T = 0$.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_iv:wwnnnnnnn #1; #2;#3#4#5 #6#7#8#9
{
+ \c_five * #1
\exp_after:wN \@@_div_significand_vi:Nw
\__int_value:w \__int_eval:w -20 + 2*#2#3 - #1*#6#7 +
\exp_after:wN \@@_div_significand_v:NN
\__int_value:w \__int_eval:w 199980 + 2*#4 - #1*#8 +
\exp_after:wN \@@_div_significand_v:NN
\__int_value:w \__int_eval:w 200000 + 2*#5 - #1*#9 ;
}
\cs_new:Npn \@@_div_significand_v:NN #1#2 { #1#2 \__int_eval_end: + }
\cs_new:Npn \@@_div_significand_vi:Nw #1#2;
{
\if_meaning:w 0 #1
\if_int_compare:w \__int_eval:w #2 > \c_zero + \c_one \fi:
\else:
\if_meaning:w - #1 - \else: + \fi: \c_one
\fi:
;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_div_significand_pack:NNN}
% At this stage, we are in the following situation: \TeX{} is in the
% process of expanding several integer expressions, thus functions at
% the bottom expand before those above.
% \begin{quote}
% \cs{@@_div_significand_test_o:w} $10^{6} + Q_{A}$
% \cs{@@_div_significand_pack:NNN} $10^{6} + Q_{B}$
% \cs{@@_div_significand_pack:NNN} $10^{6} + Q_{C}$
% \cs{@@_div_significand_pack:NNN}
% $10^{7} + 10\cdot Q_{D} + 5 \cdot P + \varepsilon$ |;| \meta{sign}
% \end{quote}
% Here, $\varepsilon = \operatorname{sign}(T)$ is $0$ in case $2E=PZ$,
% $1$ in case $2E>PZ$, which means that $P$ was the correct value, but
% not with an exact quotient, and $-1$ if $2E<PZ$, \emph{i.e.}, $P$
% was an overestimate. The packing function we define now does
% nothing special: it removes the $10^{6}$ and carries two digits (for
% the $10^{5}$'s and the $10^{4}$'s).
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_pack:NNN 1 #1 #2 { + #1 #2 ; }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_div_significand_test_o:w}
% \begin{quote}
% \cs{@@_div_significand_test_o:w} |1| |0| \meta{5d} |;|
% ~~\meta{4d} |;| \meta{4d} |;| \meta{5d} |;| \meta{sign}
% \end{quote}
% The reason we know that the first two digits are |1| and |0| is that
% the final result is known to be between $0.1$ (inclusive) and $10$,
% hence $\widetilde{Q_{A}}$ (the tilde denoting the contribution from
% the other $Q_{i}$) is at most $99999$, and $10^{6}+\widetilde{Q_{A}}
% = 10\cdots$.
%
% It is now time to round. This depends on how many digits the final
% result will have.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_test_o:w 10 #1
{
\if_meaning:w 0 #1
\exp_after:wN \@@_div_significand_small_o:wwwNNNNwN
\else:
\exp_after:wN \@@_div_significand_large_o:wwwNNNNwN
\fi:
#1
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_div_significand_small_o:wwwNNNNwN}
% \begin{quote}
% \cs{@@_div_significand_small_o:wwwNNNNwN} |0| \meta{4d} |;|
% ~~\meta{4d} |;| \meta{4d} |;| \meta{5d} |;| \meta{final sign}
% \end{quote}
% Standard use of the functions \cs{@@_basics_pack_low:NNNNNw} and
% \cs{@@_basics_pack_high:NNNNNw}. We finally get to use the
% \meta{final sign} which has been sitting there for a while.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_small_o:wwwNNNNwN
0 #1; #2; #3; #4#5#6#7#8; #9
{
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1 #1#2
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 1 #3#4#5#6#7
+ \@@_round:NNN #9 #7 #8
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_div_significand_large_o:wwwNNNNwN}
% \begin{quote}
% \cs{@@_div_significand_large_o:wwwNNNNwN} \meta{5d} |;|
% ~~\meta{4d} |;| \meta{4d} |;| \meta{5d} |;| \meta{sign}
% \end{quote}
% We know that the final result cannot reach $10$, hence |1#1#2|,
% together with contributions from the level below, cannot reach
% $2\cdot 10^{9}$. For rounding, we build the \meta{rounding digit}
% from the last two of our $18$ digits.
% \begin{macrocode}
\cs_new:Npn \@@_div_significand_large_o:wwwNNNNwN
#1; #2; #3; #4#5#6#7#8; #9
{
+ \c_one
\exp_after:wN \@@_basics_pack_weird_high:NNNNNNNNw
\__int_value:w \__int_eval:w 1 #1 #2
\exp_after:wN \@@_basics_pack_weird_low:NNNNw
\__int_value:w \__int_eval:w 1 #3 #4 #5 #6 +
\exp_after:wN \@@_round:NNN
\exp_after:wN #9
\exp_after:wN #6
\__int_value:w \@@_round_digit:Nw #7 #8 ;
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Square root}
%
% \begin{macro}[int, EXP]{\@@_sqrt_o:w}
% Zeros are unchanged: $\sqrt{-0} = -0$ and $\sqrt{+0} = +0$.
% Negative numbers (other than $-0$) have no real square root.
% Positive infinity, and \texttt{nan}, are unchanged. Finally, for
% normal positive numbers, there is some work to do.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_o:w #1 \s_@@ \@@_chk:w #2#3#4; @
{
\if_meaning:w 0 #2 \@@_case_return_same_o:w \fi:
\if_meaning:w 2 #3
\@@_case_use:nw { \@@_invalid_operation_o:nw { sqrt } }
\fi:
\if_meaning:w 1 #2 \else: \@@_case_return_same_o:w \fi:
\@@_sqrt_npos_o:w
\s_@@ \@@_chk:w #2 #3 #4;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_sqrt_npos_o:w}
% \begin{macro}[aux, rEXP]
% {\@@_sqrt_npos_auxi_o:wwnnN, \@@_sqrt_npos_auxii_o:wNNNNNNNN}
% Prepare \cs{@@_sanitize:Nw} to receive the final sign~|0| (the
% result is always positive) and the exponent, equal to half of the
% exponent~|#1| of the argument. If the exponent~|#1| is even, find a
% first approximation of the square root of the significand $10^{8}
% a_1 + a_2 = 10^{8} |#2#3| + |#4#5|$ through Newton's method,
% starting at $x = 57234133 \simeq 10^{7.75}$. Otherwise, first shift
% the significand of of the argument by one digit, getting
% $a_1'\in[10^{6}, 10^{7})$ instead of $[10^{7}, 10^{8})$, then use
% Newton's method starting at $17782794 \simeq 10^{7.25}$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_npos_o:w \s_@@ \@@_chk:w 1 0 #1#2#3#4#5;
{
\exp_after:wN \@@_sanitize:Nw
\exp_after:wN 0
\__int_value:w \__int_eval:w
\if_int_odd:w #1 \exp_stop_f:
\exp_after:wN \@@_sqrt_npos_auxi_o:wwnnN
\fi:
#1 / \c_two
\@@_sqrt_Newton_o:wwn 56234133; 0; {#2#3} {#4#5} 0
}
\cs_new:Npn \@@_sqrt_npos_auxi_o:wwnnN #1 / \c_two #2; 0; #3#4#5
{
( #1 + \c_one ) / \c_two
\@@_pack_eight:wNNNNNNNN
\@@_sqrt_npos_auxii_o:wNNNNNNNN
;
0 #3 #4
}
\cs_new:Npn \@@_sqrt_npos_auxii_o:wNNNNNNNN #1; #2#3#4#5#6#7#8#9
{ \@@_sqrt_Newton_o:wwn 17782794; 0; {#1} {#2#3#4#5#6#7#8#9} }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sqrt_Newton_o:wwn}
% Newton's method maps $x\mapsto\bigl[(x + [10^{8} a_1 / x])/2\bigr]$
% in each iteration, where $[b/c]$ denotes \eTeX{}'s division. This
% division rounds the real number $b/c$ to the closest integer,
% rounding ties away from zero, hence when $c$~is even,
% $b/c - 1/2 + 1/c \leq [b/c] \leq b/c + 1/2$
% and when $c$~is odd,
% $b/c - 1/2 + 1/(2c) \leq [b/c] \leq b/c + 1/2 - 1/(2c)$.
% For all~$c$, $b/c - 1/2 + 1/(2c) \leq [b/c] \leq b/c + 1/2$.
%
% Let us prove that the method converges when implemented with \eTeX{}
% integer division, for any $10^{6} \leq a_1 < 10^{8}$ and starting
% value $10^{6} \leq x < 10^{8}$. Using the inequalities above and
% the arithmetic--geometric inequality $(x+t)/2 \geq \sqrt{xt}$ for $t
% = 10^{8} a_1 / x$, we find
% \[
% x'
% = \left[\frac{x + [10^{8} a_1 / x]}{2}\right]
% \geq \frac{x + 10^{8} a_1 / x - 1/2 + 1/(2x)}{2}
% \geq \sqrt{10^{8} a_1} - \frac{1}{4} + \frac{1}{4x} \,.
% \]
% After any step of iteration, we thus have $\delta = x - \sqrt{10^{8}
% a_1} \geq -0.25 + 0.25 \cdot 10^{-8}$. The new difference
% $\delta' = x' - \sqrt{10^{8} a_1}$ after one step is bounded above
% as
% \[
% x' - \sqrt{10^{8} a_1}
% \leq \frac{x + 10^{8} a_1 / x + 1/2}{2} + \frac{1}{2}
% - \sqrt{10^{8} a_1}
% \leq \frac{\delta}{2} \frac{\delta}{\sqrt{10^{8} a_1} + \delta}
% + \frac{3}{4} \,.
% \]
% For $\delta > 3/2$, this last expression is
% $\leq\delta/2+3/4<\delta$, hence $\delta$~decreases at each step:
% since all~$x$ are integers, $\delta$~must reach a value
% $-1/4<\delta\leq 3/2$. In this range of values, we get $\delta'
% \leq \frac{3}{4} \frac{3}{2\sqrt{10^{8} a_1}} + \frac{3}{4} \leq
% 0.75 + 1.125 \cdot 10^{-7}$. We deduce that the difference $\delta
% = x - \sqrt{10^{8} a_1}$ eventually reaches a value in the interval
% $[-0.25 + 0.25\cdot 10^{-8}, 0.75 + 11.25 \cdot 10^{-8}]$, whose
% width is $1 + 11 \cdot 10^{-8}$. The corresponding interval for~$x$
% may contain two integers, hence $x$~might oscillate between those
% two values.
%
% However, the fact that $x\mapsto x-1$ and $x-1 \mapsto x$ puts
% stronger constraints, which are not compatible: the first implies
% \[
% x + [10^{8} a_1 / x] \leq 2x - 2
% \]
% hence $10^{8} a_1 / x \leq x - 3/2$, while the second implies
% \[
% x - 1 + [10^{8} a_1 / (x - 1)] \geq 2x - 1
% \]
% hence $10^{8} a_1 / (x - 1) \geq x - 1/2$. Combining the two
% inequalities yields $x^2 - 3x/2 \geq 10^{8} a_1 \geq x - 3x/2 +
% 1/2$, which cannot hold. Therefore, the iteration always converges
% to a single integer~$x$. To stop the iteration when two consecutive
% results are equal, the function \cs{@@_sqrt_Newton_o:wwn} receives
% the newly computed result as~|#1|, the previous result as~|#2|, and
% $a_1$ as~|#3|. Note that \eTeX{} combines the computation of a
% multiplication and a following division, thus avoiding overflow in
% |#3 * 100000000 / #1|. In any case, the result is within $[10^{7},
% 10^{8}]$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_Newton_o:wwn #1; #2; #3
{
\if_int_compare:w #1 = #2 \exp_stop_f:
\exp_after:wN \@@_sqrt_auxi_o:NNNNwnnN
\__int_value:w \__int_eval:w 9999 9999 +
\exp_after:wN \@@_use_none_until_s:w
\fi:
\exp_after:wN \@@_sqrt_Newton_o:wwn
\__int_value:w \__int_eval:w (#1 + #3 * 1 0000 0000 / #1) / \c_two ;
#1; {#3}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sqrt_auxi_o:NNNNwnnN}
% This function is followed by $10^{8}+x-1$, which has~$9$ digits
% starting with~$1$, then |;| \Arg{a_1} \Arg{a_2} \meta{a'}. Here, $x
% \simeq \sqrt{10^{8} a_1}$ and we want to estimate the square root of
% $a = 10^{-8} a_1 + 10^{-16} a_2 + 10^{-17} a'$. We set up an
% initial underestimate
% \[
% y = (x - 1) 10^{-8} + 0.2499998875 \cdot 10^{-8} \lesssim \sqrt{a}\,.
% \]
% From the inequalities shown earlier, we know that $y \leq
% \sqrt{10^{-8} a_1} \leq \sqrt{a}$ and that $\sqrt{10^{-8} a_1} \leq
% y + 10^{-8} + 11\cdot 10^{-16}$ hence (using $0.1\leq y\leq
% \sqrt{a}\leq 1$)
% \[
% a - y^2 \leq 10^{-8} a_1 + 10^{-8} - y^2
% \leq (y + 10^{-8} + 11\cdot 10^{-16})^2 - y^2 + 10^{-8}
% < 3.2 \cdot 10^{-8} \,,
% \]
% and $\sqrt{a} - y = (a - y^2)/(\sqrt{a} + y) \leq 16 \cdot 10^{-8}$.
% Next, \cs{@@_sqrt_auxii_o:NnnnnnnnN} will be called several times to
% get closer and closer underestimates of~$\sqrt{a}$. By
% construction, the underestimates~$y$ are always increasing, $a - y^2
% < 3.2 \cdot 10^{-8}$ for all. Also, $y<1$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxi_o:NNNNwnnN 1 #1#2#3#4#5;
{
\@@_sqrt_auxii_o:NnnnnnnnN
\@@_sqrt_auxiii_o:wnnnnnnnn
{#1#2#3#4} {#5} {2499} {9988} {7500}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sqrt_auxii_o:NnnnnnnnN}
% This receives a continuation function~|#1|, then five blocks of~$4$
% digits for~$y$, then two $8$-digit blocks and a single digit
% for~$a$. A common estimate of $\sqrt{a} - y = (a - y^2) / (\sqrt{a}
% + y)$ is $(a - y^2)/(2y)$, which leads to alternating overestimates
% and underestimates. We tweak this, to only work with underestimates
% (no need then to worry about signs in the computation). Each step
% finds the largest integer $j\leq 6$ such that $10^{4j}(a-y^2) <
% 2\cdot 10^{8}$, then computes the integer (with \eTeX{}'s rounding
% division)
% \[
% 10^{4j} z =
% \Bigl[\bigl(\lfloor 10^{4j}(a-y^2)\rfloor - 257\bigr)
% \cdot (0.5\cdot 10^{8})
% \Bigm/ \lfloor 10^{8} y + 1\rfloor\Bigr] \,.
% \]
% The choice of~$j$ ensures that $10^{4j} z < 2\cdot 10^{8} \cdot
% 0.5\cdot 10^{8} / 10^{7} = 10^{9}$, thus $10^{9} + 10^{4j} z$ has
% exactly $10$~digits, does not overflow \TeX{}'s integer range, and
% starts with~$1$. Incidentally, since all $a - y^2 \leq 3.2\cdot
% 10^{-8}$, we know that $j\geq 3$.
%
% Let us show that $z$ is an underestimate of $\sqrt{a} - y$. On the
% one hand, $\sqrt{a} - y \leq 16\cdot 10^{-8}$ because this holds for
% the initial~$y$ and values of~$y$ can only increase. On the other
% hand, the choice of~$j$ implies that $\sqrt{a} - y \leq
% 5(\sqrt{a}+y)(\sqrt{a}-y) = 5(a - y^2) < 10^{9-4j}$. For $j=3$, the
% first bound is better, while for larger~$j$, the second bound is
% better. For all $j\in[3,6]$, we find $\sqrt{a}-y < 16\cdot
% 10^{-2j}$. From this, we deduce that
% \[
% 10^{4j} (\sqrt{a}-y)
% = \frac{10^{4j}\bigl(a-y^2-(\sqrt{a}-y)^2\bigr)}{2y}
% \geq \frac{\bigl\lfloor 10^{4j}(a-y^2)\bigr\rfloor-257}
% {2\cdot 10^{-8} \lfloor 10^{8}y+1\rfloor}
% + \frac{1}{2}
% \]
% where we have replaced the bound $10^{4j}(16\cdot 10^{-2j}) = 256$
% by~$257$ and extracted the corresponding term $1/\bigl(2\cdot
% 10^{-8} \lfloor 10^{8}y+1\rfloor\bigr) \geq 1/2$. Given that
% \eTeX{}'s integer division obeys $[b/c] \leq b/c + 1/2$, we deduce
% that $10^{4j} z \leq 10^{4j} (\sqrt{a}-y)$, hence $y+z\leq\sqrt{a}$
% is an underestimate of~$\sqrt{a}$, as claimed. One implementation
% detail: because the computation involves |-#4*#4| |-| |2*#3*#5| |-|
% |2*#2*#6| which may be as low as $-5\cdot 10^{8}$, we need to use
% the \texttt{pack_big} functions, and the \texttt{big} shifts.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxii_o:NnnnnnnnN #1 #2#3#4#5#6 #7#8#9
{
\exp_after:wN #1
\__int_value:w \__int_eval:w \c_@@_big_leading_shift_int
+ #7 - #2 * #2
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
- 2 * #2 * #3
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
+ #8 - #3 * #3 - 2 * #2 * #4
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
- 2 * #3 * #4 - 2 * #2 * #5
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
+ #9 000 0000 - #4 * #4 - 2 * #3 * #5 - 2 * #2 * #6
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
- 2 * #4 * #5 - 2 * #3 * #6
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w \c_@@_big_middle_shift_int
- #5 * #5 - 2 * #4 * #6
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w
\c_@@_big_middle_shift_int
- 2 * #5 * #6
\exp_after:wN \@@_pack_big:NNNNNNw
\__int_value:w \__int_eval:w
\c_@@_big_trailing_shift_int
- #6 * #6 ;
% (
- 257 ) * 5000 0000 / (#2#3 + 1) + 10 0000 0000 ;
{#2}{#3}{#4}{#5}{#6} {#7}{#8}#9
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]
% {
% \@@_sqrt_auxiii_o:wnnnnnnnn,
% \@@_sqrt_auxiv_o:NNNNNw,
% \@@_sqrt_auxv_o:NNNNNw,
% \@@_sqrt_auxvi_o:NNNNNw,
% \@@_sqrt_auxvii_o:NNNNNw
% }
% We receive here the difference $a-y^2=d=\sum_i d_i \cdot 10^{-4i}$,
% as \meta{d_2} |;| \Arg{d_3} \ldots{} \Arg{d_{10}}, where each block
% has~$4$ digits, except \meta{d_2}. This function finds the largest
% $j\leq 6$ such that $10^{4j}(a-y^2) < 2\cdot 10^{8}$, then leaves an
% open parenthesis and the integer
% $\bigl\lfloor 10^{4j}(a-y^2)\bigr\rfloor$ in an integer
% expression. The closing parenthesis is provided by the caller
% \cs{@@_sqrt_auxii_o:NnnnnnnnN}, which completes the expression
% \[
% 10^{4j} z =
% \Bigl[\bigl(\lfloor 10^{4j}(a-y^2)\rfloor - 257\bigr)
% \cdot (0.5\cdot 10^{8})
% \Bigm/ \lfloor 10^{8} y + 1\rfloor\Bigr]
% \]
% for an estimate of $10^{4j} (\sqrt{a} - y)$. If $d_2\geq 2$, $j=3$
% and the \texttt{auxiv} auxiliary receives $10^{12} z$. If $d_2\leq
% 1$ but $10^{4} d_2 + d_3 \geq 2$, $j=4$ and the \texttt{auxv}
% auxiliary is called, and receives $10^{16} z$, and so on. In all
% those cases, the \texttt{auxviii} auxiliary is set up to add~$z$
% to~$y$, then go back to the \texttt{auxii} step with continuation
% \texttt{auxiii} (the function we are currently describing). The
% maximum value of $j$ is~$6$, regardless of whether $10^{12} d_2 +
% 10^{8} d_3 + 10^{4} d_4 + d_5 \geq 1$. In this last case, we detect
% when $10^{24} z < 10^{7}$, which essentially means $\sqrt{a} - y
% \lesssim 10^{-17}$: once this threshold is reached, there is enough
% information to find the correctly rounded~$\sqrt{a}$ with only one
% more call to \cs{@@_sqrt_auxii_o:NnnnnnnnN}. Note that the
% iteration cannot be stuck before reaching $j=6$, because for $j<6$,
% one has $2\cdot 10^{8}\leq 10^{4(j+1)}(a-y^2)$, hence
% \[
% 10^{4j} z
% \geq \frac{(20000-257)(0.5\cdot 10^{8})}{\lfloor 10^{8} y + 1\rfloor}
% \geq (20000-257)\cdot 0.5 > 0 \,.
% \]
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxiii_o:wnnnnnnnn
#1; #2#3#4#5#6#7#8#9
{
\if_int_compare:w #1 > \c_one
\exp_after:wN \@@_sqrt_auxiv_o:NNNNNw
\__int_value:w \__int_eval:w (#1#2 %)
\else:
\if_int_compare:w #1#2 > \c_one
\exp_after:wN \@@_sqrt_auxv_o:NNNNNw
\__int_value:w \__int_eval:w (#1#2#3 %)
\else:
\if_int_compare:w #1#2#3 > \c_one
\exp_after:wN \@@_sqrt_auxvi_o:NNNNNw
\__int_value:w \__int_eval:w (#1#2#3#4 %)
\else:
\exp_after:wN \@@_sqrt_auxvii_o:NNNNNw
\__int_value:w \__int_eval:w (#1#2#3#4#5 %)
\fi:
\fi:
\fi:
}
\cs_new:Npn \@@_sqrt_auxiv_o:NNNNNw 1#1#2#3#4#5#6;
{ \@@_sqrt_auxviii_o:nnnnnnn {#1#2#3#4#5#6} {00000000} }
\cs_new:Npn \@@_sqrt_auxv_o:NNNNNw 1#1#2#3#4#5#6;
{ \@@_sqrt_auxviii_o:nnnnnnn {000#1#2#3#4#5} {#60000} }
\cs_new:Npn \@@_sqrt_auxvi_o:NNNNNw 1#1#2#3#4#5#6;
{ \@@_sqrt_auxviii_o:nnnnnnn {0000000#1} {#2#3#4#5#6} }
\cs_new:Npn \@@_sqrt_auxvii_o:NNNNNw 1#1#2#3#4#5#6;
{
\if_int_compare:w #1#2 = \c_zero
\exp_after:wN \@@_sqrt_auxx_o:Nnnnnnnn
\fi:
\@@_sqrt_auxviii_o:nnnnnnn {00000000} {000#1#2#3#4#5}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]
% {\@@_sqrt_auxviii_o:nnnnnnn, \@@_sqrt_auxix_o:wnwnw}
% Simply add the two $8$-digit blocks of~$z$, aligned to the last four
% of the five $4$-digit blocks of~$y$, then call the \texttt{auxii}
% auxiliary to evaluate $y'^{2} = (y+z)^{2}$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxviii_o:nnnnnnn #1#2 #3#4#5#6#7
{
\exp_after:wN \@@_sqrt_auxix_o:wnwnw
\__int_value:w \__int_eval:w #3
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w #1 + 1#4#5
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w #2 + 1#6#7 ;
}
\cs_new:Npn \@@_sqrt_auxix_o:wnwnw #1; #2#3; #4#5;
{
\@@_sqrt_auxii_o:NnnnnnnnN
\@@_sqrt_auxiii_o:wnnnnnnnn {#1}{#2}{#3}{#4}{#5}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]
% {\@@_sqrt_auxx_o:Nnnnnnnn, \@@_sqrt_auxxi_o:wwnnN}
% At this stage, $j=6$ and $10^{24} z < 10^{7}$, hence
% \[
% 10^{7} + 1/2 > 10^{24} z + 1/2 \geq
% \bigl(10^{24}(a-y^2) - 258\bigr) \cdot (0.5\cdot 10^{8})
% \Bigm/ (10^{8} y + 1) \,,
% \]
% then $10^{24}(a-y^2) - 258 < 2 (10^{7} + 1/2) (y + 10^{-8})$, and
% \[
% 10^{24}(a-y^2)
% < (10^{7} + 1290.5) (1 + 10^{-8}/y) (2y)
% < (10^{7} + 1290.5) (1 + 10^{-7}) (y + \sqrt{a}) \,,
% \]
% which finally implies $0\leq\sqrt{a}-y < 0.2\cdot 10^{-16}$. In
% particular, $y$~is an underestimate of~$\sqrt{a}$ and $y+0.5\cdot
% 10^{-16}$ is a (strict) overestimate. There is at exactly one
% multiple $m$~of $0.5\cdot 10^{-16}$ in the interval $[y, y+0.5\cdot
% 10^{-16})$. If $m^2>a$, then the square root is inexact and is
% obtained by rounding $m-\epsilon$ to a multiple of $10^{-16}$ (the
% precise shift $0<\epsilon<0.5\cdot 10^{-16}$ is irrelevant for
% rounding). If $m^2=a$ then the square root is exactly~$m$, and
% there is no rounding. If $m^2<a$ then we round $m+\epsilon$. For
% now, discard a few irrelevant arguments |#1|, |#2|, |#3|, and find
% the multiple of $0.5\cdot 10^{-16}$ within $[y, y+0.5\cdot
% 10^{-16})$; rather, only the last $4$~digits |#8| of~$y$ are
% considered, and we do not perform any carry yet. The \texttt{auxxi}
% auxiliary sets up \texttt{auxii} with a continuation function
% \texttt{auxxii} instead of \texttt{auxiii} as before. To prevent
% \texttt{auxii} from giving a negative results $a-m^2$, we compute
% $a+10^{-16}-m^2$ instead, always positive since $m<\sqrt{a}+0.5\cdot
% 10^{-16}$ and $a\leq 1-10^{-16}$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxx_o:Nnnnnnnn #1#2#3 #4#5#6#7#8
{
\exp_after:wN \@@_sqrt_auxxi_o:wwnnN
\__int_value:w \__int_eval:w
(#8 + 2499) / 5000 * 5000 ;
{#4} {#5} {#6} {#7} ;
}
\cs_new:Npn \@@_sqrt_auxxi_o:wwnnN #1; #2; #3#4#5
{
\@@_sqrt_auxii_o:NnnnnnnnN
\@@_sqrt_auxxii_o:nnnnnnnnw
#2 {#1}
{#3} { #4 + \c_one } #5
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]
% {\@@_sqrt_auxxii_o:nnnnnnnnw, \@@_sqrt_auxxiii_o:w}
% The difference $0\leq a+10^{-16}-m^2\leq
% 10^{-16}+(\sqrt{a}-m)(\sqrt{a}+m)\leq 2\cdot 10^{-16}$ was just
% computed: its first $8$~digits vanish, as do the next four,~|#1|,
% and most of the following four,~|#2|. The guess~$m$ is an
% overestimate if $a+10^{-16}-m^2 < 10^{-16}$, that is, |#1#2|
% vanishes. Otherwise it is an underestimate, unless
% $a+10^{-16}-m^2=10^{-16}$ exactly. For an underestimate, call the
% \texttt{auxxiv} function with argument~$9998$. For an exact result
% call it with~$9999$, and for an overestimate call it with~$10000$.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxxii_o:nnnnnnnnw 0; #1#2#3#4#5#6#7#8 #9;
{
\if_int_compare:w #1#2 > \c_zero
\if_int_compare:w #1#2 = \c_one
\if_int_compare:w #3#4 = \c_zero
\if_int_compare:w #5#6 = \c_zero
\if_int_compare:w #7#8 = \c_zero
\@@_sqrt_auxxiii_o:w
\fi:
\fi:
\fi:
\fi:
\exp_after:wN \@@_sqrt_auxxiv_o:wnnnnnnnN
\__int_value:w 9998
\else:
\exp_after:wN \@@_sqrt_auxxiv_o:wnnnnnnnN
\__int_value:w 10000
\fi:
;
}
\cs_new:Npn \@@_sqrt_auxxiii_o:w \fi: \fi: \fi: \fi: #1 \fi: ;
{
\fi: \fi: \fi: \fi: \fi:
\@@_sqrt_auxxiv_o:wnnnnnnnN 9999 ;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, rEXP]{\@@_sqrt_auxxiv_o:wnnnnnnnN}
% This receives $9998$, $9999$ or $10000$ as~|#1| when $m$~is an
% underestimate, exact, or an overestimate, respectively. Then
% comes~$m$ as five blocks of~$4$ digits, but where the last
% block~|#6| may be $0$, $5000$, or~$10000$. In the latter case, we
% need to add a carry, unless $m$~is an overestimate (|#1|~is then
% $10000$). Then comes~$a$ as three arguments. Rounding is done by
% \cs{@@_round:NNN}, whose first argument is the final sign~$0$
% (square roots are positive). We fake its second argument. It
% should be the last digit kept, but this is only used when ties are
% \enquote{rounded to even}, and only when the result is exactly
% half-way between two representable numbers rational square roots of
% numbers with $16$~significant digits have: this situation never
% arises for the square root, as any exact square root of a $16$~digit
% number has at most $8$~significant digits. Finally, the last
% argument is the next digit, possibly shifted by~$1$ when there are
% further nonzero digits. This is achieved by \cs{@@_round_digit:Nw},
% which receives (after removal of the $10000$'s digit) one of $0000$,
% $0001$, $4999$, $5000$, $5001$, or~$9999$, which it converts to $0$,
% $1$, $4$, $5$, $6$, and~$9$, respectively.
% \begin{macrocode}
\cs_new:Npn \@@_sqrt_auxxiv_o:wnnnnnnnN #1; #2#3#4#5#6 #7#8#9
{
\exp_after:wN \@@_basics_pack_high:NNNNNw
\__int_value:w \__int_eval:w 1 0000 0000 + #2#3
\exp_after:wN \@@_basics_pack_low:NNNNNw
\__int_value:w \__int_eval:w 1 0000 0000
+ #4#5
\if_int_compare:w #6 > #1 \exp_stop_f: + \c_one \fi:
+ \exp_after:wN \@@_round:NNN
\exp_after:wN 0
\exp_after:wN 0
\__int_value:w
\exp_after:wN \use_i:nn
\exp_after:wN \@@_round_digit:Nw
\__int_value:w \__int_eval:w #6 + 19999 - #1 ;
\exp_after:wN ;
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Setting the sign}
%
% \begin{macro}[int, EXP]{\@@_set_sign_o:w}
% This function is used for the unary minus and for \texttt{abs}. It
% leaves the sign of \texttt{nan} invariant, turns negative numbers
% (sign~$2$) to positive numbers (sign~$0$) and positive numbers
% (sign~$0$) to positive or negative numbers depending on~|#1|. It
% also expands after itself in the input stream, just like
% \cs{@@_+_o:ww}.
% \begin{macrocode}
\cs_new:Npn \@@_set_sign_o:w #1 \s_@@ \@@_chk:w #2#3#4; @
{
\exp_after:wN \@@_exp_after_o:w
\exp_after:wN \s_@@
\exp_after:wN \@@_chk:w
\exp_after:wN #2
\__int_value:w
\if_case:w #3 \exp_stop_f: #1 \or: 1 \or: 0 \fi: \exp_stop_f:
#4;
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%</initex|package>
% \end{macrocode}
%
% \end{implementation}
%
% \PrintChanges
%
% \PrintIndex
|