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
|
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Generated from r6rs-lib.tex by tex2page, v 20070803
(running on MzScheme 371, unix),
(c) Dorai Sitaram,
http://www.ccs.neu.edu/~dorai/tex2page/tex2page-doc.html
-->
<head>
<title>
r6rs-lib
</title>
<link rel="stylesheet" type="text/css" href="r6rs-lib-Z-S.css" title=default>
<meta name=robots content="index,follow">
</head>
<body>
<div id=slidecontent>
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-11.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-13.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
<p></p>
<a name="node_chap_11"></a>
<h1 class=chapter>
<div class=chapterheading><a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_11">Chapter 11</a></div><br>
<a href="r6rs-lib-Z-H-1.html#node_toc_node_chap_11">Arithmetic</a></h1>
<p>
<a name="node_idx_850"></a></p>
<p>
This chapter describes Scheme’s libraries for more specialized
numerical operations: fixnum and flonum arithmetic, as well as bitwise
operations on exact integer objects. </p>
<p>
</p>
<a name="node_sec_11.1"></a>
<h2 class=section><a href="r6rs-lib-Z-H-1.html#node_toc_node_sec_11.1">11.1 Bitwise operations</a></h2>
<p>A number of procedures operate on the binary two’s-complement
representations of exact integer objects: Bit positions within an
exact integer object are counted from the right, i.e. bit 0 is the
least significant bit. Some procedures allow extracting <a name="node_idx_852"></a><em>bit
fields</em>, i.e., number objects representing subsequences of the
binary representation of an exact integer object. Bit fields are
always positive, and always defined using a finite number of bits.</p>
<p>
</p>
<a name="node_sec_11.2"></a>
<h2 class=section><a href="r6rs-lib-Z-H-1.html#node_toc_node_sec_11.2">11.2 Fixnums</a></h2>
<p></p>
<p>
Every implementation must define its fixnum range as a closed
interval
</p>
<div align=left><img src="r6rs-lib-Z-G-4.gif" border="0" alt="[r6rs-lib-Z-G-4.gif]"></div><p>
such that <em>w</em> is a (mathematical) integer <em>w</em> ≥ 24. Every
mathematical integer within an implementation’s fixnum range must
correspond to an exact integer object that is representable within the
implementation.
A fixnum is an exact integer object whose value lies within this
fixnum range.</p>
<p>
This section describes the <tt>(rnrs arithmetic fixnums (6))</tt><a name="node_idx_854"></a>library,
which defines various operations on fixnums.
Fixnum operations perform integer arithmetic on their fixnum
arguments, but raise an exception with condition type
<tt>&implementation-restriction</tt> if the result is not a fixnum.</p>
<p>
This section uses <i>fx</i>, <i>fx<sub>1</sub></i>, <i>fx<sub>2</sub></i>, etc., as parameter
names for arguments that must be fixnums.</p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_856"></a>fixnum?<i> obj</i>)</tt> procedure </div>
<p>
Returns <tt>#t</tt> if <i>obj</i> is an exact
integer object within the fixnum range, <tt>#f</tt> otherwise.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_858"></a>fixnum-width<i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_860"></a>least-fixnum<i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_862"></a>greatest-fixnum<i></i>)</tt> procedure </div>
<p>
These procedures return <em>w</em>,
<tt>-</tt> 2<sup><em>w</em><tt>-</tt>1</sup> and 2<sup><em>w</em><tt>-</tt>1</sup> <tt>-</tt> 1: the
width, minimum and the maximum value of the fixnum range, respectively.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_864"></a>fx=?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_866"></a>fx>?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_868"></a>fx<?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_870"></a>fx>=?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_872"></a>fx<=?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return <tt>#t</tt> if their arguments are (respectively):
equal, monotonically increasing, monotonically decreasing,
monotonically nondecreasing, or monotonically nonincreasing,
<tt>#f</tt> otherwise.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_874"></a>fxzero?<i> fx</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_876"></a>fxpositive?<i> fx</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_878"></a>fxnegative?<i> fx</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_880"></a>fxodd?<i> fx</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_882"></a>fxeven?<i> fx</i>)</tt> procedure </div>
<p>
These numerical predicates test a fixnum for a particular property,
returning <tt>#t</tt> or <tt>#f</tt>. The five properties tested by
these procedures are: whether the number object is zero, greater than zero,
less than zero, odd, or even.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_884"></a>fxmax<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_886"></a>fxmin<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return the maximum or minimum of their arguments.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_888"></a>fx+<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_890"></a>fx*<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<p>
These procedures return the sum or product of their arguments,
provided that sum or product is a fixnum. An exception with condition
type <tt>&implementation-restriction</tt> is raised if
that sum or product is not a fixnum.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_892"></a>fx-<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_894"></a>fx-<i> fx</i>)</tt> procedure </div>
<p>
With two arguments, this procedure returns the difference
<i>fx<sub>1</sub></i> <tt>-</tt> <i>fx<sub>2</sub></i>, provided that difference is a fixnum.</p>
<p>
With one argument, this procedure returns the additive
inverse of its argument, provided that integer object is a
fixnum.</p>
<p>
An exception with condition type <tt>&assertion</tt> is raised if the
mathematically correct result of this procedure is not a fixnum.</p>
<p>
</p>
<tt>(fx- (least-fixnum)) <tt> &assertion</tt> <i>exception</i><p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_896"></a>fxdiv-and-mod<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_898"></a>fxdiv<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_900"></a>fxmod<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_902"></a>fxdiv0-and-mod0<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_904"></a>fxdiv0<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_906"></a>fxmod0<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> must be nonzero.
These procedures implement number-theoretic integer division and
return the results of the corresponding mathematical operations
specified in report section on “Integer division”.</p>
<p>
</p>
<tt>(fxdiv <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) ⇒ <i>fx<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>fx<sub>2</sub></i><br>
(fxmod <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) ⇒ <i>fx<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>fx<sub>2</sub></i><br>
(fxdiv-and-mod <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) <br> ⇒ <i>fx<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>fx<sub>2</sub></i>, <i>fx<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>fx<sub>2</sub></i><br>
; two return values<br>
(fxdiv0 <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) ⇒ <i>fx<sub>1</sub></i> <em>d</em><em>i</em><em>v</em>sb0 <i>fx<sub>2</sub></i><br>
(fxmod0 <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) ⇒ <i>fx<sub>1</sub></i> <em>m</em><em>o</em><em>d</em>sb0 <i>fx<sub>2</sub></i><br>
(fxdiv0-and-mod0 <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) <br> ⇒ <i>fx<sub>1</sub></i> <i>fx<sub>1</sub></i> <em>d</em><em>i</em><em>v</em>sb0 <i>fx<sub>2</sub></i>, <i>fx<sub>1</sub></i> <em>m</em><em>o</em><em>d</em>sb0 <i>fx<sub>2</sub></i><br>
; two return values<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_908"></a>fx+/carry<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
Returns the two fixnum results of the following computation:
</p>
<tt>(let* ((s (+ <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i>))<br>
(s0 (mod0 s (expt 2 (fixnum-width))))<br>
(s1 (div0 s (expt 2 (fixnum-width)))))<br>
(values s0 s1))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_910"></a>fx-/carry<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
Returns the two fixnum results of the following computation:
</p>
<tt>(let* ((d (- <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i>))<br>
(d0 (mod0 d (expt 2 (fixnum-width))))<br>
(d1 (div0 d (expt 2 (fixnum-width)))))<br>
(values d0 d1))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_912"></a>fx*/carry<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
Returns the two fixnum results of the following computation:
</p>
<tt>(let* ((s (+ (* <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>) <i>fx<sub>3</sub></i>))<br>
(s0 (mod0 s (expt 2 (fixnum-width))))<br>
(s1 (div0 s (expt 2 (fixnum-width)))))<br>
(values s0 s1))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_914"></a>fxnot<i> <i>fx</i></i>)</tt> procedure </div>
<p>
Returns the unique fixnum that is congruent
mod 2<sup><em>w</em></sup> to the one’s-complement of <i>fx</i>.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_916"></a>fxand<i> <i>fx<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_918"></a>fxior<i> <i>fx<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_920"></a>fxxor<i> <i>fx<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return the fixnum that is the bit-wise “and”,
“inclusive or”, or “exclusive or” of the two’s complement
representations of their arguments. If they are passed only one
argument, they return that argument. If they are passed no arguments,
they return the fixnum (either <tt>-</tt> 1 or 0) that acts as identity for the
operation.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_922"></a>fxif<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
Returns the fixnum that is the bit-wise “if” of the two’s complement
representations of its arguments, i.e. for each bit, if it is 1 in
<i>fx<sub>1</sub></i>, the corresponding bit in <i>fx<sub>2</sub></i> becomes the value of
the corresponding bit in the result, and if it is 0, the corresponding
bit in <i>fx<sub>3</sub></i> becomes the corresponding bit in the value of the
result. This is the fixnum result of the following computation:
</p>
<tt>(fxior (fxand <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i>)<br>
(fxand (fxnot <i>fx<sub>1</sub></i>) <i>fx<sub>3</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_924"></a>fxbit-count<i> <i>fx</i></i>)</tt> procedure </div>
<p>
If <i>fx</i> is non-negative, this procedure returns the
number of 1 bits in the two’s complement representation of <i>fx</i>.
Otherwise it returns the result of the following computation:
</p>
<tt>(fxnot (fxbit-count (fxnot <i>ei</i>)))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_926"></a>fxlength<i> <i>fx</i></i>)</tt> procedure </div>
<p>
Returns the number of bits needed to represent <i>fx</i> if it is
positive, and the number of bits needed to represent <tt>(fxnot
<i>fx</i>)</tt> if it is negative, which is the fixnum result of the
following computation:
</p>
<tt>(do ((result 0 (+ result 1))<br>
(bits (if (fxnegative? <i>fx</i>)<br>
(fxnot <i>fx</i>)<br>
<i>fx</i>)<br>
(fxarithmetic-shift-right bits 1)))<br>
((fxzero? bits)<br>
result))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_928"></a>fxfirst-bit-set<i> <i>fx</i></i>)</tt> procedure </div>
<p>
Returns the index of the least significant 1 bit in
the two’s complement representation of <i>fx</i>. If
<i>fx</i> is 0, then <tt>-</tt> 1 is returned.
</p>
<tt>(fxfirst-bit-set 0) ⇒ -1<br>
(fxfirst-bit-set 1) ⇒ 0<br>
(fxfirst-bit-set -4) ⇒ 2<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_930"></a>fxbit-set?<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> must be non-negative and less than <tt>(fixnum-width)</tt>. The <tt>fxbit-set?</tt> procedure returns
<tt>#t</tt> if the <i>fx<sub>2</sub></i>th bit is 1 in the two’s complement
representation of <i>fx<sub>1</sub></i>, and <tt>#f</tt> otherwise. This is the
fixnum result of the following computation:
</p>
<tt>(not<br>
(fxzero?<br>
(fxand <i>fx<sub>1</sub></i><br>
(fxarithmetic-shift-left 1 <i>fx<sub>2</sub></i>))))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_932"></a>fxcopy-bit<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> must be non-negative and less than <tt>(fixnum-width)</tt>. <i>Fx<sub>3</sub></i> must be 0 or
1. The <tt>fxcopy-bit</tt> procedure returns the result of replacing
the <i>fx<sub>2</sub></i>th bit of <i>fx<sub>1</sub></i> by <i>fx<sub>3</sub></i>, which is
the result of the following computation:
</p>
<tt>(let* ((mask (fxarithmetic-shift-left 1 <i>fx<sub>2</sub></i>)))<br>
(fxif mask<br>
(fxarithmetic-shift-left <i>fx<sub>3</sub></i> <i>fx<sub>2</sub></i>)<br>
<i>fx<sub>1</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_934"></a>fxbit-field<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> and <i>fx<sub>3</sub></i> must be non-negative and less than
<tt>(fixnum-width)</tt>. Moreover, <i>fx<sub>2</sub></i> must be less than or
equal to <i>fx<sub>3</sub></i>. The <tt>fxbit-field</tt> procedure returns the
number represented by the bits at the positions from <i>fx<sub>2</sub></i> (inclusive) to
<i>fx<sub>3</sub></i> (exclusive), which is
the fixnum result of the following computation:
</p>
<tt>(let* ((mask (fxnot<br>
(fxarithmetic-shift-left -1 <i>fx<sub>3</sub></i>))))<br>
(fxarithmetic-shift-right (fxand <i>fx<sub>1</sub></i> mask)<br>
<i>fx<sub>2</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_936"></a>fxcopy-bit-field<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <i>fx<sub>4</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> and <i>fx<sub>3</sub></i> must be non-negative and less than
<tt>(fixnum-width)</tt>. Moreover, <i>fx<sub>2</sub></i> must be less than or
equal to <i>fx<sub>3</sub></i>. The <tt>fxcopy-bit-field</tt> procedure returns
the result of replacing in <i>fx<sub>1</sub></i> the bits at positions from
<i>fx<sub>2</sub></i> (inclusive) to <i>fx<sub>3</sub></i> (exclusive) by the corresponding bits in <i>fx<sub>4</sub></i>, which
is the fixnum result of the following computation:
</p>
<tt>(let* ((to <i>fx<sub>1</sub></i>)<br>
(start <i>fx<sub>2</sub></i>)<br>
(end <i>fx<sub>3</sub></i>)<br>
(from <i>fx<sub>4</sub></i>)<br>
(mask1 (fxarithmetic-shift-left -1 start))<br>
(mask2 (fxnot<br>
(fxarithmetic-shift-left -1 end)))<br>
(mask (fxand mask1 mask2)))<br>
(fxif mask<br>
(fxarithmetic-shift-left from start)<br>
to))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_938"></a>fxarithmetic-shift<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<p>
The absolute value of <i>fx<sub>2</sub></i> must be less than
<tt>(fixnum-width)</tt>. If
</p>
<tt>(floor (* <i>fx<sub>1</sub></i> (expt 2 <i>fx<sub>2</sub></i>)))<p></tt>
is a fixnum, then that fixnum is returned. Otherwise an exception
with condition type <tt>&implementation-restriction</tt> is
raised.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_940"></a>fxarithmetic-shift-left<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_942"></a>fxarithmetic-shift-right<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> must be non-negative, and less than <tt>(fixnum-width)</tt>.
The <tt>fxarithmetic-shift-left</tt> procedure behaves the same as <tt>fxarithmetic-shift</tt>, and <tt>(fxarithmetic-shift-right <i>fx<sub>1</sub></i>
<i>fx<sub>2</sub></i>)</tt> behaves the same as <tt>(fxarithmetic-shift <i>fx<sub>1</sub></i>
(fx- <i>fx<sub>2</sub></i>))</tt>.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_944"></a>fxrotate-bit-field<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i> <i>fx<sub>4</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i>, <i>fx<sub>3</sub></i>, and <i>fx<sub>4</sub></i> must be non-negative
and less than <tt>(fixnum-width)</tt>. <i>Fx<sub>2</sub></i> must be less than or
equal to <i>fx<sub>3</sub></i>. <i>Fx<sub>4</sub></i> must be less than the difference
between <i>fx<sub>3</sub></i> and <i>fx<sub>2</sub></i>. The <tt>fxrotate-bit-field</tt>
procedure returns the result of cyclically permuting in <i>fx<sub>1</sub></i> the
bits at positions from <i>fx<sub>2</sub></i> (inclusive) to <i>fx<sub>3</sub></i>
(exclusive) by <i>fx<sub>4</sub></i> bits
towards the more significant bits, which is the result of the
following computation:
</p>
<tt>(let* ((n <i>fx<sub>1</sub></i>)<br>
(start <i>fx<sub>2</sub></i>)<br>
(end <i>fx<sub>3</sub></i>)<br>
(count <i>fx<sub>4</sub></i>)<br>
(width (fx- end start)))<br>
(if (fxpositive? width)<br>
(let* ((count (fxmod count width))<br>
(field0<br>
(fxbit-field n start end))<br>
(field1<br>
(fxarithmetic-shift-left<br>
field0 count))<br>
(field2<br>
(fxarithmetic-shift-right<br>
field0 (fx- width count)))<br>
(field (fxior field1 field2)))<br>
(fxcopy-bit-field n start end field))<br>
n))<p></tt></p>
<p>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_946"></a>fxreverse-bit-field<i> <i>fx<sub>1</sub></i> <i>fx<sub>2</sub></i> <i>fx<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Fx<sub>2</sub></i> and <i>fx<sub>3</sub></i> must be non-negative and less than
<tt>(fixnum-width)</tt>. Moreover, <i>fx<sub>2</sub></i> must be less than or
equal to <i>fx<sub>3</sub></i>. The <tt>fxreverse-bit-field</tt> procedure
returns
the fixnum obtained from <i>fx<sub>1</sub></i> by reversing the
order of the bits at positions from <i>fx<sub>2</sub></i> (inclusive) to
<i>fx<sub>3</sub></i> (exclusive).
</p>
<tt>(fxreverse-bit-field <tt>#</tt>b1010010 1 4) <br> ⇒ 88 ; <tt>#</tt>b1011000<p></tt></p>
<p>
</p>
<p></p>
<p>
</p>
<a name="node_sec_11.3"></a>
<h2 class=section><a href="r6rs-lib-Z-H-1.html#node_toc_node_sec_11.3">11.3 Flonums</a></h2>
<p></p>
<p>
This section describes the <tt>(rnrs arithmetic flonums (6))</tt><a name="node_idx_948"></a>library.</p>
<p>
This section uses <i>fl</i>, <i>fl<sub>1</sub></i>, <i>fl<sub>2</sub></i>, etc., as
parameter names for arguments that must be flonums, and <i>ifl</i>
as a name for arguments that
must be integer-valued flonums, i.e., flonums for which the
<tt>integer-valued?</tt> predicate returns true.</p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_950"></a>flonum?<i> obj</i>)</tt> procedure </div>
<p>
Returns <tt>#t</tt> if <i>obj</i> is a flonum, <tt>#f</tt> otherwise.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_952"></a>real->flonum<i> x</i>)</tt> procedure </div>
<p>
Returns the best flonum representation of
<i>x</i>.</p>
<p>
The value returned is a flonum that is numerically closest to the
argument.</p>
<p>
</p>
<blockquote><em>Note: </em>
If flonums are represented in binary floating point, then
implementations should break ties by preferring
the floating-point representation whose least significant bit is
zero.
</blockquote>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_954"></a>fl=?<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <i>fl<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_956"></a>fl<?<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <i>fl<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_958"></a>fl<=?<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <i>fl<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_960"></a>fl>?<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <i>fl<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_962"></a>fl>=?<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <i>fl<sub>3</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return <tt>#t</tt> if their arguments are (respectively):
equal, monotonically increasing, monotonically decreasing,
monotonically nondecreasing, or monotonically nonincreasing,
<tt>#f</tt> otherwise. These
predicates must be transitive.</p>
<p>
</p>
<tt>(fl= +inf.0 +inf.0) ⇒ <tt>#t</tt><br>
(fl= -inf.0 +inf.0) ⇒ <tt>#f</tt><br>
(fl= -inf.0 -inf.0) ⇒ <tt>#t</tt><br>
(fl= 0.0 -0.0) ⇒ <tt>#t</tt><br>
(fl< 0.0 -0.0) ⇒ <tt>#f</tt><br>
(fl= +nan.0 <i>fl</i>) ⇒ <tt>#f</tt><br>
(fl< +nan.0 <i>fl</i>) ⇒ <tt>#f</tt><p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_964"></a>flinteger?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_966"></a>flzero?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_968"></a>flpositive?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_970"></a>flnegative?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_972"></a>flodd?<i> ifl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_974"></a>fleven?<i> ifl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_976"></a>flfinite?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_978"></a>flinfinite?<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_980"></a>flnan?<i> fl</i>)</tt> procedure </div>
<p>
These numerical predicates test a flonum for a particular property,
returning <tt>#t</tt> or <tt>#f</tt>.
The <tt>flinteger?</tt> procedure tests whether the number object is an integer,
<tt>flzero?</tt> tests whether
it is <tt>fl=?</tt> to zero, <tt>flpositive?</tt> tests whether it is greater
than zero, <tt>flnegative?</tt> tests whether it is less
than zero, <tt>flodd?</tt> tests whether it is odd,
<tt>fleven?</tt> tests whether it is even,
<tt>flfinite?</tt> tests whether it is not an infinity and not a NaN,
<tt>flinfinite?</tt> tests whether it is an infinity, and
<tt>flnan?</tt> tests whether it is a NaN.</p>
<p>
</p>
<tt>(flnegative? -0.0) ⇒ <tt>#f</tt><br>
(flfinite? +inf.0) ⇒ <tt>#f</tt><br>
(flfinite? 5.0) ⇒ <tt>#t</tt><br>
(flinfinite? 5.0) ⇒ <tt>#f</tt><br>
(flinfinite? +inf.0) ⇒ <tt>#t</tt><p></tt></p>
<p>
</p>
<blockquote><em>Note: </em>
<tt>(flnegative? -0.0)</tt> must return <tt>#f</tt>,
else it would lose the correspondence with
<tt>(fl< -0.0 0.0)</tt>, which is <tt>#f</tt>
according to IEEE 754 [<a href="r6rs-lib-Z-H-21.html#node_bib_7">7</a>].
</blockquote>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_982"></a>flmax<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_984"></a>flmin<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return the maximum or minimum of their arguments.
They always return a NaN when one or more of the arguments is a NaN.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_986"></a>fl+<i> <i>fl<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_988"></a>fl*<i> <i>fl<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return the flonum sum or product of their flonum
arguments. In general, they should return the flonum that best
approximates the mathematical sum or product. (For implementations
that represent flonums using IEEE binary floating point, the
meaning of “best” is defined by the IEEE standards.)</p>
<p>
</p>
<tt>(fl+ +inf.0 -inf.0) ⇒ +nan.0<br>
(fl+ +nan.0 <i>fl</i>) ⇒ +nan.0<br>
(fl* +nan.0 <i>fl</i>) ⇒ +nan.0<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_990"></a>fl-<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_992"></a>fl-<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_994"></a>fl/<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_996"></a>fl/<i> fl</i>)</tt> procedure </div>
<p>
With two or more arguments, these procedures return the flonum
difference or quotient of their flonum arguments, associating to the
left. With one argument, however, they return the additive or
multiplicative flonum inverse of their argument. In general, they
should return the flonum that best approximates the mathematical
difference or quotient. (For implementations that represent flonums
using IEEE binary floating point, the meaning of “best” is
reasonably well-defined by the IEEE standards.)</p>
<p>
</p>
<tt>(fl- +inf.0 +inf.0) ⇒ +nan.0<p></tt></p>
<p>
For undefined quotients, <tt>fl/</tt> behaves as specified by the
IEEE standards:</p>
<p>
</p>
<tt>(fl/ 1.0 0.0) ⇒ +inf.0<br>
(fl/ -1.0 0.0) ⇒ -inf.0<br>
(fl/ 0.0 0.0) ⇒ +nan.0<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_998"></a>flabs<i> fl</i>)</tt> procedure </div>
<p>
Returns the absolute value of <i>fl</i>.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1000"></a>fldiv-and-mod<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1002"></a>fldiv<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1004"></a>flmod<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1006"></a>fldiv0-and-mod0<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1008"></a>fldiv0<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1010"></a>flmod0<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<p>
These procedures implement number-theoretic integer division and
return the results of the corresponding mathematical operations
specified in report section on “Integer division”. For zero divisors, these
procedures may return a NaN or some unspecified flonum.</p>
<p>
</p>
<tt>(fldiv <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) ⇒ <i>fl<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>fl<sub>2</sub></i><br>
(flmod <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) ⇒ <i>fl<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>fl<sub>2</sub></i><br>
(fldiv-and-mod <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) <br> ⇒ <i>fl<sub>1</sub></i> <em>d</em><em>i</em><em>v</em> <i>fl<sub>2</sub></i>, <i>fl<sub>1</sub></i> <em>m</em><em>o</em><em>d</em> <i>fl<sub>2</sub></i><br>
; two return values<br>
(fldiv0 <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) ⇒ <i>fl<sub>1</sub></i> <em>d</em><em>i</em><em>v</em><sub>0</sub> <i>fl<sub>2</sub></i><br>
(flmod0 <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) ⇒ <i>fl<sub>1</sub></i> <em>m</em><em>o</em><em>d</em><sub>0</sub> <i>fl<sub>2</sub></i><br>
(fldiv0-and-mod0 <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i>) <br> ⇒ <i>fl<sub>1</sub></i> <em>d</em><em>i</em><em>v</em><sub>0</sub> <i>fl<sub>2</sub></i>, <i>fl<sub>1</sub></i> <em>m</em><em>o</em><em>d</em><sub>0</sub> <i>fl<sub>2</sub></i><br>
; two return values<p></tt></p>
<p>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1012"></a>flnumerator<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1014"></a>fldenominator<i> fl</i>)</tt> procedure </div>
<p>
These procedures return the numerator or denominator of <i>fl</i>
as a flonum; the result is computed as if <i>fl</i> was represented as
a fraction in lowest terms. The denominator is always positive. The
denominator of 0.0 is defined to be 1.0.
</p>
<tt>(flnumerator +inf.0) ⇒ +inf.0<br>
(flnumerator -inf.0) ⇒ -inf.0<br>
(fldenominator +inf.0) ⇒ 1.0<br>
(fldenominator -inf.0) ⇒ 1.0<br>
(flnumerator 0.75) ⇒ 3.0 ; probably<br>
(fldenominator 0.75) ⇒ 4.0 ; probably<p></tt></p>
<p>
Implementations should implement following behavior:</p>
<p>
</p>
<tt>(flnumerator -0.0) ⇒ -0.0<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1016"></a>flfloor<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1018"></a>flceiling<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1020"></a>fltruncate<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1022"></a>flround<i> fl</i>)</tt> procedure </div>
<p>
These procedures return integral flonums for flonum arguments that are
not infinities or NaNs. For such arguments, <tt>flfloor</tt> returns the
largest integral flonum not larger than <i>fl</i>. The <tt>flceiling</tt>
procedure
returns the smallest integral flonum not smaller than <i>fl</i>.
The <tt>fltruncate</tt> procedure returns the integral flonum closest to <i>fl</i> whose
absolute value is not larger than the absolute value of <i>fl</i>.
The <tt>flround</tt> procedure returns the closest integral flonum to <i>fl</i>,
rounding to even when <i>fl</i> represents a number halfway between two integers.</p>
<p>
Although infinities and NaNs are not integer objects, these procedures return
an infinity when given an infinity as an argument, and a NaN when
given a NaN:</p>
<p>
</p>
<tt>(flfloor +inf.0) ⇒ +inf.0<br>
(flceiling -inf.0) ⇒ -inf.0<br>
(fltruncate +nan.0) ⇒ +nan.0<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1024"></a>flexp<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1026"></a>fllog<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1028"></a>fllog<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1030"></a>flsin<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1032"></a>flcos<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1034"></a>fltan<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1036"></a>flasin<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1038"></a>flacos<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1040"></a>flatan<i> fl</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1042"></a>flatan<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<p>
These procedures compute the usual transcendental functions.
The <tt>flexp</tt> procedure computes the base-<em>e</em> exponential of <i>fl</i>.
The <tt>fllog</tt> procedure with a single argument computes the natural logarithm of
<i>fl</i> (not the base ten logarithm); <tt>(fllog <i>fl<sub>1</sub></i>
<i>fl<sub>2</sub></i>)</tt> computes the base-<i>fl<sub>2</sub></i> logarithm of <i>fl<sub>1</sub></i>.
The <tt>flasin</tt>, <tt>flacos</tt>, and <tt>flatan</tt> procedures compute arcsine,
arccosine, and arctangent, respectively. <tt>(flatan <i>fl<sub>1</sub></i>
<i>fl<sub>2</sub></i>)</tt> computes the arc tangent of <i>fl<sub>1</sub></i>/<i>fl<sub>2</sub></i>.</p>
<p>
See report
section on “Transcendental functions” for the underlying
mathematical operations. In the event that these operations do not
yield a real result for the given arguments, the result may be a NaN,
or may be some unspecified flonum.</p>
<p>
Implementations that use IEEE binary floating-point arithmetic
should follow the relevant standards for these procedures.</p>
<p>
</p>
<tt>(flexp +inf.0) ⇒ +inf.0<br>
(flexp -inf.0) ⇒ 0.0<br>
(fllog +inf.0) ⇒ +inf.0<br>
(fllog 0.0) ⇒ -inf.0<br>
(fllog -0.0) ⇒ <i>unspecified</i><br>
; if -0.0 is distinguished<br>
(fllog -inf.0) ⇒ +nan.0<br>
(flatan -inf.0) <br> ⇒ -1.5707963267948965<br>
; approximately<br>
(flatan +inf.0) <br> ⇒ 1.5707963267948965<br>
; approximately<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1044"></a>flsqrt<i> fl</i>)</tt> procedure </div>
<p>
Returns the principal square root of <i>fl</i>. For <tt>-</tt> 0.0,
<tt>flsqrt</tt> should return <tt>-</tt> 0.0; for other negative arguments,
the result may be a NaN or some unspecified flonum.</p>
<p>
</p>
<tt>(flsqrt +inf.0) ⇒ +inf.0<br>
(flsqrt -0.0) ⇒ -0.0<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1046"></a>flexpt<i> <i>fl<sub>1</sub></i> <i>fl<sub>2</sub></i></i>)</tt> procedure </div>
<p>
Either <i>fl<sub>1</sub></i> should be non-negative, or, if <i>fl<sub>1</sub></i> is
negative, <i>fl<sub>2</sub></i> should be an integer object.
The <tt>flexpt</tt> procedure returns <i>fl<sub>1</sub></i> raised to the power <i>fl<sub>2</sub></i>. If <i>fl<sub>1</sub></i> is
negative and <i>fl<sub>2</sub></i> is not an integer object, the result may be a
NaN, or may be some unspecified flonum. If <i>fl<sub>1</sub></i> is zero, then
the result is zero.
</p>
<p></p>
<p>
</p>
<p><a name="node_idx_1048"></a></p>
<div align=left><tt><tt>&no-infinities</tt></tt> condition type </div>
<div align=left><tt>(<a name="node_idx_1050"></a>make-no-infinities-violation<i> obj</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1052"></a>no-infinities-violation?<i> obj</i>)</tt> procedure </div>
<a name="node_idx_1054"></a><div align=left><tt><tt>&no-nans</tt></tt> condition type </div>
<div align=left><tt>(<a name="node_idx_1056"></a>make-no-nans-violation<i> obj</i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1058"></a>no-nans-violation?<i> obj</i>)</tt> procedure </div>
<p>
These condition types could be defined by the following code:</p>
<p>
</p>
<tt>(define-condition-type &no-infinities<br>
&implementation-restriction<br>
make-no-infinities-violation<br>
no-infinities-violation?)<br>
<br>
(define-condition-type &no-nans<br>
&implementation-restriction<br>
make-no-nans-violation no-nans-violation?)<p></tt></p>
<p>
These types describe that a program has executed an arithmetic
operations that is specified to return an infinity or a NaN,
respectively, on a Scheme implementation that is not able to represent
the infinity or NaN. (See report section on “Representability of infinities and NaNs”.)
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1060"></a>fixnum->flonum<i> fx</i>)</tt> procedure </div>
<p>
Returns a flonum that is numerically closest to <i>fx</i>.</p>
<p>
</p>
<blockquote><em>Note: </em>
The result of this procedure may not be
numerically equal to <i>fx</i>, because the fixnum precision
may be greater than the flonum precision.
</blockquote>
<p></p>
<p>
</p>
<a name="node_sec_11.4"></a>
<h2 class=section><a href="r6rs-lib-Z-H-1.html#node_toc_node_sec_11.4">11.4 Exact bitwise arithmetic</a></h2>
<p></p>
<p>
This section describes the <tt>(rnrs arithmetic bitwise (6))</tt><a name="node_idx_1062"></a>library. The exact bitwise arithmetic provides generic operations on
exact integer objects. This section uses <i>ei</i>, <i>ei<sub>1</sub></i>, <i>ei<sub>2</sub></i>, etc.,
as parameter names that must be exact integer objects.</p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1064"></a>bitwise-not<i> ei</i>)</tt> procedure </div>
<p>
Returns the exact integer object whose two’s complement representation is the
one’s complement of the two’s complement representation of <i>ei</i>.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1066"></a>bitwise-and<i> <i>ei<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1068"></a>bitwise-ior<i> <i>ei<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1070"></a>bitwise-xor<i> <i>ei<sub>1</sub></i> <tt>...</tt></i>)</tt> procedure </div>
<p>
These procedures return the exact integer object that is the bit-wise
“and”, “inclusive or”, or “exclusive or” of the two’s complement
representations of their arguments. If they are passed only one
argument, they return that argument. If they are passed no arguments,
they return the integer object (either <tt>-</tt> 1 or 0) that acts as identity for
the operation.
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1072"></a>bitwise-if<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i></i>)</tt> procedure </div>
<p>
Returns the exact integer object that is the bit-wise “if” of the two’s complement
representations of its arguments, i.e. for each bit, if it is 1 in
<i>ei<sub>1</sub></i>, the corresponding bit in <i>ei<sub>2</sub></i> becomes the value of
the corresponding bit in the result, and if it is 0, the corresponding
bit in <i>ei<sub>3</sub></i> becomes the corresponding bit in the value of the
result.
This is the result of the following computation:
</p>
<tt>(bitwise-ior (bitwise-and <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i>)<br>
(bitwise-and (bitwise-not <i>ei<sub>1</sub></i>) <i>ei<sub>3</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1074"></a>bitwise-bit-count<i> ei</i>)</tt> procedure </div>
<p>
If <i>ei</i> is non-negative, this procedure returns the number of
1 bits in the two’s complement representation of <i>ei</i>.
Otherwise it returns the result of the following computation:
</p>
<tt>(bitwise-not (bitwise-bit-count (bitwise-not <i>ei</i>)))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1076"></a>bitwise-length<i> ei</i>)</tt> procedure </div>
<p>
Returns the number of bits needed to represent <i>ei</i> if it is
positive, and the number of bits needed to represent <tt>(bitwise-not
<i>ei</i>)</tt> if it is negative, which is the exact integer object that
is the result of the following computation:
</p>
<tt>(do ((result 0 (+ result 1))<br>
(bits (if (negative? <i>ei</i>)<br>
(bitwise-not <i>ei</i>)<br>
<i>ei</i>)<br>
(bitwise-arithmetic-shift bits -1)))<br>
((zero? bits)<br>
result))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1078"></a>bitwise-first-bit-set<i> ei</i>)</tt> procedure </div>
<p>
Returns the index of the least significant 1
bit in the two’s complement representation of <i>ei</i>.
If <i>ei</i> is 0, then <tt>-</tt> 1 is returned.
</p>
<tt>(bitwise-first-bit-set 0) ⇒ -1<br>
(bitwise-first-bit-set 1) ⇒ 0<br>
(bitwise-first-bit-set -4) ⇒ 2<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1080"></a>bitwise-bit-set?<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> must be non-negative.
The <tt>bitwise-bit-set?</tt> procedure returns
<tt>#t</tt> if the <i>ei<sub>2</sub></i>th bit is 1 in the two’s complement
representation of <i>ei<sub>1</sub></i>, and <tt>#f</tt>
otherwise. This is the result of the following computation:
</p>
<tt>(not (zero?<br>
(bitwise-and<br>
(bitwise-arithmetic-shift-left 1 <i>ei<sub>2</sub></i>)<br>
<i>ei<sub>1</sub></i>)))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1082"></a>bitwise-copy-bit<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> must be non-negative, and <i>ei<sub>3</sub></i>
must be either 0 or 1.
The <tt>bitwise-copy-bit</tt> procedure returns the result of replacing
the <i>ei<sub>2</sub></i>th bit of <i>ei<sub>1</sub></i> by the <i>ei<sub>2</sub></i>th bit of <i>ei<sub>3</sub></i>, which is
the result of the following computation:
</p>
<tt>(let* ((mask (bitwise-arithmetic-shift-left 1 <i>ei<sub>2</sub></i>)))<br>
(bitwise-if mask<br>
(bitwise-arithmetic-shift-left <i>ei<sub>3</sub></i> <i>ei<sub>2</sub></i>)<br>
<i>ei<sub>1</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1084"></a>bitwise-bit-field<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> and <i>ei<sub>3</sub></i> must be non-negative, and
<i>ei<sub>2</sub></i> must be less than or equal to <i>ei<sub>3</sub></i>.
The <tt>bitwise-bit-field</tt> procedure returns the
number represented by the bits at the positions from <i>ei<sub>2</sub></i>
(inclusive) to <i>ei<sub>3</sub></i> (exclusive), which is
the result of the following computation:
</p>
<tt>(let ((mask<br>
(bitwise-not<br>
(bitwise-arithmetic-shift-left -1 <i>ei<sub>3</sub></i>))))<br>
(bitwise-arithmetic-shift-right<br>
(bitwise-and <i>ei<sub>1</sub></i> mask)<br>
<i>ei<sub>2</sub></i>))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1086"></a>bitwise-copy-bit-field<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i> <i>ei<sub>4</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> and <i>ei<sub>3</sub></i> must be non-negative,
and <i>ei<sub>2</sub></i> must be less than or equal to <i>ei<sub>3</sub></i>.
The <tt>bitwise-copy-bit-field</tt> procedure returns
the result of replacing in <i>ei<sub>1</sub></i> the bits at positions from
<i>ei<sub>2</sub></i> (inclusive) to <i>ei<sub>3</sub></i> (exclusive) by the corresponding bits in <i>ei<sub>4</sub></i>, which
is the fixnum result of the following computation:
</p>
<tt>(let* ((to <i>ei<sub>1</sub></i>)<br>
(start <i>ei<sub>2</sub></i>)<br>
(end <i>ei<sub>3</sub></i>)<br>
(from <i>ei<sub>4</sub></i>)<br>
(mask1<br>
(bitwise-arithmetic-shift-left -1 start))<br>
(mask2<br>
(bitwise-not<br>
(bitwise-arithmetic-shift-left -1 end)))<br>
(mask (bitwise-and mask1 mask2)))<br>
(bitwise-if mask<br>
(bitwise-arithmetic-shift-left from<br>
start)<br>
to))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1088"></a>bitwise-arithmetic-shift<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i></i>)</tt> procedure </div>
<p>
Returns the result of the following computation:
</p>
<tt>(floor (* <i>ei<sub>1</sub></i> (expt 2 <i>ei<sub>2</sub></i>)))<p></tt></p>
<p>
Examples:
</p>
<tt>(bitwise-arithmetic-shift -6 -1) <br> ⇒ -3<br>
(bitwise-arithmetic-shift -5 -1) <br> ⇒ -3<br>
(bitwise-arithmetic-shift -4 -1) <br> ⇒ -2<br>
(bitwise-arithmetic-shift -3 -1) <br> ⇒ -2<br>
(bitwise-arithmetic-shift -2 -1) <br> ⇒ -1<br>
(bitwise-arithmetic-shift -1 -1) <br> ⇒ -1<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1090"></a>bitwise-arithmetic-shift-left<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i></i>)</tt> procedure </div>
<div align=left><tt>(<a name="node_idx_1092"></a>bitwise-arithmetic-shift-right<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> must be non-negative. The <tt>bitwise-arithmetic-shift-left</tt> procedure returns the same result as <tt>bitwise-arithmetic-shift</tt>, and
</p>
<tt>(bitwise-arithmetic-shift-right <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i>)<p></tt>
returns the same result as
</p>
<tt>(bitwise-arithmetic-shift <i>ei<sub>1</sub></i> (- <i>ei<sub>2</sub></i>)).<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1094"></a>bitwise-rotate-bit-field<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i> <i>ei<sub>4</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i>, <i>ei<sub>3</sub></i>, <i>ei<sub>4</sub></i> must be non-negative,
<i>ei<sub>2</sub></i> must be less than or equal to <i>ei<sub>3</sub></i>, and
<i>ei<sub>4</sub></i> must be non-negative.
procedure returns the result of cyclically permuting in <i>ei<sub>1</sub></i> the
bits at positions from <i>ei<sub>2</sub></i> (inclusive) to <i>ei<sub>3</sub></i> (exclusive) by <i>ei<sub>4</sub></i> bits
towards the more significant bits, which is the result of the
following computation:
</p>
<tt>(let* ((n <i>ei<sub>1</sub></i>)<br>
(start <i>ei<sub>2</sub></i>)<br>
(end <i>ei<sub>3</sub></i>)<br>
(count <i>ei<sub>4</sub></i>)<br>
(width (- end start)))<br>
(if (positive? width)<br>
(let* ((count (mod count width))<br>
(field0<br>
(bitwise-bit-field n start end))<br>
(field1 (bitwise-arithmetic-shift-left<br>
field0 count))<br>
(field2 (bitwise-arithmetic-shift-right<br>
field0<br>
(- width count)))<br>
(field (bitwise-ior field1 field2)))<br>
(bitwise-copy-bit-field n start end field))<br>
n))<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div align=left><tt>(<a name="node_idx_1096"></a>bitwise-reverse-bit-field<i> <i>ei<sub>1</sub></i> <i>ei<sub>2</sub></i> <i>ei<sub>3</sub></i></i>)</tt> procedure </div>
<p>
<i>Ei<sub>2</sub></i> and <i>ei<sub>3</sub></i> must be non-negative, and
<i>ei<sub>2</sub></i> must be less than or equal to <i>ei<sub>3</sub></i>. The <tt>bitwise-reverse-bit-field</tt> procedure returns
the result obtained from <i>ei<sub>1</sub></i> by reversing the
order of the bits at positions from <i>ei<sub>2</sub></i> (inclusive) to
<i>ei<sub>3</sub></i> (exclusive).
</p>
<tt>(bitwise-reverse-bit-field <tt>#</tt>b1010010 1 4) <br> ⇒ 88 ; <tt>#</tt>b1011000<p></tt>
</p>
<p></p>
<p>
</p>
<p></p>
<div class=smallskip></div>
<p style="margin-top: 0pt; margin-bottom: 0pt">
<div align=right class=navigation>[Go to <span><a href="r6rs-lib.html">first</a>, <a href="r6rs-lib-Z-H-11.html">previous</a></span><span>, <a href="r6rs-lib-Z-H-13.html">next</a></span> page<span>; </span><span><a href="r6rs-lib-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="r6rs-lib-Z-H-21.html#node_index_start">index</a></span>]</div>
</p>
<p></p>
</div>
</body>
</html>
|