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
|
% Copyright 2005-2017 Cisco Systems, Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
\chapter{Numeric Operations\label{CHPTNUMERIC}}
This chapter describes {\ChezScheme} extensions to the standard set of
operations on numbers.
See Chapter~\ref{TSPL:CHPTOBJECTS} of {\TSPLFOUR} or the Revised$^6$ Report
on Scheme for a description of standard operations on numbers.
{\ChezScheme} supports the full set of Scheme numeric datatypes, including
exact and inexact integer, rational, real, and complex numbers.
A variety of representations are used to support these datatypes:
\begin{description}
\item[\index{fixnum}\emph{Fixnums}] represent exact integers in the
fixnum range (see \scheme{most-negative-fixnum} and
\scheme{most-positive-fixnum}).
The length of a string, vector, or fxvector is constrained to be a fixnum.
\item[\index{bignum}\emph{Bignums}] represent arbitrary-precision
exact integers outside of the fixnum range.
\item[\index{ratnum}\emph{Ratnums}] represent arbitrary-precision
exact rational numbers.
Each ratnum contains an exact integer (fixnum
or bignum) numerator and an exact integer denominator.
Ratios are always reduced to lowest terms and never have a denominator
of one or a numerator of zero.
\item[\index{flonum}\emph{Flonums}] represent inexact real numbers.
Flonums are IEEE 64-bit floating-point numbers.
(Since flonums cannot represent irrational numbers, all inexact real
numbers are actually rational, although they may approximate irrational
quantities.)
\item[\index{exact complexnum}\emph{Exact complexnums}]
represent exact complex numbers.
Each exact complexnum contains an exact rational (fixnum, bignum, or
ratnum) real part and an exact rational imaginary part.
\item[\index{inexact complexnum}\emph{Inexact complexnums}]
represent inexact complex numbers.
Each inexact complexnum contains a flonum real part and a flonum imaginary part.
\end{description}
\noindent
Most numbers can be represented in only one way; however, real numbers
are sometimes represented as inexact complex numbers with imaginary
component equal to zero.
{\ChezScheme} extends the syntax of numbers with arbitrary radixes from
two through 36, nondecimal floating-point and scientific notation,
and printed representations for
IEEE infinities and NANs. (NAN stands for ``not-a-number.'')
Arbitrary radixes are specified with the prefix \scheme{#\var{n}r}, where
\var{n} ranges from 2 through 36.
Digits beyond 9 are specified with the letters (in either
upper or lower case) \scheme{a} through \scheme{z}.
For example, \scheme{#2r101} is $5_{10}$, and
\scheme{#36rZ} is $35_{10}$.
For higher radixes, an ambiguity arises between the interpretation of
certain letters, e.g., \scheme{e}, as digits or exponent specifiers; in
such cases, the letter is assumed to be a digit.
For example, the \scheme{e} in \scheme{#x3.2e5} is interpreted as a
digit, not as an exponent marker, whereas in \scheme{3.2e5} it is
treated as an exponent marker.
IEEE infinities are printed as \scheme{+inf.0} and \scheme{-inf.0},
while IEEE NANs are printed as \scheme{+nan.0} or \scheme{-nan.0}.
(+nan.0 is used on output for all NANs.)
\schemedisplay
(/ 1.0 0.0) ;=> +inf.0
(/ 1.0 -0.0) ;=> -inf.0
(/ 0.0 0.0) ;=> +nan.0
(/ +inf.0 -inf.0) ;=> +nan.0
\endschemedisplay
The first section of this chapter describes type-specific numeric type
predicates.
Sections~\ref{SECTNUMERICFIXNUM} through~\ref{SECTNUMERICCOMPLEXNUM}
describe fast, type-specific
numeric operations on fixnums, flonums, and inexact complex numbers
(flonums and/or inexact complexnums).
The fixnum-specific versions should be used only when the programmer
is certain that the operands and results (where appropriate) will be
fixnums, i.e., integers in the range \scheme{(most-negative-fixnum)} to
\scheme{(most-positive-fixnum)}, inclusive.
The flonum-specific versions should be used only when the
inputs and outputs (where appropriate) are certain to be flonums.
The mixed flonum/complexnum versions should be used only when the
inputs are certain to be either flonums or inexact complexnums.
Section~\ref{SECTNUMERICLOGICAL} describes operations, both
arbitrary precision and fixnum-specific, that allow
exact integers to be treated as sets or sequences of bits.
Random number generation is covered Section~\ref{SECTNUMERICRANDOM},
and miscellaneous numeric operations are covered in the
Section~\ref{SECTNUMERICMISC}.
\section{Numeric Type Predicates}
\index{fixnum}\index{flonum}\index{bignum}\index{ratnum}\index{cflonum}%
The Revised$^6$ Report distinguishes two types of special numeric objects:
fixnums and flonums.
{\ChezScheme} additionally distinguishes \emph{bignums} (exact integers outside
of the fixnum range) and \emph{ratnums} (ratios of exact integers).
It also provides a predicate for recognizing \emph{cflonums}, which are
flonums or inexact complex numbers.
%----------------------------------------------------------------------------
\entryheader
\formdef{bignum?}{\categoryprocedure}{(bignum? \var{obj})}
\returns \scheme{#t} if \var{obj} is a bignum, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noskip\schemedisplay
(bignum? 0) ;=> #f
(bignum? (most-positive-fixnum)) ;=> #f
(bignum? (most-negative-fixnum)) ;=> #f
(bignum? (* (most-positive-fixnum) 2)) ;=> #t
(bignum? 3/4) ;=> #f
(bignum? 'a) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{ratnum?}{\categoryprocedure}{(ratnum? \var{obj})}
\returns \scheme{#t} if \var{obj} is a ratnum, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noskip\schemedisplay
(ratnum? 0) ;=> #f
(ratnum? (* (most-positive-fixnum) 2)) ;=> #f
(ratnum? 3/4) ;=> #t
(ratnum? -10/2) ;=> #f
(ratnum? -11/2) ;=> #t
(ratnum? 'a) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cflonum?}{\categoryprocedure}{(cflonum? \var{obj})}
\returns \scheme{#t} if \var{obj} is an inexact complexnum or flonum, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noskip\schemedisplay
(cflonum? 0) ;=> #f
(cflonum? 0.0) ;=> #t
(cflonum? 3+4i) ;=> #f
(cflonum? 3.0+4i) ;=> #t
(cflonum? +i) ;=> #f
(cflonum? +1.0i) ;=> #t
\endschemedisplay
\section{Fixnum Operations\label{SECTNUMERICFIXNUM}}
Fixnum-specific procedures normally check their inputs and outputs (where
appropriate), but at optimization level 3 the compiler generates, in most
cases, code that does not perform these checks.
%----------------------------------------------------------------------------
\entryheader
\formdef{most-positive-fixnum}{\categoryprocedure}{(most-positive-fixnum)}
\returns the most positive fixnum supported by the system
\formdef{most-negative-fixnum}{\categoryprocedure}{(most-negative-fixnum)}
\returns the most negative fixnum supported by the system
\listlibraries
\endentryheader
\noindent
These procedures are identical to the Revised$^6$ Report
\scheme{greatest-fixnum} and \scheme{least-fixnum} procedures.
%----------------------------------------------------------------------------
\entryheader
\formdef{fx=}{\categoryprocedure}{(fx= \var{fixnum_1} \var{fixnum_2} \dots)}
\formdef{fx<}{\categoryprocedure}{(fx< \var{fixnum_1} \var{fixnum_2} \dots)}
\formdef{fx>}{\categoryprocedure}{(fx> \var{fixnum_1} \var{fixnum_2} \dots)}
\formdef{fx<=}{\categoryprocedure}{(fx<= \var{fixnum_1} \var{fixnum_2} \dots)}
\formdef{fx>=}{\categoryprocedure}{(fx>= \var{fixnum_1} \var{fixnum_2} \dots)}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
The predicate \scheme{fx=} returns \scheme{#t} if its arguments are equal.
The predicate \scheme{fx<} returns \scheme{#t} if its arguments are monotonically
increasing, i.e., each argument is greater than the preceding ones,
while \scheme{fx>} returns \scheme{#t} if its arguments are monotonically decreasing.
The predicate \scheme{fx<=} returns \scheme{#t} if its arguments are monotonically
nondecreasing, i.e., each argument is not less than the preceding ones,
while \scheme{fx>=} returns \scheme{#t} if its arguments are monotonically nonincreasing.
When passed only one argument, each of these predicates returns \scheme{#t}.
These procedures are similar to the Revised$^6$ Report procedures
\scheme{fx=?}, \scheme{fx<?}, \scheme{fx>?}, \scheme{fx<=?},
and \scheme{fx>=?} except that the Revised$^6$ Report procedures
require two or more arguments, and their names have the ``\scheme{?}''
suffix.
\schemedisplay
(fx= 0) ;=> #t
(fx= 0 0) ;=> #t
(fx< (most-negative-fixnum) 0 (most-positive-fixnum)) ;=> #t
(let ([x 3]) (fx<= 0 x 9)) ;=> #t
(fx<= 0 3 3) ;=> #t
(fx>= 0 0 (most-negative-fixnum)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxnonpositive?}{\categoryprocedure}{(fxnonpositive? \var{fixnum})}
\returns \scheme{#t} if \var{fixnum} is not greater than zero, \scheme{#f} otherwise
\formdef{fxnonnegative?}{\categoryprocedure}{(fxnonnegative? \var{fixnum})}
\returns \scheme{#t} if \var{fixnum} is not less than zero, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
\scheme{fxnonpositive?} is equivalent to \scheme{(lambda (x) (fx<= x 0))},
and
\scheme{fxnonnegative?} is equivalent to \scheme{(lambda (x) (fx>= x 0))}.
\schemedisplay
(fxnonpositive? 128) ;=> #f
(fxnonpositive? 0) ;=> #t
(fxnonpositive? -1) ;=> #t
(fxnonnegative? -65) ;=> #f
(fxnonnegative? 0) ;=> #t
(fxnonnegative? 1) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fx+}{\categoryprocedure}{(fx+ \var{fixnum} \dots)}
\returns the sum of the arguments \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noindent
When called with no arguments, \scheme{fx+} returns \scheme{0}.
\schemedisplay
(fx+) ;=> 0
(fx+ 1 2) ;=> 3
(fx+ 3 4 5) ;=> 12
(apply fx+ '(1 2 3 4 5)) ;=> 15
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fx-}{\categoryprocedure}{(fx- \var{fixnum_1} \var{fixnum_2} \dots)}
\returns a fixnum
\listlibraries
\endentryheader
\noindent
When called with one argument, \scheme{fx-} returns the negative of \var{fixnum_1}.
Thus, \scheme{(fx- \var{fixnum_1})} is an idiom for \scheme{(fx- 0 \var{fixnum_1})}.
When called with two or more arguments, \scheme{fx-} returns the result of
subtracting the sum of the numbers \scheme{\var{fixnum_2} \dots} from
\var{fixnum_1}.
\schemedisplay
(fx- 3) ;=> -3
(fx- 4 3) ;=> 1
(fx- 4 3 2 1) ;=> -2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fx*}{\categoryprocedure}{(fx* \var{fixnum} \dots)}
\returns the product of the arguments \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noindent
When called with no arguments, \scheme{fx*} returns \scheme{1}.
\schemedisplay
(fx*) ;=> 1
(fx* 1 2) ;=> 2
(fx* 3 -4 5) ;=> -60
(apply fx* '(1 -2 3 -4 5)) ;=> 120
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fx/}{\categoryprocedure}{(fx/ \var{fixnum_1} \var{fixnum_2} \dots)}
\returns see explanation
\listlibraries
\endentryheader
\noindent
When called with one argument, \scheme{fx/} returns the reciprocal
of \var{fixnum_1}.
That is, \scheme{(fx/ \var{fixnum_1})} is an idiom for
\scheme{(fx/ 1 \var{fixnum_1})}.
When called with two or more arguments, \scheme{fx/} returns
the result of
dividing \var{fixnum_1} by the product of the remaining arguments
\scheme{\var{fixnum_2} \dots}.
\schemedisplay
(fx/ 1) ;=> 1
(fx/ -17) ;=> 0
(fx/ 8 -2) ;=> -4
(fx/ -9 2) ;=> -4
(fx/ 60 5 3 2) ;=> 2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fx1+}{\categoryprocedure}{(fx1+ \var{fixnum})}
\formdef{fx1-}{\categoryprocedure}{(fx1- \var{fixnum})}
\returns \var{fixnum} plus 1 or \var{fixnum} minus 1
\listlibraries
\endentryheader
\schemedisplay
(define fxplus
(lambda (x y)
(if (fxzero? x)
y
(fxplus (fx1- x) (fx1+ y)))))
(fxplus 7 8) ;=> 15
\endschemedisplay
\noindent
\scheme{fx1+} and \scheme{fx1-} can be defined as follows:
\schemedisplay
(define fx1+ (lambda (x) (fx+ x 1)))
(define fx1- (lambda (x) (fx- x 1)))
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxquotient}{\categoryprocedure}{(fxquotient \var{fixnum_1} \var{fixnum_2} \dots)}
\returns see explanation
\listlibraries
\endentryheader
\noindent
\scheme{fxquotient} is identical to \scheme{fx/}.
See the description of \scheme{fx/} above.
%----------------------------------------------------------------------------
\entryheader
\formdef{fxremainder}{\categoryprocedure}{(fxremainder \var{fixnum_1} \var{fixnum_2})}
\returns the fixnum remainder of \var{fixnum_1} divided by \var{fixnum_2}
\listlibraries
\endentryheader
\noindent
The result of \scheme{fxremainder} has the same sign as \var{fixnum_1}.
\schemedisplay
(fxremainder 16 4) ;=> 0
(fxremainder 5 2) ;=> 1
(fxremainder -45 7) ;=> -3
(fxremainder 10 -3) ;=> 1
(fxremainder -17 -9) ;=> -8
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxmodulo}{\categoryprocedure}{(fxmodulo \var{fixnum_1} \var{fixnum_2})}
\returns the fixnum modulus of \var{fixnum_1} and \var{fixnum_2}
\listlibraries
\endentryheader
\noindent
The result of \scheme{fxmodulo} has the same sign as \var{fixnum_2}.
\schemedisplay
(fxmodulo 16 4) ;=> 0
(fxmodulo 5 2) ;=> 1
(fxmodulo -45 7) ;=> 4
(fxmodulo 10 -3) ;=> -2
(fxmodulo -17 -9) ;=> -8
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxabs}{\categoryprocedure}{(fxabs \var{fixnum})}
\returns the absolute value of \var{fixnum}
\listlibraries
\endentryheader
\noskip\schemedisplay
(fxabs 1) ;=> 1
(fxabs -1) ;=> 1
(fxabs 0) ;=> 0
\endschemedisplay
\section{Flonum Operations\label{SECTNUMERICFLONUM}}
Inexact real numbers are normally represented by \var{flonums}.
A flonum is a single 64-bit double-precision floating point
number.
This section describes operations on flonums, most of which accept
flonum arguments and return flonum values.
In most cases, the operations are inline-coded or coded as machine
language subroutines at optimize-level~3 with
no argument type checking; full type checking is performed at lower
optimize levels.
Flonum-specific procedure names begin with the prefix ``\scheme{fl}'' to
set them apart from their generic counterparts.
Inexact real numbers may also be represented by inexact complexnums
with imaginary parts equal to zero, which cannot be used as input
to the flonum-specific operators.
Such numbers are produced, however, only from operations involving
complex numbers with nonzero imaginary parts, by explicit calls
to \scheme{fl-make-rectangular}, \scheme{make-rectangular}, or
\scheme{make-polar}, or by numeric input in either polar or rectangular
format.
%----------------------------------------------------------------------------
\entryheader
\formdef{flonum->fixnum}{\categoryprocedure}{(flonum->fixnum \var{flonum})}
\returns the fixnum representation of \var{flonum}, truncated
\listlibraries
\endentryheader
\noindent
The truncated value of \var{flonum} must fall within the fixnum range.
\scheme{flonum->fixnum} is a restricted version of
\index{\scheme{exact}}\scheme{exact},
which converts any numeric representation
to its exact equivalent.
\schemedisplay
(flonum->fixnum 0.0) ;=> 0
(flonum->fixnum 3.9) ;=> 3
(flonum->fixnum -2.2) ;=> -2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fl=}{\categoryprocedure}{(fl= \var{flonum_1} \var{flonum_2} \dots)}
\formdef{fl<}{\categoryprocedure}{(fl< \var{flonum_1} \var{flonum_2} \dots)}
\formdef{fl>}{\categoryprocedure}{(fl> \var{flonum_1} \var{flonum_2} \dots)}
\formdef{fl<=}{\categoryprocedure}{(fl<= \var{flonum_1} \var{flonum_2} \dots)}
\formdef{fl>=}{\categoryprocedure}{(fl>= \var{flonum_1} \var{flonum_2} \dots)}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
The predicate \scheme{fl=} returns \scheme{#t} if its arguments are equal.
The predicate \scheme{fl<} returns \scheme{#t} if its arguments are monotonically
increasing, i.e., each argument is greater than the preceding ones,
while \scheme{fl>} returns \scheme{#t} if its arguments are monotonically decreasing.
The predicate \scheme{fl<=} returns \scheme{#t} if its arguments are monotonically
nondecreasing, i.e., each argument is not less than the preceding ones,
while \scheme{fl>=} returns \scheme{#t} if its arguments are monotonically nonincreasing.
When passed only one argument, each of these predicates returns \scheme{#t}.
IEEE NANs are not comparable, i.e., comparisons involving NANs always return
\scheme{#f}.
These procedures are similar to the Revised$^6$ Report procedures
\scheme{fl=?}, \scheme{fl<?}, \scheme{fl>?}, \scheme{fl<=?},
and \scheme{fl>=?} except that the Revised$^6$ Report procedures
require two or more arguments, and their names have the ``\scheme{?}''
suffix.
\schemedisplay
(fl= 0.0) ;=> #t
(fl= 0.0 0.0) ;=> #t
(fl< -1.0 0.0 1.0) ;=> #t
(fl> -1.0 0.0 1.0) ;=> #f
(fl<= 0.0 3.0 3.0) ;=> #t
(fl>= 4.0 3.0 3.0) ;=> #t
(fl< 7.0 +inf.0) ;=> #t
(fl= +nan.0 0.0) ;=> #f
(fl= +nan.0 +nan.0) ;=> #f
(fl< +nan.0 +nan.0) ;=> #f
(fl> +nan.0 +nan.0) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{flnonpositive?}{\categoryprocedure}{(flnonpositive? \var{fl})}
\returns \scheme{#t} if \var{fl} is not greater than zero, \scheme{#f} otherwise
\formdef{flnonnegative?}{\categoryprocedure}{(flnonnegative? \var{fl})}
\returns \scheme{#t} if \var{fl} is not less than zero, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
\scheme{flnonpositive?} is equivalent to \scheme{(lambda (x) (fl<= x 0.0))},
and
\scheme{flnonnegative?} is equivalent to \scheme{(lambda (x) (fl>= x 0.0))}.
\noindent
Even if the flonum representation distinguishes -0.0 from +0.0, both
are considered nonpositive and nonnegative.
\schemedisplay
(flnonpositive? 128.0) ;=> #f
(flnonpositive? 0.0) ;=> #t
(flnonpositive? -0.0) ;=> #t
(flnonpositive? -1.0) ;=> #t
(flnonnegative? -65.0) ;=> #f
(flnonnegative? 0.0) ;=> #t
(flnonnegative? -0.0) ;=> #t
(flnonnegative? 1.0) ;=> #t
(flnonnegative? +nan.0) ;=> #f
(flnonpositive? +nan.0) ;=> #f
(flnonnegative? +inf.0) ;=> #t
(flnonnegative? -inf.0) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{decode-float}{\categoryprocedure}{(decode-float \var{x})}
\returns see below
\listlibraries
\endentryheader
\noindent
\var{x} must be a flonum.
\scheme{decode-float} returns a vector with three integer elements,
\var{m}, \var{e}, and \var{s}, such that
$x = sm2^e$.
It is useful primarily in the printing of floating-point numbers.
\schemedisplay
(decode-float 1.0) ;=> #(4503599627370496 -52 1)
(decode-float -1.0) ;=> #(4503599627370496 -52 -1)
(define slow-identity
(lambda (x)
(inexact
(let ([v (decode-float x)])
(let ([m (vector-ref v 0)]
[e (vector-ref v 1)]
[s (vector-ref v 2)])
(* s m (expt 2 e)))))))
(slow-identity 1.0) ;=> 1.0
(slow-identity -1e20) ;=> -1e20
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fllp}{\categoryprocedure}{(fllp \var{flonum})}
\returns see below
\listlibraries
\endentryheader
\noindent
\scheme{fllp} returns the 12-bit integer consisting of the exponent
plus highest order represented bit of a flonum (ieee 64-bit
floating-point number).
It can be used to compute a fast approximation of the logarithm of
the number.
\schemedisplay
(fllp 0.0) ;=> 0
(fllp 1.0) ;=> 2046
(fllp -1.0) ;=> 2046
(fllp 1.5) ;=> 2047
(fllp +inf.0) ;=> 4094
(fllp -inf.0) ;=> 4094
(fllp #b1.0e-1111111111) ;=> 1
(fllp #b1.0e-10000000000) ;=> 0
\endschemedisplay
\section{Inexact Complex Operations\label{SECTNUMERICCOMPLEXNUM}}
The procedures described in this section provide mechanisms for
creating and operating on inexact complex numbers.
Inexact complex numbers with nonzero imaginary parts are represented as
\emph{inexact complexnums}.
An inexact complexnum contains two 64-bit double-precision floating point
numbers.
Inexact \index{imaginary numbers}\index{complex numbers}complex numbers
with imaginary parts equal to zero (in other words, inexact real numbers)
may be represented as either inexact complexnums or flonums.
The operations described in this section accept any mix of
inexact complexnum and flonum arguments
(collectively, ``\index{cflonums}cflonums'').
In most cases, the operations are performed with minimal type checking
at optimize-level 3; full type checking is performed at lower optimize
levels.
Inexact complex procedure names begin with the prefix ``\scheme{cfl}''
to set them apart from their generic counterparts.
%----------------------------------------------------------------------------
\entryheader
\formdef{fl-make-rectangular}{\categoryprocedure}{(fl-make-rectangular \var{flonum_1} \var{flonum_2})}
\returns an inexact complexnum
\listlibraries
\endentryheader
\noindent
The inexact complexnum produced by fl-make-rectangular has real part equal
to \var{flonum_1} and imaginary part equal to \var{flonum_2}.
\schemedisplay
(fl-make-rectangular 2.0 -3.0) ;=> 2.0-3.0i
(fl-make-rectangular 2.0 0.0) ;=> 2.0+0.0i
(fl-make-rectangular 2.0 -0.0) ;=> 2.0-0.0i
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cfl-real-part}{\categoryprocedure}{(cfl-real-part \var{cflonum})}
\returns the real part of \var{cflonum}
\formdef{cfl-imag-part}{\categoryprocedure}{(cfl-imag-part \var{cflonum})}
\returns the imaginary part of \var{cflonum}
\listlibraries
\endentryheader
%\noindent
%Due to the flonum and inexact complexnum representations employed by
%{\ChezScheme}, these operations require no memory
%references and no heap allocation.
\schemedisplay
(cfl-real-part 2.0-3.0i) ;=> 2.0
(cfl-imag-part 2.0-3.0i) ;=> -3.0
(cfl-imag-part 2.0-0.0i) ;=> -0.0
(cfl-imag-part 2.0-inf.0i) ;=> -inf.0
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cfl=}{\categoryprocedure}{(cfl= \var{cflonum} \dots)}
\returns \scheme{#t} if its arguments are equal, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noskip\schemedisplay
(cfl= 7.0+0.0i 7.0) ;=> #t
(cfl= 1.0+2.0i 1.0+2.0i) ;=> #t
(cfl= 1.0+2.0i 1.0-2.0i) ;=> #f
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cfl+}{\categoryprocedure}{(cfl+ \var{cflonum} \dots)}
\formdef{cfl*}{\categoryprocedure}{(cfl* \var{cflonum} \dots)}
\formdef{cfl-}{\categoryprocedure}{(cfl- \var{cflonum_1} \var{cflonum_2} \dots)}
\formdef{cfl/}{\categoryprocedure}{(cfl/ \var{cflonum_1} \var{cflonum_2} \dots)}
\returns a cflonum
\listlibraries
\endentryheader
\noindent
These procedures compute the sum, difference, product, or quotient
of inexact complex quantities, whether these quantities are represented
by flonums or inexact complexnums.
For example, if \scheme{cfl+} receives two flonum arguments $a$ and $b$, it
returns the sum $a+b$; in this case, it behaves the same as \scheme{fl+}.
With two inexact complexnum arguments $a+bi$ and $c+di$, it returns
the sum $(a+c)+(b+d)i$.
If one argument is a flonum $a$ and the other an inexact complexnum
$c+di$, \scheme{cfl+} returns $(a+c)+di$.
When passed zero arguments, \scheme{cfl+} returns 0.0 and
\scheme{cfl*} returns 1.0.
When passed one argument, \scheme{cfl-} returns the additive inverse
of the argument, and \scheme{cfl/} returns the multiplicative inverse
of the argument.
When passed three or more arguments, \scheme{cfl-} returns the
difference between its first and the sum of its remaining arguments,
and \scheme{cfl/} returns the quotient of its first and the product
of its remaining arguments.
%On machines supporting the IEEE Standard for Floating Point Arithmetic
%\cite{IEEEFLOAT},
%adding a flonum $a$ to a complexnum $c+di$ is not always the same
%as adding the complexnum $a+0.0i$ to $c+di$.
%The counter example is when $d=-0.0$, in which case the former leads
%to $(a+c)-0.0i$ while the latter leads to $(a+c)+0.0i$.
%{\ChezScheme} performs the former operation under the assumption that
%the imaginary part of a flonum representing a complex quantity always
%has an exact zero imaginary part.
%We do not treat flonums $a$ as if they were equivalent to $a+0.0i$.
%Although this would seem to simplify the semantics slightly, it leads
%to unfortunate consistency problems.
%For example, assuming that we do want to treat flonums $a$ as
%equivalent to $a+0.0i$, the product $ab$ of two flonums $a$ and $b$
%would presumably be equivalent $ab+0.0i$.
%However, if $a$ and $b$ are negative, the product of $a+0.0i$ and
%$b+0.0i$ is actually $ab-0.0i$, not $ab+0.0i$.
%Unfortunately, {\ChezScheme} currently represents imaginary numbers
%as complexnums, so while 1.0 is assumed to have an exact
%zero imaginary part, +1.0i is arbitrarily assigned a real part of
%+0.0.
\schemedisplay
(cfl+) ;=> 0.0
(cfl*) ;=> 1.0
(cfl- 5.0+1.0i) ;=> -5.0-1.0i
(cfl/ 2.0+2.0i) ;=> 0.25-0.25i
(cfl+ 1.0+2.2i -3.7+5.3i) ;=> -2.7+7.5i
(cfl+ 1.0 -5.3) ;=> -4.3
(cfl+ 1.0 2.0 -5.3i) ;=> 3.0-5.3i
(cfl- 1.0+2.5i -3.7) ;=> 4.7+2.5i
(cfl* 1.0+2.0i 3.0+4.0i) ;=> -5.0+10.0i
(cfl/ -5.0+10.0i 1.0+2.0i 2.0) ;=> 1.5+2.0i
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cfl-conjugate}{\categoryprocedure}{(cfl-conjugate \var{cflonum})}
\returns complex conjugate of \var{cflonum}
\listlibraries
\endentryheader
\noindent
The procedure \scheme{cfl-conjugate}, when passed an inexact complex argument
$a + bi$, returns its complex conjugate $a + (-b)i$.
See also \index{\scheme{conjugate}}\scheme{conjugate}, which is a generic
version of this operator that returns the complex conjugate of any
valid representation for a complex number.
\schemedisplay
(cfl-conjugate 3.0) ;=> 3.0
(cfl-conjugate 3.0+4.0i) ;=> 3.0-4.0i
(cfl-conjugate 1e-20-2e-30i) ;=> 1e-20+2e-30i
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{cfl-magnitude-squared}{\categoryprocedure}{(cfl-magnitude-squared \var{cflonum})}
\returns magnitude of \var{cflonum} squared
\listlibraries
\endentryheader
\noindent
The procedure \scheme{cfl-magnitude-squared}, when passed an inexact complex
argument $a + bi$ returns a flonum representing the magnitude of the
argument squared, i.e., $a^2 + b^2$.
See also \index{\scheme{magnitude-squared}}\scheme{magnitude-squared},
which is a generic version of this
operator that returns the magnitude squared of any valid representation
for a complex number.
Both operations are similar to the \index{\scheme{magnitude}}\scheme{magnitude} procedure,
which returns the magnitude, $sqrt(a^2 + b^2)$, of its generic complex
argument.
\schemedisplay
(cfl-magnitude-squared 3.0) ;=> 9.0
(cfl-magnitude-squared 3.0-4.0i) ;=> 25.0
\endschemedisplay
\section{Bitwise and Logical Operators\label{SECTNUMERICLOGICAL}}
{\ChezScheme} provides a set of logical operators that allow exact
integers (fixnums and bignums) to be treated as sets or sequences
of bits.
These operators include
\scheme{logand} (bitwise logical \scheme{and}),
\scheme{logior} (bitwise logical \scheme{or}),
\scheme{logxor} (bitwise logical exclusive \scheme{or}),
\scheme{lognot} (bitwise logical \scheme{not}),
\scheme{logtest} (test multiple bits),
\scheme{logbit?} (test single bit),
\scheme{logbit0} (reset single bit),
\scheme{logbit1} (set single bit),
and \scheme{ash} (arithmetic shift).
Each of these operators treats its arguments as two's complement integers,
regardless of the underlying representation.
This treatment can be exploited to represent infinite sets:
a negative number represents an infinite number of one bits beyond the
leftmost zero, and a nonnegative number represents an infinite number of zero
bits beyond the leftmost one bit.
Fixnum equivalents of the logical operators are provided, as
\scheme{fxlogand}, \scheme{fxlogior}, \scheme{fxlogxor},
\scheme{fxlognot}, \scheme{fxlogtest}, \scheme{fxlogbit?},
\scheme{fxlogbit0}, and \scheme{fxlogbit1}.
Three separate fixnum operators are provided for shifting:
\scheme{fxsll} (shift-left logical),
\scheme{fxsrl} (shift-right logical),
\scheme{fxsra} (shift-right arithmetic).
Logical and arithmetic shifts differ only for right shifts.
Shift-right logical shifts in zero bits on the left end, and shift-right
arithmetic replicates the sign bit.
Logical shifts do not make sense for arbitrary-precision integers,
since these have no ``left end'' into which bits must be shifted.
%----------------------------------------------------------------------------
\entryheader
\formdef{logand}{\categoryprocedure}{(logand \var{int} \dots)}
\returns the logical ``and'' of the arguments \scheme{\var{int} \dots}
\listlibraries
\endentryheader
\noindent
The arguments must be exact integers (fixnums or bignums) and are treated
as two's complement integers, regardless of the underlying representation.
With no arguments, \scheme{logand} returns -1, i.e., all bits set.
\schemedisplay
(logand) ;=> -1
(logand 15) ;=> 15
(logand -1 -1) ;=> -1
(logand -1 0) ;=> 0
(logand 5 3) ;=> 1
(logand #x173C8D95 7) ;=> 5
(logand #x173C8D95 -8) ;=> #x173C8D90
(logand #b1100 #b1111 #b1101) ;=> #b1100
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logior}{\categoryprocedure}{(logior \var{int} \dots)}
\formdef{logor}{\categoryprocedure}{(logor \var{int} \dots)}
\returns the logical ``or'' of the arguments \scheme{\var{int} \dots}
\listlibraries
\endentryheader
\noindent
The arguments must be exact integers (fixnums or bignums) and are treated
as two's complement integers, regardless of the underlying representation.
With no arguments, \scheme{logior} returns 0, i.e., all bits reset.
\schemedisplay
(logior) ;=> 0
(logior 15) ;=> 15
(logior -1 -1) ;=> -1
(logior -1 0) ;=> -1
(logior 5 3) ;=> 7
(logior #b111000 #b101010) ;=> #b111010
(logior #b1000 #b0100 #b0010) ;=> #b1110
(apply logior '(1 2 4 8 16)) ;=> 31
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logxor}{\categoryprocedure}{(logxor \var{int} \dots)}
\returns the logical ``exclusive or'' of the arguments \scheme{\var{int} \dots}
\listlibraries
\endentryheader
\noindent
The arguments must be exact integers (fixnums or bignums) and are treated
as two's complement integers, regardless of the underlying representation.
With no arguments, \scheme{logxor} returns 0, i.e., all bits reset.
\schemedisplay
(logxor) ;=> 0
(logxor 15) ;=> 15
(logxor -1 -1) ;=> 0
(logxor -1 0) ;=> -1
(logxor 5 3) ;=> 6
(logxor #b111000 #b101010) ;=> #b010010
(logxor #b1100 #b0100 #b0110) ;=> #b1110
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{lognot}{\categoryprocedure}{(lognot \var{int})}
\returns the logical ``not'' of \var{int}
\listlibraries
\endentryheader
\noindent
The argument must be an exact integer (fixnum or bignum) and is treated
as a two's complement integer, regardless of the underlying representation.
\schemedisplay
(lognot -1) ;=> 0
(lognot 0) ;=> -1
(lognot 7) ;=> -8
(lognot -8) ;=> 7
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logbit?}{\categoryprocedure}{(logbit? \var{index} \var{int})}
\returns \scheme{#t} if the specified bit is set, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noindent
\var{index} must be a nonnegative exact integer.
\var{int} must be an exact integer (fixnum or bignum) and is treated
as a two's complement integer, regardless of the underlying representation.
\scheme{logbit?} returns \scheme{#t} if the bit at index \var{index}
of \var{int} is set (one) and \scheme{#f} otherwise.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
There is no upper limit on the index; for nonnegative values of \var{int},
the bits above the highest order set bit are all considered to be zero,
and for negative values, the bits above the highest order reset bit are
all considered to be one.
\scheme{logbit?} is equivalent to
\schemedisplay
(lambda (k n) (not (zero? (logand n (ash 1 k)))))
\endschemedisplay
but more efficient.
\schemedisplay
(logbit? 0 #b1110) ;=> #f
(logbit? 1 #b1110) ;=> #t
(logbit? 2 #b1110) ;=> #t
(logbit? 3 #b1110) ;=> #t
(logbit? 4 #b1110) ;=> #f
(logbit? 100 #b1110) ;=> #f
(logbit? 0 -6) ;=> #f ; \var{the two's complement of} -6 \var{is} 1...1010
(logbit? 1 -6) ;=> #t
(logbit? 2 -6) ;=> #f
(logbit? 3 -6) ;=> #t
(logbit? 100 -6) ;=> #t
(logbit? (random 1000000) 0) ;=> #f
(logbit? (random 1000000) -1) ;=> #t
(logbit? 20000 (ash 1 20000)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logtest}{\categoryprocedure}{(logtest \var{int_1} \var{int_2})}
\returns \scheme{#t} if any common bits are set, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noindent
The arguments must be exact integers (fixnums or bignums) and are treated
as two's complement integers, regardless of the underlying representation.
\scheme{logtest} returns \scheme{#t} if any bit set in one argument is
also set in the other.
It returns \scheme{#f} if the two arguments have no set bits in common.
\scheme{logtest} is equivalent to
\schemedisplay
(lambda (n1 n2) (not (zero? (logand n1 n2))))
\endschemedisplay
but more efficient.
\schemedisplay
(logtest #b10001 #b1110) ;=> #f
(logtest #b10101 #b1110) ;=> #t
(logtest #b111000 #b110111) ;=> #t
(logtest #b101 -6) ;=> #f ; \var{the two's complement of} -6 \var{is} 1...1010
(logtest #b1000 -6) ;=> #t
(logtest 100 -6) ;=> #t
(logtest (+ (random 1000000) 1) 0) ;=> #f
(logtest (+ (random 1000000) 1) -1) ;=> #t
(logtest (ash #b101 20000) (ash #b111 20000)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logbit0}{\categoryprocedure}{(logbit0 \var{index} \var{int})}
\returns the result of clearing bit \var{index} of \var{int}
\listlibraries
\endentryheader
\noindent
\var{index} must be a nonnegative exact integer.
\var{int} must be an exact integer (fixnum or bignum) and is treated
as a two's complement integer, regardless of the underlying representation.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
As with \scheme{logbit?}, there is no upper limit on the index.
\scheme{logbit0} is equivalent to
\schemedisplay
(lambda (i n) (logand (lognot (ash 1 i)) n))
\endschemedisplay
but more efficient.
\schemedisplay
(logbit0 3 #b10101010) ;=> #b10100010
(logbit0 4 #b10101010) ;=> #b10101010
(logbit0 0 -1) ;=> -2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{logbit1}{\categoryprocedure}{(logbit1 \var{index} \var{int})}
\returns the result of setting bit \var{index} of \var{int}
\listlibraries
\endentryheader
\noindent
\var{index} must be a nonnegative exact integer.
\var{int} must be an exact integer (fixnum or bignum) and is treated
as a two's complement integer, regardless of the underlying representation.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
As with \scheme{logbit?}, there is no upper limit on the index.
\scheme{logbit1} is equivalent to
\schemedisplay
(lambda (i n) (logor (ash 1 i) n))
\endschemedisplay
but more efficient.
\schemedisplay
(logbit1 3 #b10101010) ;=> #b10101010
(logbit1 4 #b10101010) ;=> #b10111010
(logbit1 4 0) ;=> #b10000
(logbit1 0 -2) ;=> -1
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{ash}{\categoryprocedure}{(ash \var{int} \var{count})}
\returns \var{int} shifted left arithmetically by \var{count}.
\listlibraries
\endentryheader
\noindent
Both arguments must be exact integers.
The first argument is treated as a two's complement integer, regardless
of the underlying representation.
If \var{count} is negative, \var{int} is shifted right by
$-$\var{count} bits.
\schemedisplay
(ash 8 0) ;=> 8
(ash 8 2) ;=> 32
(ash 8 -2) ;=> 2
(ash -1 2) ;=> -4
(ash -1 -2) ;=> -1
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogand}{\categoryprocedure}{(fxlogand \var{fixnum} \dots)}
\returns the logical ``and'' of the arguments \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noindent
The arguments are treated as two's complement integers, regardless
of the underlying representation.
With no arguments, \scheme{fxlogand} returns -1, i.e., all bits set.
\schemedisplay
(fxlogand) ;=> -1
(fxlogand 15) ;=> 15
(fxlogand -1 -1) ;=> -1
(fxlogand -1 0) ;=> 0
(fxlogand 5 3) ;=> 1
(fxlogand #b111000 #b101010) ;=> #b101000
(fxlogand #b1100 #b1111 #b1101) ;=> #b1100
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogior}{\categoryprocedure}{(fxlogior \var{fixnum} \dots)}
\formdef{fxlogor}{\categoryprocedure}{(fxlogor \var{fixnum} \dots)}
\returns the logical ``or'' of the arguments \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noindent
The arguments are treated as two's complement integers, regardless
of the underlying representation.
With no arguments, \scheme{fxlogior} returns 0, i.e., all bits reset.
\schemedisplay
(fxlogior) ;=> 0
(fxlogior 15) ;=> 15
(fxlogior -1 -1) ;=> -1
(fxlogior -1 0) ;=> -1
(fxlogior #b111000 #b101010) ;=> #b111010
(fxlogior #b1000 #b0100 #b0010) ;=> #b1110
(apply fxlogior '(1 2 4 8 16)) ;=> 31
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogxor}{\categoryprocedure}{(fxlogxor \var{fixnum} \dots)}
\returns the logical ``exclusive or'' of the arguments \scheme{\var{fixnum} \dots}
\listlibraries
\endentryheader
\noindent
The arguments are treated as two's complement integers, regardless
of the underlying representation.
With no arguments, \scheme{fxlogxor} returns 0, i.e., all bits reset.
\schemedisplay
(fxlogxor) ;=> 0
(fxlogxor 15) ;=> 15
(fxlogxor -1 -1) ;=> 0
(fxlogxor -1 0) ;=> -1
(fxlogxor 5 3) ;=> 6
(fxlogxor #b111000 #b101010) ;=> #b010010
(fxlogxor #b1100 #b0100 #b0110) ;=> #b1110
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlognot}{\categoryprocedure}{(fxlognot \var{fixnum})}
\returns the logical ``not'' of \var{fixnum}
\listlibraries
\endentryheader
\noindent
The argument is treated as a two's complement integer, regardless
of the underlying representation.
\schemedisplay
(fxlognot -1) ;=> 0
(fxlognot 0) ;=> -1
(fxlognot 1) ;=> -2
(fxlognot -2) ;=> 1
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogbit?}{\categoryprocedure}{(fxlogbit? \var{index} \var{fixnum})}
\returns \scheme{#t} if the specified bit is set, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noindent
\var{index} must be a nonnegative fixnum.
\var{fixnum} is treated as a two's complement integer, regardless of
the underlying representation.
\scheme{fxlogbit?} returns \scheme{#t} if the bit at index \var{index}
of \var{fixnum} is set (one) and \scheme{#f} otherwise.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
The index is limited only by the fixnum range; for nonnegative values of
\var{fixnum}, the bits above the highest order set bit are all considered
to be zero, and for negative values, the bits above the highest order
reset bit are all considered to be one.
\schemedisplay
(fxlogbit? 0 #b1110) ;=> #f
(fxlogbit? 1 #b1110) ;=> #t
(fxlogbit? 2 #b1110) ;=> #t
(fxlogbit? 3 #b1110) ;=> #t
(fxlogbit? 4 #b1110) ;=> #f
(fxlogbit? 100 #b1110) ;=> #f
(fxlogbit? 0 -6) ;=> #f ; \var{the two's complement of} -6 \var{is} 1...1010
(fxlogbit? 1 -6) ;=> #t
(fxlogbit? 2 -6) ;=> #f
(fxlogbit? 3 -6) ;=> #t
(fxlogbit? 100 -6) ;=> #t
(fxlogbit? (random 1000000) 0) ;=> #f
(fxlogbit? (random 1000000) -1) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogtest}{\categoryprocedure}{(fxlogtest \var{fixnum_1} \var{fixnum_2})}
\returns \scheme{#t} if any common bits are set, otherwise \scheme{#f}
\listlibraries
\endentryheader
\noindent
The arguments are treated as two's complement integers, regardless of
the underlying representation.
\scheme{fxlogtest} returns \scheme{#t} if any bit set in one argument is
also set in the other.
It returns \scheme{#f} if the two arguments have no set bits in common.
\schemedisplay
(fxlogtest #b10001 #b1110) ;=> #f
(fxlogtest #b10101 #b1110) ;=> #t
(fxlogtest #b111000 #b110111) ;=> #t
(fxlogtest #b101 -6) ;=> #f ; \var{the two's complement of} -6 \var{is} 1...1010
(fxlogtest #b1000 -6) ;=> #t
(fxlogtest 100 -6) ;=> #t
(fxlogtest (+ (random 1000000) 1) 0) ;=> #f
(fxlogtest (+ (random 1000000) 1) -1) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogbit0}{\categoryprocedure}{(fxlogbit0 \var{index} \var{fixnum})}
\returns the result of clearing bit \var{index} of \var{fixnum}
\listlibraries
\endentryheader
\noindent
\var{fixnum} is treated
as a two's complement integer, regardless of the underlying representation.
\var{index} must be nonnegative and less than the number of
bits in a fixnum, excluding the sign bit, i.e., less than
\scheme{(integer-length (most-positive-fixnum))}.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
\scheme{fxlogbit0} is equivalent to
\schemedisplay
(lambda (i n) (fxlogand (fxlognot (fxsll 1 i)) n))
\endschemedisplay
but more efficient.
\schemedisplay
(fxlogbit0 3 #b10101010) ;=> #b10100010
(fxlogbit0 4 #b10101010) ;=> #b10101010
(fxlogbit0 0 -1) ;=> -2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxlogbit1}{\categoryprocedure}{(fxlogbit1 \var{index} \var{fixnum})}
\returns the result of setting bit \var{index} of \var{fixnum}
\listlibraries
\endentryheader
\noindent
\var{fixnum} is treated
as a two's complement integer, regardless of the underlying representation.
\var{index} must be nonnegative and less than the number of
bits in a fixnum, excluding the sign bit, i.e., less than
\scheme{(integer-length (most-positive-fixnum))}.
The index is zero-based, counting from the lowest-order toward
higher-order bits.
\scheme{fxlogbit1} is equivalent to
\schemedisplay
(lambda (i n) (fxlogor (fxsll 1 i) n))
\endschemedisplay
but more efficient.
\schemedisplay
(fxlogbit1 3 #b10101010) ;=> #b10101010
(fxlogbit1 4 #b10101010) ;=> #b10111010
(fxlogbit1 4 0) ;=> #b10000
(fxlogbit1 0 -2) ;=> -1
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxsll}{\categoryprocedure}{(fxsll \var{fixnum} \var{count})}
\returns \var{fixnum} shifted left by \var{count}
\listlibraries
\endentryheader
\noindent
\var{fixnum} is treated as a two's complement integer, regardless
of the underlying representation.
\var{count} must be nonnegative and not more than the number of
bits in a fixnum, i.e.,
\scheme{(+ (integer-length (most-positive-fixnum)) 1)}.
An exception is raised with condition-type
\scheme{&implementation-restriction} if the result cannot be represented
as a fixnum.
\schemedisplay
(fxsll 1 2) ;=> 4
(fxsll -1 2) ;=> -4
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxsrl}{\categoryprocedure}{(fxsrl \var{fixnum} \var{count})}
\returns \var{fixnum} logically shifted right by \var{count}
\listlibraries
\endentryheader
\noindent
\var{fixnum} is treated as a two's complement integer, regardless
of the underlying representation.
\var{count} must be nonnegative and not more than the number of
bits in a fixnum, i.e.,
\scheme{(+ (integer-length (most-positive-fixnum)) 1)}.
\schemedisplay
(fxsrl 4 2) ;=> 1
(= (fxsrl -1 1) (most-positive-fixnum)) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{fxsra}{\categoryprocedure}{(fxsra \var{fixnum} \var{count})}
\returns \var{fixnum} arithmetically shifted right by \var{count}
\listlibraries
\endentryheader
\noindent
\var{fixnum} is treated as a two's complement integer, regardless
of the underlying representation.
\var{count} must be nonnegative and not more than the number of
bits in a fixnum, i.e.,
\scheme{(+ (integer-length (most-positive-fixnum)) 1)}.
\schemedisplay
(fxsra 64 3) ;=> 8
(fxsra -1 1) ;=> -1
(fxsra -64 3) ;=> -8
\endschemedisplay
\section{Random Number Generation\label{SECTNUMERICRANDOM}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{random}{\categoryprocedure}{(random \var{real})}
\returns a nonnegative pseudo-random number less than \var{real}
\listlibraries
\endnoskipentryheader
\noindent
\var{real} must be a positive integer or positive inexact real number.
\schemedisplay
(random 1) ;=> 0
(random 1029384535235) ;=> 1029384535001, \var{every} \var{now} \var{and} \var{then}
(random 1.0) ;=> 0.5, \var{every} \var{now} \var{and} \var{then}
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{random-seed}{\categorythreadparameter}{random-seed}
\listlibraries
\endentryheader
\noindent
The \index{random number generator}random number generator allows the
current random seed to be obtained and modified via the parameter
\scheme{random-seed}.
When called without arguments, \scheme{random-seed} returns the current
random seed.
When called with one argument, which must be a nonnegative exact integer
ranging from 1 through $2^{32}-1$, \scheme{random-seed} sets the current
random seed to the argument.
\schemedisplay
(let ([s (random-seed)])
(let ([r1 (random 1.0)])
(random-seed s)
(eqv? (random 1.0) r1))) ;=> #t
\endschemedisplay
\section{Miscellaneous Numeric Operations\label{SECTNUMERICMISC}}
%----------------------------------------------------------------------------
\noskipentryheader
\formdef{=}{\categoryprocedure}{(= \var{num_1} \var{num_2} \var{num_3} \dots)}
\formdef{<}{\categoryprocedure}{(< \var{real_1} \var{real_2} \var{real_3} \dots)}
\formdef{>}{\categoryprocedure}{(> \var{real_1} \var{real_2} \var{real_3} \dots)}
\formdef{<=}{\categoryprocedure}{(<= \var{real_1} \var{real_2} \var{real_3} \dots)}
\formdef{>=}{\categoryprocedure}{(>= \var{real_1} \var{real_2} \var{real_3} \dots)}
\returns \scheme{#t} if the relation holds, \scheme{#f} otherwise
\listlibraries
\endnoskipentryheader
\noindent
These predicates are identical to the Revised$^6$ Report counterparts,
except they are extended to accept one or more rather than two or more
arguments.
When passed one argument, each of these predicates returns \scheme{#t}.
\schemedisplay
(> 3/4) ;=> #t
(< 3/4) ;=> #t
(= 3/4) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{1+}{\categoryprocedure}{(1+ \var{num})}
\formdef{add1}{\categoryprocedure}{(add1 \var{num})}
\formdef{1-}{\categoryprocedure}{(1- \var{num})}
\formdef{-1+}{\categoryprocedure}{(-1+ \var{num})}
\formdef{sub1}{\categoryprocedure}{(sub1 \var{num})}
\returns \var{num} plus 1 or \var{num} minus 1
\listlibraries
\endentryheader
\noindent
\scheme{1+} and \scheme{add1} are equivalent to
\scheme{(lambda (x) (+ x 1))};
\scheme{1-}, \scheme{-1+}, and \scheme{sub1} are equivalent to
\scheme{(lambda (x) (- x 1))}.
\schemedisplay
(define plus
; x should be a nonnegative integer
(lambda (x y)
(if (zero? x)
y
(plus (1- x) (1+ y)))))
(plus 7 8) ;=> 15
(define double
; x should be a nonnegative integer
(lambda (x)
(if (zero? x)
0
(add1 (add1 (double (sub1 x)))))))
(double 7) ;=> 14
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{expt-mod}{\categoryprocedure}{(expt-mod \var{int_1} \var{int_2} \var{int_3})}
\returns \var{int_1} raised to the \var{int_2} power, modulo \var{int_3}
\listlibraries
\endentryheader
\noindent
\var{int_1}, \var{int_2} and \var{int_3}
must be nonnegative integers.
\scheme{expt-mod} performs its computation in such a way that the
intermediate results are never much larger than \var{int_3}.
This means that when \var{int_2} is large, \scheme{expt-mod} is more efficient
than the equivalent procedure \scheme{(lambda (x y z) (modulo (expt x y) z))}.
\schemedisplay
(expt-mod 2 4 3) ;=> 1
(expt-mod 2 76543 76543) ;=> 2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{isqrt}{\categoryprocedure}{(isqrt \var{n})}
\returns the integer square root of \var{n}
\listlibraries
\endentryheader
\noindent
\var{n} must be a nonnegative integer.
The integer square root of $n$ is defined to be
$\bigl\lfloor\sqrt n\bigr\rfloor$.
\schemedisplay
(isqrt 0) ;=> 0
(isqrt 16) ;=> 4
(isqrt 16.0) ;=> 4.0
(isqrt 20) ;=> 4
(isqrt 20.0) ;=> 4.0
(isqrt (* 2 (expt 10 20))) ;=> 14142135623
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{integer-length}{\categoryprocedure}{(integer-length \var{n})}
\returns see below
\listlibraries
\endentryheader
\noindent
The procedure \scheme{integer-length} returns the length in bits of
the smallest two's complement representation for \var{n}, with an
assumed leading 1 (sign) bit for negative numbers.
For zero, \scheme{integer-length} returns 0.
\schemedisplay
(integer-length 0) ;=> 0
(integer-length 1) ;=> 1
(integer-length 2) ;=> 2
(integer-length 3) ;=> 2
(integer-length 4) ;=> 3
(integer-length #b10000000) ;=> 8
(integer-length #b11111111) ;=> 8
(integer-length -1) ;=> 0
(integer-length -2) ;=> 1
(integer-length -3) ;=> 2
(integer-length -4) ;=> 2
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{nonpositive?}{\categoryprocedure}{(nonpositive? \var{real})}
\returns \scheme{#t} if \var{real} is not greater than zero, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
\scheme{nonpositive?} is equivalent to \scheme{(lambda (x) (<= x 0))}.
\schemedisplay
(nonpositive? 128) ;=> #f
(nonpositive? 0.0) ;=> #t
(nonpositive? 1.8e-15) ;=> #f
(nonpositive? -2/3) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{nonnegative?}{\categoryprocedure}{(nonnegative? \var{real})}
\returns \scheme{#t} if \var{real} is not less than zero, \scheme{#f} otherwise
\listlibraries
\endentryheader
\noindent
\scheme{nonnegative?} is equivalent to \scheme{(lambda (x) (>= x 0))}.
\schemedisplay
(nonnegative? -65) ;=> #f
(nonnegative? 0) ;=> #t
(nonnegative? -0.0121) ;=> #f
(nonnegative? 15/16) ;=> #t
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{conjugate}{\categoryprocedure}{(conjugate \var{num})}
\returns complex conjugate of \var{num}
\listlibraries
\endentryheader
\noindent
The procedure \scheme{conjugate}, when passed a complex argument
$a + bi$, returns its complex conjugate $a + (-b)i$.
\schemedisplay
(conjugate 3.0+4.0i) ;=> 3.0-4.0i
(conjugate 1e-20-2e-30i) ;=> 1e-20+2e-30i
(conjugate 3) ;=> 3
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{magnitude-squared}{\categoryprocedure}{(magnitude-squared \var{num})}
\returns magnitude of \var{num} squared
\listlibraries
\endentryheader
\noindent
The procedure \scheme{magnitude-squared}, when passed a complex
argument $a + bi$ returns its magnitude squared,
i.e., $a^2 + b^2$.
\schemedisplay
(magnitude-squared 3.0-4.0i) ;=> 25.0
(magnitude-squared 3.0) ;=> 9.0
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{sinh}{\categoryprocedure}{(sinh \var{num})}
\formdef{cosh}{\categoryprocedure}{(cosh \var{num})}
\formdef{tanh}{\categoryprocedure}{(tanh \var{num})}
\returns the hyperbolic sine, cosine, or tangent of \var{num}
\listlibraries
\endentryheader
\schemedisplay
(sinh 0.0) ;=> 0.0
(cosh 0.0) ;=> 1.0
(tanh -0.0) ;=> -0.0
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{asinh}{\categoryprocedure}{(asinh \var{num})}
\formdef{acosh}{\categoryprocedure}{(acosh \var{num})}
\formdef{atanh}{\categoryprocedure}{(atanh \var{num})}
\returns the hyperbolic arc sine, arc cosine, or arc tangent of \var{num}
\listlibraries
\endentryheader
\schemedisplay
(acosh 0.0) ;=> 0.0+1.5707963267948966i
(acosh 1.0) ;=> 0.0
(atanh -1.0) ;=> -inf.0
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{string->number}{\categoryprocedure}{(string->number \var{string})}
\formdef{string->number}{\categoryprocedure}{(string->number \var{string} \var{radix})}
\returns the number represented by \var{string}, or \scheme{#f}
\listlibraries
\endentryheader
\noindent
This procedure is identical to the Revised$^6$ Report version except
that \scheme{radix} may be any exact integer between 2 and 36, inclusive.
The Revised$^6$ Report version requires radix to be in the set
$\{2,8,10,16\}$.
\schemedisplay
(string->number "211012" 3) ;=> 559
(string->number "tobeornottobe" 36) ;=> 140613689159812836698
\endschemedisplay
%----------------------------------------------------------------------------
\entryheader
\formdef{number->string}{\categoryprocedure}{(number->string \var{num})}
\formdef{number->string}{\categoryprocedure}{(number->string \var{num} \var{radix})}
\formdef{number->string}{\categoryprocedure}{(number->string \var{num} \var{radix} \var{precision})}
\returns an external representation of \var{num} as a string
\listlibraries
\endentryheader
\noindent
This procedure is identical to the Revised$^6$ Report version except
that \scheme{radix} may be any exact integer between 2 and 36, inclusive.
The Revised$^6$ Report version requires radix to be in the set
$\{2,8,10,16\}$.
\schemedisplay
(number->string 10000 4) ;=> "2130100"
(number->string 10000 27) ;=> "DJA"
\endschemedisplay
|