1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714
|
/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004 Free Software Foundation, Inc.
*
* Portions Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories
* and Bellcore. See scm_divide.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#include <math.h>
#include "libguile/_scm.h"
#include "libguile/feature.h"
#include "libguile/ports.h"
#include "libguile/root.h"
#include "libguile/smob.h"
#include "libguile/strings.h"
#include "libguile/validate.h"
#include "libguile/numbers.h"
#include "libguile/deprecation.h"
static SCM scm_divbigbig (SCM_BIGDIG *x, size_t nx, SCM_BIGDIG *y, size_t ny, int sgn, int modes);
static SCM scm_divbigint (SCM x, long z, int sgn, int mode);
#define DIGITS '0':case '1':case '2':case '3':case '4':\
case '5':case '6':case '7':case '8':case '9'
#define SCM_SWAP(x,y) do { SCM __t = x; x = y; y = __t; } while (0)
/* FLOBUFLEN is the maximum number of characters neccessary for the
* printed or scm_string representation of an inexact number.
*/
#define FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
/* IS_INF tests its floating point number for infiniteness
Dirk:FIXME:: This test does not work if x == 0
*/
#ifndef IS_INF
#define IS_INF(x) ((x) == (x) / 2)
#endif
/* Return true if X is not infinite and is not a NaN
Dirk:FIXME:: Since IS_INF is broken, this test does not work if x == 0
*/
#ifndef isfinite
#define isfinite(x) (!IS_INF (x) && (x) == (x))
#endif
static SCM abs_most_negative_fixnum;
SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
"otherwise.")
#define FUNC_NAME s_scm_exact_p
{
if (SCM_INUMP (x)) {
return SCM_BOOL_T;
} else if (SCM_BIGP (x)) {
return SCM_BOOL_T;
} else {
return SCM_BOOL_F;
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0,
(SCM n),
"Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
"otherwise.")
#define FUNC_NAME s_scm_odd_p
{
if (SCM_INUMP (n)) {
return SCM_BOOL ((4 & SCM_UNPACK (n)) != 0);
} else if (SCM_BIGP (n)) {
return SCM_BOOL ((1 & SCM_BDIGITS (n) [0]) != 0);
} else {
SCM_WRONG_TYPE_ARG (1, n);
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_even_p, "even?", 1, 0, 0,
(SCM n),
"Return @code{#t} if @var{n} is an even number, @code{#f}\n"
"otherwise.")
#define FUNC_NAME s_scm_even_p
{
if (SCM_INUMP (n)) {
return SCM_BOOL ((4 & SCM_UNPACK (n)) == 0);
} else if (SCM_BIGP (n)) {
return SCM_BOOL ((1 & SCM_BDIGITS (n) [0]) == 0);
} else {
SCM_WRONG_TYPE_ARG (1, n);
}
}
#undef FUNC_NAME
SCM_GPROC (s_abs, "abs", 1, 0, 0, scm_abs, g_abs);
/* "Return the absolute value of @var{x}."
*/
SCM
scm_abs (SCM x)
{
if (SCM_INUMP (x)) {
long int xx = SCM_INUM (x);
if (xx >= 0) {
return x;
} else if (SCM_POSFIXABLE (-xx)) {
return SCM_MAKINUM (-xx);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (-xx);
#else
scm_num_overflow (s_abs);
#endif
}
} else if (SCM_BIGP (x)) {
if (!SCM_BIGSIGN (x)) {
return x;
} else {
return scm_i_copybig (x, 0);
}
} else if (SCM_REALP (x)) {
return scm_make_real (fabs (SCM_REAL_VALUE (x)));
} else {
SCM_WTA_DISPATCH_1 (g_abs, x, 1, s_abs);
}
}
SCM_GPROC (s_quotient, "quotient", 2, 0, 0, scm_quotient, g_quotient);
/* "Return the quotient of the numbers @var{x} and @var{y}."
*/
SCM
scm_quotient (SCM x, SCM y)
{
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_quotient);
} else {
long z = xx / yy;
if (SCM_FIXABLE (z)) {
return SCM_MAKINUM (z);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (z);
#else
scm_num_overflow (s_quotient);
#endif
}
}
} else if (SCM_BIGP (y)) {
if (SCM_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM
&& scm_bigcomp (abs_most_negative_fixnum, y) == 0)
{
/* Special case: x == fixnum-min && y == abs (fixnum-min) */
return SCM_MAKINUM (-1);
}
else
return SCM_MAKINUM (0);
} else {
SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_quotient);
} else if (yy == 1) {
return x;
} else {
long z = yy < 0 ? -yy : yy;
if (z < SCM_BIGRAD) {
SCM sw = scm_i_copybig (x, SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0));
scm_divbigdig (SCM_BDIGITS (sw), SCM_NUMDIGS (sw), (SCM_BIGDIG) z);
return scm_i_normbig (sw);
} else {
#ifndef SCM_DIGSTOOBIG
long w = scm_pseudolong (z);
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
(SCM_BIGDIG *) & w, SCM_DIGSPERLONG,
SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0), 2);
#else
SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
scm_longdigs (z, zdigs);
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
zdigs, SCM_DIGSPERLONG,
SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0), 2);
#endif
}
}
} else if (SCM_BIGP (y)) {
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (x) ^ SCM_BIGSIGN (y), 2);
} else {
SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
}
} else {
SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG1, s_quotient);
}
}
SCM_GPROC (s_remainder, "remainder", 2, 0, 0, scm_remainder, g_remainder);
/* "Return the remainder of the numbers @var{x} and @var{y}.\n"
* "@lisp\n"
* "(remainder 13 4) @result{} 1\n"
* "(remainder -13 4) @result{} -1\n"
* "@end lisp"
*/
SCM
scm_remainder (SCM x, SCM y)
{
if (SCM_INUMP (x)) {
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_remainder);
} else {
long z = SCM_INUM (x) % yy;
return SCM_MAKINUM (z);
}
} else if (SCM_BIGP (y)) {
if (SCM_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM
&& scm_bigcomp (abs_most_negative_fixnum, y) == 0)
{
/* Special case: x == fixnum-min && y == abs (fixnum-min) */
return SCM_MAKINUM (0);
}
else
return x;
} else {
SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_remainder);
} else {
return scm_divbigint (x, yy, SCM_BIGSIGN (x), 0);
}
} else if (SCM_BIGP (y)) {
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (x), 0);
} else {
SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
}
} else {
SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG1, s_remainder);
}
}
SCM_GPROC (s_modulo, "modulo", 2, 0, 0, scm_modulo, g_modulo);
/* "Return the modulo of the numbers @var{x} and @var{y}.\n"
* "@lisp\n"
* "(modulo 13 4) @result{} 1\n"
* "(modulo -13 4) @result{} 3\n"
* "@end lisp"
*/
SCM
scm_modulo (SCM x, SCM y)
{
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_modulo);
} else {
long z = xx % yy;
return SCM_MAKINUM (((yy < 0) ? (z > 0) : (z < 0)) ? z + yy : z);
}
} else if (SCM_BIGP (y)) {
return (SCM_BIGSIGN (y) ? (xx > 0) : (xx < 0)) ? scm_sum (x, y) : x;
} else {
SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_modulo);
} else {
return scm_divbigint (x, yy, yy < 0,
(SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0)) ? 1 : 0);
}
} else if (SCM_BIGP (y)) {
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (y),
(SCM_BIGSIGN (x) ^ SCM_BIGSIGN (y)) ? 1 : 0);
} else {
SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
}
} else {
SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG1, s_modulo);
}
}
SCM_GPROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd, g_gcd);
/* "Return the greatest common divisor of all arguments.\n"
* "If called without arguments, 0 is returned."
*/
SCM
scm_gcd (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
return SCM_INUM0;
} else {
return x;
}
}
tailrec:
if (SCM_INUMP (x)) {
if (SCM_INUMP (y)) {
long xx = SCM_INUM (x);
long yy = SCM_INUM (y);
long u = xx < 0 ? -xx : xx;
long v = yy < 0 ? -yy : yy;
long result;
if (xx == 0) {
result = v;
} else if (yy == 0) {
result = u;
} else {
long k = 1;
long t;
/* Determine a common factor 2^k */
while (!(1 & (u | v))) {
k <<= 1;
u >>= 1;
v >>= 1;
}
/* Now, any factor 2^n can be eliminated */
if (u & 1) {
t = -v;
} else {
t = u;
b3:
t = SCM_SRS (t, 1);
}
if (!(1 & t))
goto b3;
if (t > 0)
u = t;
else
v = -t;
t = u - v;
if (t != 0)
goto b3;
result = u * k;
}
if (SCM_POSFIXABLE (result)) {
return SCM_MAKINUM (result);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (result);
#else
scm_num_overflow (s_gcd);
#endif
}
} else if (SCM_BIGP (y)) {
SCM_SWAP (x, y);
goto big_gcd;
} else {
SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
}
} else if (SCM_BIGP (x)) {
big_gcd:
if (SCM_BIGSIGN (x))
x = scm_i_copybig (x, 0);
newy:
if (SCM_INUMP (y)) {
if (SCM_EQ_P (y, SCM_INUM0)) {
return x;
} else {
goto swaprec;
}
} else if (SCM_BIGP (y)) {
if (SCM_BIGSIGN (y))
y = scm_i_copybig (y, 0);
switch (scm_bigcomp (x, y))
{
case -1: /* x > y */
swaprec:
{
SCM t = scm_remainder (x, y);
x = y;
y = t;
}
goto tailrec;
case 1: /* x < y */
y = scm_remainder (y, x);
goto newy;
default: /* x == y */
return x;
}
/* instead of the switch, we could just
return scm_gcd (y, scm_modulo (x, y)); */
} else {
SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
}
} else {
SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
}
}
SCM_GPROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm, g_lcm);
/* "Return the least common multiple of the arguments.\n"
* "If called without arguments, 1 is returned."
*/
SCM
scm_lcm (SCM n1, SCM n2)
{
if (SCM_UNBNDP (n2)) {
if (SCM_UNBNDP (n1)) {
return SCM_MAKINUM (1L);
} else {
n2 = SCM_MAKINUM (1L);
}
};
#ifndef SCM_BIGDIG
SCM_GASSERT2 (SCM_INUMP (n1), g_lcm, n1, n2, SCM_ARG1, s_lcm);
SCM_GASSERT2 (SCM_INUMP (n2), g_lcm, n1, n2, SCM_ARGn, s_lcm);
#else
SCM_GASSERT2 (SCM_INUMP (n1) || SCM_BIGP (n1),
g_lcm, n1, n2, SCM_ARG1, s_lcm);
SCM_GASSERT2 (SCM_INUMP (n2) || SCM_BIGP (n2),
g_lcm, n1, n2, SCM_ARGn, s_lcm);
#endif
{
SCM d = scm_gcd (n1, n2);
if (SCM_EQ_P (d, SCM_INUM0)) {
return d;
} else {
return scm_abs (scm_product (n1, scm_quotient (n2, d)));
}
}
}
#ifndef scm_long2num
#define SCM_LOGOP_RETURN(x) scm_ulong2num(x)
#else
#define SCM_LOGOP_RETURN(x) SCM_MAKINUM(x)
#endif
/* Emulating 2's complement bignums with sign magnitude arithmetic:
Logand:
X Y Result Method:
(len)
+ + + x (map digit:logand X Y)
+ - + x (map digit:logand X (lognot (+ -1 Y)))
- + + y (map digit:logand (lognot (+ -1 X)) Y)
- - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
Logior:
X Y Result Method:
+ + + (map digit:logior X Y)
+ - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
- + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
- - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
Logxor:
X Y Result Method:
+ + + (map digit:logxor X Y)
+ - - (+ 1 (map digit:logxor X (+ -1 Y)))
- + - (+ 1 (map digit:logxor (+ -1 X) Y))
- - + (map digit:logxor (+ -1 X) (+ -1 Y))
Logtest:
X Y Result
+ + (any digit:logand X Y)
+ - (any digit:logand X (lognot (+ -1 Y)))
- + (any digit:logand (lognot (+ -1 X)) Y)
- - #t
*/
#ifdef SCM_BIGDIG
SCM scm_copy_big_dec(SCM b, int sign);
SCM scm_copy_smaller(SCM_BIGDIG *x, size_t nx, int zsgn);
SCM scm_big_ior(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy);
SCM scm_big_xor(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy);
SCM scm_big_and(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy, int zsgn);
SCM scm_big_test(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy);
SCM scm_copy_big_dec(SCM b, int sign)
{
long num = -1;
size_t nx = SCM_NUMDIGS(b);
size_t i = 0;
SCM ans = scm_i_mkbig(nx, sign);
SCM_BIGDIG *src = SCM_BDIGITS(b), *dst = SCM_BDIGITS(ans);
if SCM_BIGSIGN(b) do {
num += src[i];
if (num < 0) {dst[i] = num + SCM_BIGRAD; num = -1;}
else {dst[i] = SCM_BIGLO(num); num = 0;}
} while (++i < nx);
else
while (nx--) dst[nx] = src[nx];
return ans;
}
SCM scm_copy_smaller(SCM_BIGDIG *x, size_t nx, int zsgn)
{
long num = -1;
size_t i = 0;
SCM z = scm_i_mkbig(nx, zsgn);
SCM_BIGDIG *zds = SCM_BDIGITS(z);
if (zsgn) do {
num += x[i];
if (num < 0) {zds[i] = num + SCM_BIGRAD; num = -1;}
else {zds[i] = SCM_BIGLO(num); num = 0;}
} while (++i < nx);
else do zds[i] = x[i]; while (++i < nx);
return z;
}
SCM scm_big_ior(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy)
/* Assumes nx <= SCM_NUMDIGS(bigy) */
/* Assumes xsgn equals either 0 or SCM_BIGSIGNFLAG */
{
long num = -1;
size_t i = 0, ny = SCM_NUMDIGS(bigy);
SCM z = scm_copy_big_dec (bigy, xsgn & SCM_BIGSIGN (bigy));
SCM_BIGDIG *zds = SCM_BDIGITS(z);
if (xsgn) {
do {
num += x[i];
if (num < 0) {zds[i] |= num + SCM_BIGRAD; num = -1;}
else {zds[i] |= SCM_BIGLO(num); num = 0;}
} while (++i < nx);
/* ========= Need to increment zds now =========== */
i = 0; num = 1;
while (i < ny) {
num += zds[i];
zds[i++] = SCM_BIGLO(num);
num = SCM_BIGDN(num);
if (!num) return z;
}
scm_i_adjbig(z, 1 + ny); /* OOPS, overflowed into next digit. */
SCM_BDIGITS(z)[ny] = 1;
return z;
}
else do zds[i] = zds[i] | x[i]; while (++i < nx);
return z;
}
SCM scm_big_xor(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy)
/* Assumes nx <= SCM_NUMDIGS(bigy) */
/* Assumes xsgn equals either 0 or SCM_BIGSIGNFLAG */
{
long num = -1;
size_t i = 0, ny = SCM_NUMDIGS(bigy);
SCM z = scm_copy_big_dec(bigy, xsgn ^ SCM_BIGSIGN(bigy));
SCM_BIGDIG *zds = SCM_BDIGITS(z);
if (xsgn) do {
num += x[i];
if (num < 0) {zds[i] ^= num + SCM_BIGRAD; num = -1;}
else {zds[i] ^= SCM_BIGLO(num); num = 0;}
} while (++i < nx);
else do {
zds[i] = zds[i] ^ x[i];
} while (++i < nx);
if (xsgn ^ SCM_BIGSIGN(bigy)) {
/* ========= Need to increment zds now =========== */
i = 0; num = 1;
while (i < ny) {
num += zds[i];
zds[i++] = SCM_BIGLO(num);
num = SCM_BIGDN(num);
if (!num) return scm_i_normbig(z);
}
}
return scm_i_normbig(z);
}
SCM scm_big_and(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy, int zsgn)
/* Assumes nx <= SCM_NUMDIGS(bigy) */
/* Assumes xsgn equals either 0 or SCM_BIGSIGNFLAG */
/* return sign equals either 0 or SCM_BIGSIGNFLAG */
{
long num = -1;
size_t i = 0;
SCM z;
SCM_BIGDIG *zds;
if (xsgn==zsgn) {
z = scm_copy_smaller(x, nx, zsgn);
x = SCM_BDIGITS(bigy);
xsgn = SCM_BIGSIGN(bigy);
}
else z = scm_copy_big_dec(bigy, zsgn);
zds = SCM_BDIGITS(z);
if (zsgn) {
if (xsgn) do {
num += x[i];
if (num < 0) {zds[i] &= num + SCM_BIGRAD; num = -1;}
else {zds[i] &= SCM_BIGLO(num); num = 0;}
} while (++i < nx);
else do zds[i] = zds[i] & ~x[i]; while (++i < nx);
/* ========= need to increment zds now =========== */
i = 0; num = 1;
while (i < nx) {
num += zds[i];
zds[i++] = SCM_BIGLO(num);
num = SCM_BIGDN(num);
if (!num) return scm_i_normbig(z);
}
}
else if (xsgn) {
unsigned long int carry = 1;
do {
unsigned long int mask = (SCM_BIGDIG) ~x[i] + carry;
zds[i] = zds[i] & (SCM_BIGDIG) mask;
carry = (mask >= SCM_BIGRAD) ? 1 : 0;
} while (++i < nx);
} else do zds[i] = zds[i] & x[i]; while (++i < nx);
return scm_i_normbig(z);
}
SCM scm_big_test(SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy)
/* Assumes nx <= SCM_NUMDIGS(bigy) */
/* Assumes xsgn equals either 0 or SCM_BIGSIGNFLAG */
{
SCM_BIGDIG *y;
size_t i = 0;
long num = -1;
if (SCM_BIGSIGN(bigy) & xsgn) return SCM_BOOL_T;
if (SCM_NUMDIGS(bigy) != nx && xsgn) return SCM_BOOL_T;
y = SCM_BDIGITS(bigy);
if (xsgn)
do {
num += x[i];
if (num < 0) {
if (y[i] & ~(num + SCM_BIGRAD)) return SCM_BOOL_T;
num = -1;
}
else {
if (y[i] & ~SCM_BIGLO(num)) return SCM_BOOL_T;
num = 0;
}
} while (++i < nx);
else if SCM_BIGSIGN(bigy)
do {
num += y[i];
if (num < 0) {
if (x[i] & ~(num + SCM_BIGRAD)) return SCM_BOOL_T;
num = -1;
}
else {
if (x[i] & ~SCM_BIGLO(num)) return SCM_BOOL_T;
num = 0;
}
} while (++i < nx);
else
do if (x[i] & y[i]) return SCM_BOOL_T;
while (++i < nx);
return SCM_BOOL_F;
}
#endif
SCM_DEFINE1 (scm_logand, "logand", scm_tc7_asubr,
(SCM n1, SCM n2),
"Return the bitwise AND of the integer arguments.\n\n"
"@lisp\n"
"(logand) @result{} -1\n"
"(logand 7) @result{} 7\n"
"(logand #b111 #b011 #\b001) @result{} 1\n"
"@end lisp")
#define FUNC_NAME s_scm_logand
{
long int nn1;
if (SCM_UNBNDP (n2)) {
if (SCM_UNBNDP (n1)) {
return SCM_MAKINUM (-1);
} else if (!SCM_NUMBERP (n1)) {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
#ifndef SCM_RECKLESS
} else if (SCM_NUMBERP (n1)) {
return n1;
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
#else
} else {
return n1;
#endif
}
}
if (SCM_INUMP (n1)) {
nn1 = SCM_INUM (n1);
if (SCM_INUMP (n2)) {
long nn2 = SCM_INUM (n2);
return SCM_MAKINUM (nn1 & nn2);
} else if SCM_BIGP (n2) {
intbig:
{
# ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (nn1);
if ((nn1 < 0) && SCM_BIGSIGN (n2)) {
return scm_big_ior ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
SCM_BIGSIGNFLAG, n2);
} else {
return scm_big_and ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2, 0);
}
# else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (nn1, zdigs);
if ((nn1 < 0) && SCM_BIGSIGN (n2)) {
return scm_big_ior (zdigs, SCM_DIGSPERLONG, SCM_BIGSIGNFLAG, n2);
} else {
return scm_big_and (zdigs, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2, 0);
}
# endif
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else if (SCM_BIGP (n1)) {
if (SCM_INUMP (n2)) {
SCM_SWAP (n1, n2);
nn1 = SCM_INUM (n1);
goto intbig;
} else if (SCM_BIGP (n2)) {
if (SCM_NUMDIGS (n1) > SCM_NUMDIGS (n2)) {
SCM_SWAP (n1, n2);
};
if ((SCM_BIGSIGN (n1)) && SCM_BIGSIGN (n2)) {
return scm_big_ior (SCM_BDIGITS (n1), SCM_NUMDIGS (n1),
SCM_BIGSIGNFLAG, n2);
} else {
return scm_big_and (SCM_BDIGITS (n1), SCM_NUMDIGS (n1),
SCM_BIGSIGN (n1), n2, 0);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
}
}
#undef FUNC_NAME
SCM_DEFINE1 (scm_logior, "logior", scm_tc7_asubr,
(SCM n1, SCM n2),
"Return the bitwise OR of the integer arguments.\n\n"
"@lisp\n"
"(logior) @result{} 0\n"
"(logior 7) @result{} 7\n"
"(logior #b000 #b001 #b011) @result{} 3\n"
"@end lisp")
#define FUNC_NAME s_scm_logior
{
long int nn1;
if (SCM_UNBNDP (n2)) {
if (SCM_UNBNDP (n1)) {
return SCM_INUM0;
#ifndef SCM_RECKLESS
} else if (SCM_NUMBERP (n1)) {
return n1;
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
#else
} else {
return n1;
#endif
}
}
if (SCM_INUMP (n1)) {
nn1 = SCM_INUM (n1);
if (SCM_INUMP (n2)) {
long nn2 = SCM_INUM (n2);
return SCM_MAKINUM (nn1 | nn2);
} else if (SCM_BIGP (n2)) {
intbig:
{
# ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (nn1);
if ((!(nn1 < 0)) && !SCM_BIGSIGN (n2)) {
return scm_big_ior ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2);
} else {
return scm_big_and ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2, SCM_BIGSIGNFLAG);
}
# else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (nn1, zdigs);
if ((!(nn1 < 0)) && !SCM_BIGSIGN (n2)) {
return scm_big_ior (zdigs, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2);
} else {
return scm_big_and (zdigs, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2, SCM_BIGSIGNFLAG);
}
# endif
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else if (SCM_BIGP (n1)) {
if (SCM_INUMP (n2)) {
SCM_SWAP (n1, n2);
nn1 = SCM_INUM (n1);
goto intbig;
} else if (SCM_BIGP (n2)) {
if (SCM_NUMDIGS (n1) > SCM_NUMDIGS (n2)) {
SCM_SWAP (n1, n2);
};
if ((!SCM_BIGSIGN (n1)) && !SCM_BIGSIGN (n2)) {
return scm_big_ior (SCM_BDIGITS (n1), SCM_NUMDIGS (n1),
SCM_BIGSIGN (n1), n2);
} else {
return scm_big_and (SCM_BDIGITS (n1), SCM_NUMDIGS (n1),
SCM_BIGSIGN (n1), n2, SCM_BIGSIGNFLAG);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
}
}
#undef FUNC_NAME
SCM_DEFINE1 (scm_logxor, "logxor", scm_tc7_asubr,
(SCM n1, SCM n2),
"Return the bitwise XOR of the integer arguments. A bit is\n"
"set in the result if it is set in an odd number of arguments.\n"
"@lisp\n"
"(logxor) @result{} 0\n"
"(logxor 7) @result{} 7\n"
"(logxor #b000 #b001 #b011) @result{} 2\n"
"(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
"@end lisp")
#define FUNC_NAME s_scm_logxor
{
long int nn1;
if (SCM_UNBNDP (n2)) {
if (SCM_UNBNDP (n1)) {
return SCM_INUM0;
#ifndef SCM_RECKLESS
} else if (SCM_NUMBERP (n1)) {
return n1;
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
#else
} else {
return n1;
#endif
}
}
if (SCM_INUMP (n1)) {
nn1 = SCM_INUM (n1);
if (SCM_INUMP (n2)) {
long nn2 = SCM_INUM (n2);
return SCM_MAKINUM (nn1 ^ nn2);
} else if (SCM_BIGP (n2)) {
intbig:
{
# ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (nn1);
return scm_big_xor ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2);
# else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (nn1, zdigs);
return scm_big_xor (zdigs, SCM_DIGSPERLONG,
(nn1 < 0) ? SCM_BIGSIGNFLAG : 0, n2);
# endif
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else if (SCM_BIGP (n1)) {
if (SCM_INUMP (n2)) {
SCM_SWAP (n1, n2);
nn1 = SCM_INUM (n1);
goto intbig;
} else if (SCM_BIGP (n2)) {
if (SCM_NUMDIGS(n1) > SCM_NUMDIGS(n2)) {
SCM_SWAP (n1, n2);
}
return scm_big_xor (SCM_BDIGITS (n1), SCM_NUMDIGS (n1),
SCM_BIGSIGN (n1), n2);
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
(SCM j, SCM k),
"@lisp\n"
"(logtest j k) @equiv{} (not (zero? (logand j k)))\n\n"
"(logtest #b0100 #b1011) @result{} #f\n"
"(logtest #b0100 #b0111) @result{} #t\n"
"@end lisp")
#define FUNC_NAME s_scm_logtest
{
long int nj;
if (SCM_INUMP (j)) {
nj = SCM_INUM (j);
if (SCM_INUMP (k)) {
long nk = SCM_INUM (k);
return SCM_BOOL (nj & nk);
} else if (SCM_BIGP (k)) {
intbig:
{
# ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (nj);
return scm_big_test ((SCM_BIGDIG *)&z, SCM_DIGSPERLONG,
(nj < 0) ? SCM_BIGSIGNFLAG : 0, k);
# else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (nj, zdigs);
return scm_big_test (zdigs, SCM_DIGSPERLONG,
(nj < 0) ? SCM_BIGSIGNFLAG : 0, k);
# endif
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
}
} else if (SCM_BIGP (j)) {
if (SCM_INUMP (k)) {
SCM_SWAP (j, k);
nj = SCM_INUM (j);
goto intbig;
} else if (SCM_BIGP (k)) {
if (SCM_NUMDIGS (j) > SCM_NUMDIGS (k)) {
SCM_SWAP (j, k);
}
return scm_big_test (SCM_BDIGITS (j), SCM_NUMDIGS (j),
SCM_BIGSIGN (j), k);
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
(SCM index, SCM j),
"@lisp\n"
"(logbit? index j) @equiv{} (logtest (integer-expt 2 index) j)\n\n"
"(logbit? 0 #b1101) @result{} #t\n"
"(logbit? 1 #b1101) @result{} #f\n"
"(logbit? 2 #b1101) @result{} #t\n"
"(logbit? 3 #b1101) @result{} #t\n"
"(logbit? 4 #b1101) @result{} #f\n"
"@end lisp")
#define FUNC_NAME s_scm_logbit_p
{
unsigned long int iindex;
SCM_VALIDATE_INUM_MIN (SCM_ARG1, index, 0);
iindex = (unsigned long int) SCM_INUM (index);
if (SCM_INUMP (j)) {
{
/* bits above what's in an inum follow the sign bit */
iindex = min (iindex, SCM_LONG_BIT - 1);
return SCM_BOOL ((1L << iindex) & SCM_INUM (j));
}
} else if (SCM_BIGP (j)) {
if (SCM_NUMDIGS (j) * SCM_BITSPERDIG < iindex) {
return SCM_BOOL_F;
} else if (SCM_BIGSIGN (j)) {
long num = -1;
size_t i = 0;
SCM_BIGDIG * x = SCM_BDIGITS (j);
size_t nx = iindex / SCM_BITSPERDIG;
while (1) {
num += x[i];
if (nx == i++) {
return SCM_BOOL (((1L << (iindex % SCM_BITSPERDIG)) & num) == 0);
} else if (num < 0) {
num = -1;
} else {
num = 0;
}
}
} else {
return SCM_BOOL (SCM_BDIGITS (j) [iindex / SCM_BITSPERDIG]
& (1L << (iindex % SCM_BITSPERDIG)));
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
(SCM n),
"Return the integer which is the ones-complement of the integer\n"
"argument.\n"
"\n"
"@lisp\n"
"(number->string (lognot #b10000000) 2)\n"
" @result{} \"-10000001\"\n"
"(number->string (lognot #b0) 2)\n"
" @result{} \"-1\"\n"
"@end lisp")
#define FUNC_NAME s_scm_lognot
{
return scm_difference (SCM_MAKINUM (-1L), n);
}
#undef FUNC_NAME
SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
(SCM n, SCM k),
"Return @var{n} raised to the non-negative integer exponent\n"
"@var{k}.\n"
"\n"
"@lisp\n"
"(integer-expt 2 5)\n"
" @result{} 32\n"
"(integer-expt -3 3)\n"
" @result{} -27\n"
"@end lisp")
#define FUNC_NAME s_scm_integer_expt
{
SCM acc = SCM_MAKINUM (1L);
int i2;
#ifdef SCM_BIGDIG
/* 0^0 == 1 according to R5RS */
if (SCM_EQ_P (n, SCM_INUM0) || SCM_EQ_P (n, acc))
return SCM_FALSEP (scm_zero_p(k)) ? n : acc;
else if (SCM_EQ_P (n, SCM_MAKINUM (-1L)))
return SCM_FALSEP (scm_even_p (k)) ? n : acc;
#endif
if (SCM_REALP (k))
{
double r = SCM_REAL_VALUE (k);
i2 = r;
if (i2 != r)
SCM_WRONG_TYPE_ARG (2, k);
}
else
SCM_VALIDATE_ULONG_COPY (2,k,i2);
if (i2 < 0)
{
i2 = -i2;
n = scm_divide (n, SCM_UNDEFINED);
}
while (1)
{
if (0 == i2)
return acc;
if (1 == i2)
return scm_product (acc, n);
if (i2 & 1)
acc = scm_product (acc, n);
n = scm_product (n, n);
i2 >>= 1;
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
(SCM n, SCM cnt),
"The function ash performs an arithmetic shift left by @var{cnt}\n"
"bits (or shift right, if @var{cnt} is negative). 'Arithmetic'\n"
"means, that the function does not guarantee to keep the bit\n"
"structure of @var{n}, but rather guarantees that the result\n"
"will always be rounded towards minus infinity. Therefore, the\n"
"results of ash and a corresponding bitwise shift will differ if\n"
"@var{n} is negative.\n"
"\n"
"Formally, the function returns an integer equivalent to\n"
"@code{(inexact->exact (floor (* @var{n} (expt 2 @var{cnt}))))}.\n"
"\n"
"@lisp\n"
"(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
"(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
"@end lisp")
#define FUNC_NAME s_scm_ash
{
long bits_to_shift;
#ifndef SCM_BIGDIG
SCM_VALIDATE_INUM (1, n)
#endif
SCM_VALIDATE_INUM (2, cnt);
bits_to_shift = SCM_INUM (cnt);
#ifdef SCM_BIGDIG
if (bits_to_shift < 0) {
/* Shift right by abs(cnt) bits. This is realized as a division by
div:=2^abs(cnt). However, to guarantee the floor rounding, negative
values require some special treatment.
*/
SCM div = scm_integer_expt (SCM_MAKINUM (2), SCM_MAKINUM (-bits_to_shift));
if (SCM_FALSEP (scm_negative_p (n)))
return scm_quotient (n, div);
else
return scm_sum (SCM_MAKINUM (-1L),
scm_quotient (scm_sum (SCM_MAKINUM (1L), n), div));
} else
/* Shift left is done by multiplication with 2^CNT */
return scm_product (n, scm_integer_expt (SCM_MAKINUM (2), cnt));
#else
if (bits_to_shift < 0)
/* Signed right shift (SCM_SRS does it right) by abs(cnt) bits. */
return SCM_MAKINUM (SCM_SRS (SCM_INUM (n), -bits_to_shift));
else {
/* Shift left, but make sure not to leave the range of inums */
SCM res = SCM_MAKINUM (SCM_INUM (n) << cnt);
if (SCM_INUM (res) >> cnt != SCM_INUM (n))
scm_num_overflow (FUNC_NAME);
return res;
}
#endif
}
#undef FUNC_NAME
SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
(SCM n, SCM start, SCM end),
"Return the integer composed of the @var{start} (inclusive)\n"
"through @var{end} (exclusive) bits of @var{n}. The\n"
"@var{start}th bit becomes the 0-th bit in the result.\n"
"\n"
"@lisp\n"
"(number->string (bit-extract #b1101101010 0 4) 2)\n"
" @result{} \"1010\"\n"
"(number->string (bit-extract #b1101101010 4 9) 2)\n"
" @result{} \"10110\"\n"
"@end lisp")
#define FUNC_NAME s_scm_bit_extract
{
unsigned long int istart, iend;
SCM_VALIDATE_INUM_MIN_COPY (2,start,0,istart);
SCM_VALIDATE_INUM_MIN_COPY (3, end, 0, iend);
SCM_ASSERT_RANGE (3, end, (iend >= istart));
if (SCM_INUMP (n)) {
long int in = SCM_INUM (n);
unsigned long int bits = iend - istart;
if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
{
/* Since we emulate two's complement encoded numbers, this special
* case requires us to produce a result that has more bits than can be
* stored in a fixnum. Thus, we fall back to the more general
* algorithm that is used for bignums.
*/
goto generalcase;
}
if (istart < SCM_I_FIXNUM_BIT)
{
in = in >> istart;
if (bits < SCM_I_FIXNUM_BIT)
return SCM_MAKINUM (in & ((1L << bits) - 1));
else /* we know: in >= 0 */
return SCM_MAKINUM (in);
}
else if (in < 0)
{
return SCM_MAKINUM (-1L & ((1L << bits) - 1));
}
else
{
return SCM_MAKINUM (0);
}
} else if (SCM_BIGP (n)) {
generalcase:
{
SCM num1 = SCM_MAKINUM (1L);
SCM num2 = SCM_MAKINUM (2L);
SCM bits = SCM_MAKINUM (iend - istart);
SCM mask = scm_difference (scm_integer_expt (num2, bits), num1);
return scm_logand (mask, scm_ash (n, SCM_MAKINUM (-istart)));
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
}
}
#undef FUNC_NAME
static const char scm_logtab[] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
};
SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
(SCM n),
"Return the number of bits in integer @var{n}. If integer is\n"
"positive, the 1-bits in its binary representation are counted.\n"
"If negative, the 0-bits in its two's-complement binary\n"
"representation are counted. If 0, 0 is returned.\n"
"\n"
"@lisp\n"
"(logcount #b10101010)\n"
" @result{} 4\n"
"(logcount 0)\n"
" @result{} 0\n"
"(logcount -2)\n"
" @result{} 1\n"
"@end lisp")
#define FUNC_NAME s_scm_logcount
{
if (SCM_INUMP (n)) {
unsigned long int c = 0;
long int nn = SCM_INUM (n);
if (nn < 0) {
nn = -1 - nn;
};
while (nn) {
c += scm_logtab[15 & nn];
nn >>= 4;
};
return SCM_MAKINUM (c);
} else if (SCM_BIGP (n)) {
if (SCM_BIGSIGN (n)) {
return scm_logcount (scm_difference (SCM_MAKINUM (-1L), n));
} else {
unsigned long int c = 0;
size_t i = SCM_NUMDIGS (n);
SCM_BIGDIG * ds = SCM_BDIGITS (n);
while (i--) {
SCM_BIGDIG d;
for (d = ds[i]; d; d >>= 4) {
c += scm_logtab[15 & d];
}
}
return SCM_MAKINUM (c);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
}
}
#undef FUNC_NAME
static const char scm_ilentab[] = {
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
};
SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
(SCM n),
"Return the number of bits necessary to represent @var{n}.\n"
"\n"
"@lisp\n"
"(integer-length #b10101010)\n"
" @result{} 8\n"
"(integer-length 0)\n"
" @result{} 0\n"
"(integer-length #b1111)\n"
" @result{} 4\n"
"@end lisp")
#define FUNC_NAME s_scm_integer_length
{
if (SCM_INUMP (n)) {
unsigned long int c = 0;
unsigned int l = 4;
long int nn = SCM_INUM (n);
if (nn < 0) {
nn = -1 - nn;
};
while (nn) {
c += 4;
l = scm_ilentab [15 & nn];
nn >>= 4;
};
return SCM_MAKINUM (c - 4 + l);
} else if (SCM_BIGP (n)) {
if (SCM_BIGSIGN (n)) {
return scm_integer_length (scm_difference (SCM_MAKINUM (-1L), n));
} else {
unsigned long int digs = SCM_NUMDIGS (n) - 1;
unsigned long int c = digs * SCM_BITSPERDIG;
unsigned int l = 4;
SCM_BIGDIG * ds = SCM_BDIGITS (n);
SCM_BIGDIG d = ds [digs];
while (d) {
c += 4;
l = scm_ilentab [15 & d];
d >>= 4;
};
return SCM_MAKINUM (c - 4 + l);
}
} else {
SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
}
}
#undef FUNC_NAME
#ifdef SCM_BIGDIG
static const char s_bignum[] = "bignum";
SCM
scm_i_mkbig (size_t nlen, int sign)
{
SCM v;
SCM_BIGDIG *base;
if (((nlen << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD) != nlen)
scm_memory_error (s_bignum);
base = scm_must_malloc (nlen * sizeof (SCM_BIGDIG), s_bignum);
SCM_NEWCELL (v);
SCM_SET_BIGNUM_BASE (v, base);
SCM_SETNUMDIGS (v, nlen, sign);
return v;
}
SCM
scm_i_big2inum (SCM b, size_t l)
{
unsigned long num = 0;
SCM_BIGDIG *tmp = SCM_BDIGITS (b);
while (l--)
num = SCM_BIGUP (num) + tmp[l];
if (!SCM_BIGSIGN (b))
{
if (SCM_POSFIXABLE (num))
return SCM_MAKINUM (num);
}
else if (num <= -SCM_MOST_NEGATIVE_FIXNUM)
return SCM_MAKINUM (-num);
return b;
}
static const char s_adjbig[] = "scm_i_adjbig";
SCM
scm_i_adjbig (SCM b, size_t nlen)
{
size_t nsiz = nlen;
if (((nsiz << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD) != nlen)
scm_memory_error (s_adjbig);
SCM_DEFER_INTS;
{
SCM_BIGDIG *digits
= ((SCM_BIGDIG *)
scm_must_realloc ((char *) SCM_BDIGITS (b),
(long) (SCM_NUMDIGS (b) * sizeof (SCM_BIGDIG)),
(long) (nsiz * sizeof (SCM_BIGDIG)), s_bignum));
SCM_SET_BIGNUM_BASE (b, digits);
SCM_SETNUMDIGS (b, nsiz, SCM_BIGSIGN (b));
}
SCM_ALLOW_INTS;
return b;
}
SCM
scm_i_normbig (SCM b)
{
#ifndef _UNICOS
size_t nlen = SCM_NUMDIGS (b);
#else
int nlen = SCM_NUMDIGS (b); /* unsigned nlen breaks on Cray when nlen => 0 */
#endif
SCM_BIGDIG *zds = SCM_BDIGITS (b);
while (nlen-- && !zds[nlen]);
nlen++;
if (nlen * SCM_BITSPERDIG / SCM_CHAR_BIT <= sizeof (SCM))
if (SCM_INUMP (b = scm_i_big2inum (b, (size_t) nlen)))
return b;
if (SCM_NUMDIGS (b) == nlen)
return b;
return scm_i_adjbig (b, (size_t) nlen);
}
SCM
scm_i_copybig (SCM b, int sign)
{
size_t i = SCM_NUMDIGS (b);
SCM ans = scm_i_mkbig (i, sign);
SCM_BIGDIG *src = SCM_BDIGITS (b), *dst = SCM_BDIGITS (ans);
while (i--)
dst[i] = src[i];
return ans;
}
int
scm_bigcomp (SCM x, SCM y)
{
int xsign = SCM_BIGSIGN (x);
int ysign = SCM_BIGSIGN (y);
size_t xlen, ylen;
/* Look at the signs, first. */
if (ysign < xsign)
return 1;
if (ysign > xsign)
return -1;
/* They're the same sign, so see which one has more digits. Note
that, if they are negative, the longer number is the lesser. */
ylen = SCM_NUMDIGS (y);
xlen = SCM_NUMDIGS (x);
if (ylen > xlen)
return (xsign) ? -1 : 1;
if (ylen < xlen)
return (xsign) ? 1 : -1;
/* They have the same number of digits, so find the most significant
digit where they differ. */
while (xlen)
{
--xlen;
if (SCM_BDIGITS (y)[xlen] != SCM_BDIGITS (x)[xlen])
/* Make the discrimination based on the digit that differs. */
return ((SCM_BDIGITS (y)[xlen] > SCM_BDIGITS (x)[xlen])
? (xsign ? -1 : 1)
: (xsign ? 1 : -1));
}
/* The numbers are identical. */
return 0;
}
#ifndef SCM_DIGSTOOBIG
long
scm_pseudolong (long x)
{
union
{
long l;
SCM_BIGDIG bd[SCM_DIGSPERLONG];
}
p;
size_t i = 0;
if (x < 0)
x = -x;
while (i < SCM_DIGSPERLONG)
{
p.bd[i++] = SCM_BIGLO (x);
x = SCM_BIGDN (x);
}
/* p.bd[0] = SCM_BIGLO(x); p.bd[1] = SCM_BIGDN(x); */
return p.l;
}
#else
void
scm_longdigs (long x, SCM_BIGDIG digs[])
{
size_t i = 0;
if (x < 0)
x = -x;
while (i < SCM_DIGSPERLONG)
{
digs[i++] = SCM_BIGLO (x);
x = SCM_BIGDN (x);
}
}
#endif
SCM
scm_addbig (SCM_BIGDIG *x, size_t nx, int xsgn, SCM bigy, int sgny)
{
/* Assumes nx <= SCM_NUMDIGS(bigy) */
/* Assumes xsgn and sgny scm_equal either 0 or SCM_BIGSIGNFLAG */
long num = 0;
size_t i = 0, ny = SCM_NUMDIGS (bigy);
SCM z = scm_i_copybig (bigy, SCM_BIGSIGN (bigy) ^ sgny);
SCM_BIGDIG *zds = SCM_BDIGITS (z);
if (xsgn ^ SCM_BIGSIGN (z))
{
do
{
num += (long) zds[i] - x[i];
if (num < 0)
{
zds[i] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[i] = SCM_BIGLO (num);
num = 0;
}
}
while (++i < nx);
if (num && nx == ny)
{
num = 1;
i = 0;
SCM_SET_CELL_WORD_0 (z, SCM_CELL_WORD_0 (z) ^ SCM_BIGSIGNFLAG);
do
{
num += (SCM_BIGRAD - 1) - zds[i];
zds[i++] = SCM_BIGLO (num);
num = SCM_BIGDN (num);
}
while (i < ny);
}
else
while (i < ny)
{
num += zds[i];
if (num < 0)
{
zds[i++] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[i++] = SCM_BIGLO (num);
num = 0;
}
}
}
else
{
do
{
num += (long) zds[i] + x[i];
zds[i++] = SCM_BIGLO (num);
num = SCM_BIGDN (num);
}
while (i < nx);
if (!num)
return z;
while (i < ny)
{
num += zds[i];
zds[i++] = SCM_BIGLO (num);
num = SCM_BIGDN (num);
if (!num)
return z;
}
if (num)
{
z = scm_i_adjbig (z, ny + 1);
SCM_BDIGITS (z)[ny] = num;
return z;
}
}
return scm_i_normbig (z);
}
SCM
scm_mulbig (SCM_BIGDIG *x, size_t nx, SCM_BIGDIG *y, size_t ny, int sgn)
{
size_t i = 0, j = nx + ny;
unsigned long n = 0;
SCM z = scm_i_mkbig (j, sgn);
SCM_BIGDIG *zds = SCM_BDIGITS (z);
while (j--)
zds[j] = 0;
do
{
j = 0;
if (x[i])
{
do
{
n += zds[i + j] + ((unsigned long) x[i] * y[j]);
zds[i + j++] = SCM_BIGLO (n);
n = SCM_BIGDN (n);
}
while (j < ny);
if (n)
{
zds[i + j] = n;
n = 0;
}
}
}
while (++i < nx);
return scm_i_normbig (z);
}
unsigned int
scm_divbigdig (SCM_BIGDIG * ds, size_t h, SCM_BIGDIG div)
{
register unsigned long t2 = 0;
while (h--)
{
t2 = SCM_BIGUP (t2) + ds[h];
ds[h] = t2 / div;
t2 %= div;
}
return t2;
}
static SCM
scm_divbigint (SCM x, long z, int sgn, int mode)
{
if (z < 0)
z = -z;
if (z < SCM_BIGRAD)
{
register unsigned long t2 = 0;
register SCM_BIGDIG *ds = SCM_BDIGITS (x);
size_t nd = SCM_NUMDIGS (x);
while (nd--)
t2 = (SCM_BIGUP (t2) + ds[nd]) % z;
if (mode && t2)
t2 = z - t2;
return SCM_MAKINUM (sgn ? -t2 : t2);
}
{
#ifndef SCM_DIGSTOOBIG
unsigned long t2 = scm_pseudolong (z);
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
(SCM_BIGDIG *) & t2, SCM_DIGSPERLONG,
sgn, mode);
#else
SCM_BIGDIG t2[SCM_DIGSPERLONG];
scm_longdigs (z, t2);
return scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
t2, SCM_DIGSPERLONG,
sgn, mode);
#endif
}
}
static SCM
scm_divbigbig (SCM_BIGDIG *x, size_t nx, SCM_BIGDIG *y, size_t ny, int sgn, int modes)
{
/* modes description
0 remainder
1 scm_modulo
2 quotient
3 quotient but returns SCM_UNDEFINED if division is not exact. */
size_t i = 0, j = 0;
long num = 0;
unsigned long t2 = 0;
SCM z, newy;
SCM_BIGDIG d = 0, qhat, *zds, *yds;
/* algorithm requires nx >= ny */
if (nx < ny)
switch (modes)
{
case 0: /* remainder -- just return x */
z = scm_i_mkbig (nx, sgn);
zds = SCM_BDIGITS (z);
do
{
zds[i] = x[i];
}
while (++i < nx);
return z;
case 1: /* scm_modulo -- return y-x */
z = scm_i_mkbig (ny, sgn);
zds = SCM_BDIGITS (z);
do
{
num += (long) y[i] - x[i];
if (num < 0)
{
zds[i] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[i] = num;
num = 0;
}
}
while (++i < nx);
while (i < ny)
{
num += y[i];
if (num < 0)
{
zds[i++] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[i++] = num;
num = 0;
}
}
goto doadj;
case 2:
return SCM_INUM0; /* quotient is zero */
case 3:
return SCM_UNDEFINED; /* the division is not exact */
}
z = scm_i_mkbig (nx == ny ? nx + 2 : nx + 1, sgn);
zds = SCM_BDIGITS (z);
if (nx == ny)
zds[nx + 1] = 0;
while (!y[ny - 1])
ny--; /* in case y came in as a psuedolong */
if (y[ny - 1] < (SCM_BIGRAD >> 1))
{ /* normalize operands */
d = SCM_BIGRAD / (y[ny - 1] + 1);
newy = scm_i_mkbig (ny, 0);
yds = SCM_BDIGITS (newy);
while (j < ny)
{
t2 += (unsigned long) y[j] * d;
yds[j++] = SCM_BIGLO (t2);
t2 = SCM_BIGDN (t2);
}
y = yds;
j = 0;
t2 = 0;
while (j < nx)
{
t2 += (unsigned long) x[j] * d;
zds[j++] = SCM_BIGLO (t2);
t2 = SCM_BIGDN (t2);
}
zds[j] = t2;
}
else
{
zds[j = nx] = 0;
while (j--)
zds[j] = x[j];
}
j = nx == ny ? nx + 1 : nx; /* dividend needs more digits than divisor */
do
{ /* loop over digits of quotient */
if (zds[j] == y[ny - 1])
qhat = SCM_BIGRAD - 1;
else
qhat = (SCM_BIGUP (zds[j]) + zds[j - 1]) / y[ny - 1];
if (!qhat)
continue;
i = 0;
num = 0;
t2 = 0;
do
{ /* multiply and subtract */
t2 += (unsigned long) y[i] * qhat;
num += zds[j - ny + i] - SCM_BIGLO (t2);
if (num < 0)
{
zds[j - ny + i] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[j - ny + i] = num;
num = 0;
}
t2 = SCM_BIGDN (t2);
}
while (++i < ny);
num += zds[j - ny + i] - t2; /* borrow from high digit; don't update */
while (num)
{ /* "add back" required */
i = 0;
num = 0;
qhat--;
do
{
num += (long) zds[j - ny + i] + y[i];
zds[j - ny + i] = SCM_BIGLO (num);
num = SCM_BIGDN (num);
}
while (++i < ny);
num--;
}
if (modes & 2)
zds[j] = qhat;
}
while (--j >= ny);
switch (modes)
{
case 3: /* check that remainder==0 */
for (j = ny; j && !zds[j - 1]; --j);
if (j)
return SCM_UNDEFINED;
case 2: /* move quotient down in z */
j = (nx == ny ? nx + 2 : nx + 1) - ny;
for (i = 0; i < j; i++)
zds[i] = zds[i + ny];
ny = i;
break;
case 1: /* subtract for scm_modulo */
i = 0;
num = 0;
j = 0;
do
{
num += y[i] - zds[i];
j = j | zds[i];
if (num < 0)
{
zds[i] = num + SCM_BIGRAD;
num = -1;
}
else
{
zds[i] = num;
num = 0;
}
}
while (++i < ny);
if (!j)
return SCM_INUM0;
case 0: /* just normalize remainder */
if (d)
scm_divbigdig (zds, ny, d);
}
doadj:
for (j = ny; j && !zds[j - 1]; --j);
if (j * SCM_BITSPERDIG <= sizeof (SCM) * SCM_CHAR_BIT)
if (SCM_INUMP (z = scm_i_big2inum (z, j)))
return z;
return scm_i_adjbig (z, j);
}
#endif
/*** NUMBERS -> STRINGS ***/
int scm_dblprec;
static const double fx[] =
{ 0.0, 5e-1, 5e-2, 5e-3, 5e-4, 5e-5,
5e-6, 5e-7, 5e-8, 5e-9, 5e-10,
5e-11, 5e-12, 5e-13, 5e-14, 5e-15,
5e-16, 5e-17, 5e-18, 5e-19, 5e-20};
static size_t
idbl2str (double f, char *a)
{
int efmt, dpt, d, i, wp = scm_dblprec;
size_t ch = 0;
int exp = 0;
if (f == 0.0)
goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */
if (f < 0.0)
{
f = -f;
a[ch++] = '-';
}
else if (f > 0.0);
else
goto funny;
if (IS_INF (f))
{
if (ch == 0)
a[ch++] = '+';
funny:
a[ch++] = '#';
a[ch++] = '.';
a[ch++] = '#';
return ch;
}
#ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
make-uniform-vector, from causing infinite loops. */
while (f < 1.0)
{
f *= 10.0;
if (exp-- < DBL_MIN_10_EXP)
goto funny;
}
while (f > 10.0)
{
f *= 0.10;
if (exp++ > DBL_MAX_10_EXP)
goto funny;
}
#else
while (f < 1.0)
{
f *= 10.0;
exp--;
}
while (f > 10.0)
{
f /= 10.0;
exp++;
}
#endif
if (f + fx[wp] >= 10.0)
{
f = 1.0;
exp++;
}
zero:
#ifdef ENGNOT
dpt = (exp + 9999) % 3;
exp -= dpt++;
efmt = 1;
#else
efmt = (exp < -3) || (exp > wp + 2);
if (!efmt)
{
if (exp < 0)
{
a[ch++] = '0';
a[ch++] = '.';
dpt = exp;
while (++dpt)
a[ch++] = '0';
}
else
dpt = exp + 1;
}
else
dpt = 1;
#endif
do
{
d = f;
f -= d;
a[ch++] = d + '0';
if (f < fx[wp])
break;
if (f + fx[wp] >= 1.0)
{
a[ch - 1]++;
break;
}
f *= 10.0;
if (!(--dpt))
a[ch++] = '.';
}
while (wp--);
if (dpt > 0)
{
#ifndef ENGNOT
if ((dpt > 4) && (exp > 6))
{
d = (a[0] == '-' ? 2 : 1);
for (i = ch++; i > d; i--)
a[i] = a[i - 1];
a[d] = '.';
efmt = 1;
}
else
#endif
{
while (--dpt)
a[ch++] = '0';
a[ch++] = '.';
}
}
if (a[ch - 1] == '.')
a[ch++] = '0'; /* trailing zero */
if (efmt && exp)
{
a[ch++] = 'e';
if (exp < 0)
{
exp = -exp;
a[ch++] = '-';
}
for (i = 10; i <= exp; i *= 10);
for (i /= 10; i; i /= 10)
{
a[ch++] = exp / i + '0';
exp %= i;
}
}
return ch;
}
static size_t
iflo2str (SCM flt, char *str)
{
size_t i;
if (SCM_SLOPPY_REALP (flt))
i = idbl2str (SCM_REAL_VALUE (flt), str);
else
{
i = idbl2str (SCM_COMPLEX_REAL (flt), str);
if (SCM_COMPLEX_IMAG (flt) != 0.0)
{
if (0 <= SCM_COMPLEX_IMAG (flt))
str[i++] = '+';
i += idbl2str (SCM_COMPLEX_IMAG (flt), &str[i]);
str[i++] = 'i';
}
}
return i;
}
/* convert a long to a string (unterminated). returns the number of
characters in the result.
rad is output base
p is destination: worst case (base 2) is SCM_INTBUFLEN */
size_t
scm_iint2str (long num, int rad, char *p)
{
size_t j = 1;
size_t i;
unsigned long n = (num < 0) ? -num : num;
for (n /= rad; n > 0; n /= rad)
j++;
i = j;
if (num < 0)
{
*p++ = '-';
j++;
n = -num;
}
else
n = num;
while (i--)
{
int d = n % rad;
n /= rad;
p[i] = d + ((d < 10) ? '0' : 'a' - 10);
}
return j;
}
#ifdef SCM_BIGDIG
static SCM
big2str (SCM b, unsigned int radix)
{
SCM t = scm_i_copybig (b, 0); /* sign of temp doesn't matter */
register SCM_BIGDIG *ds = SCM_BDIGITS (t);
size_t i = SCM_NUMDIGS (t);
size_t j = radix == 16 ? (SCM_BITSPERDIG * i) / 4 + 2
: radix >= 10 ? (SCM_BITSPERDIG * i * 241L) / 800 + 2
: (SCM_BITSPERDIG * i) + 2;
size_t k = 0;
size_t radct = 0;
SCM_BIGDIG radpow = 1, radmod = 0;
SCM ss = scm_allocate_string (j);
char *s = SCM_STRING_CHARS (ss), c;
while ((long) radpow * radix < SCM_BIGRAD)
{
radpow *= radix;
radct++;
}
while ((i || radmod) && j)
{
if (k == 0)
{
radmod = (SCM_BIGDIG) scm_divbigdig (ds, i, radpow);
k = radct;
if (!ds[i - 1])
i--;
}
c = radmod % radix;
radmod /= radix;
k--;
s[--j] = c < 10 ? c + '0' : c + 'a' - 10;
}
if (SCM_BIGSIGN (b))
s[--j] = '-';
if (j > 0)
{
/* The pre-reserved string length was too large. */
unsigned long int length = SCM_STRING_LENGTH (ss);
ss = scm_substring (ss, SCM_MAKINUM (j), SCM_MAKINUM (length));
}
return scm_return_first (ss, t);
}
#endif
SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
(SCM n, SCM radix),
"Return a string holding the external representation of the\n"
"number @var{n} in the given @var{radix}. If @var{n} is\n"
"inexact, a radix of 10 will be used.")
#define FUNC_NAME s_scm_number_to_string
{
int base;
if (SCM_UNBNDP (radix)) {
base = 10;
} else {
SCM_VALIDATE_INUM (2, radix);
base = SCM_INUM (radix);
SCM_ASSERT_RANGE (2, radix, base >= 2);
}
if (SCM_INUMP (n)) {
char num_buf [SCM_INTBUFLEN];
size_t length = scm_iint2str (SCM_INUM (n), base, num_buf);
return scm_mem2string (num_buf, length);
} else if (SCM_BIGP (n)) {
return big2str (n, (unsigned int) base);
} else if (SCM_INEXACTP (n)) {
char num_buf [FLOBUFLEN];
return scm_mem2string (num_buf, iflo2str (n, num_buf));
} else {
SCM_WRONG_TYPE_ARG (1, n);
}
}
#undef FUNC_NAME
/* These print routines are stubbed here so that scm_repl.c doesn't need
SCM_BIGDIG conditionals */
int
scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
{
char num_buf[FLOBUFLEN];
scm_lfwrite (num_buf, iflo2str (sexp, num_buf), port);
return !0;
}
int
scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
{
char num_buf[FLOBUFLEN];
scm_lfwrite (num_buf, iflo2str (sexp, num_buf), port);
return !0;
}
int
scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
{
#ifdef SCM_BIGDIG
exp = big2str (exp, (unsigned int) 10);
scm_lfwrite (SCM_STRING_CHARS (exp), (size_t) SCM_STRING_LENGTH (exp), port);
#else
scm_ipruk ("bignum", exp, port);
#endif
return !0;
}
/*** END nums->strs ***/
/*** STRINGS -> NUMBERS ***/
static SCM
scm_small_istr2int (char *str, long len, long radix)
{
register long n = 0, ln;
register int c;
register int i = 0;
int lead_neg = 0;
if (0 >= len)
return SCM_BOOL_F; /* zero scm_length */
switch (*str)
{ /* leading sign */
case '-':
lead_neg = 1;
case '+':
if (++i == len)
return SCM_BOOL_F; /* bad if lone `+' or `-' */
}
do
{
switch (c = str[i++])
{
case DIGITS:
c = c - '0';
goto accumulate;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
c = c - 'A' + 10;
goto accumulate;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
c = c - 'a' + 10;
accumulate:
if (c >= radix)
return SCM_BOOL_F; /* bad digit for radix */
ln = n;
n = n * radix - c;
/* Negation is a workaround for HP700 cc bug */
if (n > ln || (-n > -SCM_MOST_NEGATIVE_FIXNUM))
goto ovfl;
break;
default:
return SCM_BOOL_F; /* not a digit */
}
}
while (i < len);
if (!lead_neg)
if ((n = -n) > SCM_MOST_POSITIVE_FIXNUM)
goto ovfl;
return SCM_MAKINUM (n);
ovfl: /* overflow scheme integer */
return SCM_BOOL_F;
}
SCM
scm_istr2int (char *str, long len, long radix)
{
size_t j;
register size_t k, blen = 1;
size_t i = 0;
int c;
SCM res;
register SCM_BIGDIG *ds;
register unsigned long t2;
if (0 >= len)
return SCM_BOOL_F; /* zero scm_length */
/* Short numbers we parse directly into an int, to avoid the overhead
of creating a bignum. */
if (len < 6)
return scm_small_istr2int (str, len, radix);
/* table[] is the number of bits used by each digit in the given base,
ie. log(base)/log(2). A scale factor of 25 is applied, so eg. base 8
has 75 for 3 bits per digit. When the number is not exact (any non
power-of-2 base) it's rounded up, ensuring the size calculated will be
no less than what's needed. Eg. 25*log(10)/log(2) is 83.04 which gets
rounded up to 84. The following spot of perl generates the table
use POSIX;
foreach $i (2 .. 16) {
print POSIX::ceil(log($i)/log(2)*25),", /","* $i *","/\n";
}
The factor 25 is more or less arbitrary, it gives enough precision and
is what the code had in the past for base 10. */
{
static const unsigned table[] = {
25, /* 2 */
40, /* 3 */
50, /* 4 */
59, /* 5 */
65, /* 6 */
71, /* 7 */
75, /* 8 */
80, /* 9 */
84, /* 10 */
87, /* 11 */
90, /* 12 */
93, /* 13 */
96, /* 14 */
98, /* 15 */
100, /* 16 */
};
/* FIXME: What is sizeof(char) for? */
j = 1 + (table[radix-2] * len * sizeof (char)) / (SCM_BITSPERDIG * 25);
}
switch (str[0])
{ /* leading sign */
case '-':
case '+':
if (++i == (unsigned) len)
return SCM_BOOL_F; /* bad if lone `+' or `-' */
}
res = scm_i_mkbig (j, '-' == str[0]);
ds = SCM_BDIGITS (res);
for (k = j; k--;)
ds[k] = 0;
do
{
switch (c = str[i++])
{
case DIGITS:
c = c - '0';
goto accumulate;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
c = c - 'A' + 10;
goto accumulate;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
c = c - 'a' + 10;
accumulate:
if (c >= radix)
return SCM_BOOL_F; /* bad digit for radix */
k = 0;
t2 = c;
moretodo:
while (k < blen)
{
/* printf ("k = %d, blen = %d, t2 = %ld, ds[k] = %d\n", k, blen, t2, ds[k]); */
t2 += ds[k] * radix;
ds[k++] = SCM_BIGLO (t2);
t2 = SCM_BIGDN (t2);
}
if (t2)
{
if (blen >= j)
scm_num_overflow ("bignum");
blen++;
goto moretodo;
}
break;
default:
return SCM_BOOL_F; /* not a digit */
}
}
while (i < (unsigned) len);
if (blen * SCM_BITSPERDIG / SCM_CHAR_BIT <= sizeof (SCM))
if (SCM_INUMP (res = scm_i_big2inum (res, blen)))
return res;
if (j == blen)
return res;
return scm_i_adjbig (res, blen);
}
SCM
scm_istr2flo (char *str, long len, long radix)
{
register int c, i = 0;
double lead_sgn;
double res = 0.0, tmp = 0.0;
int flg = 0;
int point = 0;
SCM second;
if (i >= len)
return SCM_BOOL_F; /* zero scm_length */
switch (*str)
{ /* leading sign */
case '-':
lead_sgn = -1.0;
i++;
break;
case '+':
lead_sgn = 1.0;
i++;
break;
default:
lead_sgn = 0.0;
}
if (i == len)
return SCM_BOOL_F; /* bad if lone `+' or `-' */
if (str[i] == 'i' || str[i] == 'I')
{ /* handle `+i' and `-i' */
if (lead_sgn == 0.0)
return SCM_BOOL_F; /* must have leading sign */
if (++i < len)
return SCM_BOOL_F; /* `i' not last character */
return scm_make_complex (0.0, lead_sgn);
}
do
{ /* check initial digits */
switch (c = str[i])
{
case DIGITS:
c = c - '0';
goto accum1;
case 'D':
case 'E':
case 'F':
if (radix == 10)
goto out1; /* must be exponent */
case 'A':
case 'B':
case 'C':
c = c - 'A' + 10;
goto accum1;
case 'd':
case 'e':
case 'f':
if (radix == 10)
goto out1;
case 'a':
case 'b':
case 'c':
c = c - 'a' + 10;
accum1:
if (c >= radix)
return SCM_BOOL_F; /* bad digit for radix */
res = res * radix + c;
flg = 1; /* res is valid */
break;
default:
goto out1;
}
}
while (++i < len);
out1:
/* if true, then we did see a digit above, and res is valid */
if (i == len)
goto done;
/* By here, must have seen a digit,
or must have next char be a `.' with radix==10 */
if (!flg)
if (!(str[i] == '.' && radix == 10))
return SCM_BOOL_F;
while (str[i] == '#')
{ /* optional sharps */
res *= radix;
if (++i == len)
goto done;
}
if (str[i] == '/')
{
while (++i < len)
{
switch (c = str[i])
{
case DIGITS:
c = c - '0';
goto accum2;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
c = c - 'A' + 10;
goto accum2;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
c = c - 'a' + 10;
accum2:
if (c >= radix)
return SCM_BOOL_F;
tmp = tmp * radix + c;
break;
default:
goto out2;
}
}
out2:
if (tmp == 0.0)
return SCM_BOOL_F; /* `slash zero' not allowed */
if (i < len)
while (str[i] == '#')
{ /* optional sharps */
tmp *= radix;
if (++i == len)
break;
}
res /= tmp;
goto done;
}
if (str[i] == '.')
{ /* decimal point notation */
if (radix != 10)
return SCM_BOOL_F; /* must be radix 10 */
while (++i < len)
{
switch (c = str[i])
{
case DIGITS:
point--;
res = res * 10.0 + c - '0';
flg = 1;
break;
default:
goto out3;
}
}
out3:
if (!flg)
return SCM_BOOL_F; /* no digits before or after decimal point */
if (i == len)
goto adjust;
while (str[i] == '#')
{ /* ignore remaining sharps */
if (++i == len)
goto adjust;
}
}
switch (str[i])
{ /* exponent */
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
case 'l':
case 'L':
case 's':
case 'S':
{
int expsgn = 1, expon = 0;
if (radix != 10)
return SCM_BOOL_F; /* only in radix 10 */
if (++i == len)
return SCM_BOOL_F; /* bad exponent */
switch (str[i])
{
case '-':
expsgn = (-1);
case '+':
if (++i == len)
return SCM_BOOL_F; /* bad exponent */
}
if (str[i] < '0' || str[i] > '9')
return SCM_BOOL_F; /* bad exponent */
do
{
switch (c = str[i])
{
case DIGITS:
expon = expon * 10 + c - '0';
if (expon > SCM_MAXEXP)
scm_out_of_range ("string->number", SCM_MAKINUM (expon));
break;
default:
goto out4;
}
}
while (++i < len);
out4:
point += expsgn * expon;
}
}
adjust:
if (point >= 0)
while (point--)
res *= 10.0;
else
#ifdef _UNICOS
while (point++)
res *= 0.1;
#else
while (point++)
res /= 10.0;
#endif
done:
/* at this point, we have a legitimate floating point result */
if (lead_sgn == -1.0)
res = -res;
if (i == len)
return scm_make_real (res);
if (str[i] == 'i' || str[i] == 'I')
{ /* pure imaginary number */
if (lead_sgn == 0.0)
return SCM_BOOL_F; /* must have leading sign */
if (++i < len)
return SCM_BOOL_F; /* `i' not last character */
return scm_make_complex (0.0, res);
}
switch (str[i++])
{
case '-':
lead_sgn = -1.0;
break;
case '+':
lead_sgn = 1.0;
break;
case '@':
{ /* polar input for complex number */
/* get a `real' for scm_angle */
second = scm_istr2flo (&str[i], (long) (len - i), radix);
if (!SCM_SLOPPY_INEXACTP (second))
return SCM_BOOL_F; /* not `real' */
if (SCM_SLOPPY_COMPLEXP (second))
return SCM_BOOL_F; /* not `real' */
tmp = SCM_REAL_VALUE (second);
return scm_make_complex (res * cos (tmp), res * sin (tmp));
}
default:
return SCM_BOOL_F;
}
/* at this point, last char must be `i' */
if (str[len - 1] != 'i' && str[len - 1] != 'I')
return SCM_BOOL_F;
/* handles `x+i' and `x-i' */
if (i == (len - 1))
return scm_make_complex (res, lead_sgn);
/* get a `ureal' for complex part */
second = scm_istr2flo (&str[i], (long) ((len - i) - 1), radix);
if (!SCM_INEXACTP (second))
return SCM_BOOL_F; /* not `ureal' */
if (SCM_SLOPPY_COMPLEXP (second))
return SCM_BOOL_F; /* not `ureal' */
tmp = SCM_REAL_VALUE (second);
if (tmp < 0.0)
return SCM_BOOL_F; /* not `ureal' */
return scm_make_complex (res, (lead_sgn * tmp));
}
SCM
scm_istring2number (char *str, long len, long radix)
{
int i = 0;
char ex = 0;
char ex_p = 0, rx_p = 0; /* Only allow 1 exactness and 1 radix prefix */
SCM res;
if (len == 1)
if (*str == '+' || *str == '-') /* Catches lone `+' and `-' for speed */
return SCM_BOOL_F;
while ((len - i) >= 2 && str[i] == '#' && ++i)
switch (str[i++])
{
case 'b':
case 'B':
if (rx_p++)
return SCM_BOOL_F;
radix = 2;
break;
case 'o':
case 'O':
if (rx_p++)
return SCM_BOOL_F;
radix = 8;
break;
case 'd':
case 'D':
if (rx_p++)
return SCM_BOOL_F;
radix = 10;
break;
case 'x':
case 'X':
if (rx_p++)
return SCM_BOOL_F;
radix = 16;
break;
case 'i':
case 'I':
if (ex_p++)
return SCM_BOOL_F;
ex = 2;
break;
case 'e':
case 'E':
if (ex_p++)
return SCM_BOOL_F;
ex = 1;
break;
default:
return SCM_BOOL_F;
}
switch (ex)
{
case 1:
return scm_istr2int (&str[i], len - i, radix);
case 0:
res = scm_istr2int (&str[i], len - i, radix);
if (!SCM_FALSEP (res))
return res;
case 2:
return scm_istr2flo (&str[i], len - i, radix);
}
return SCM_BOOL_F;
}
SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
(SCM string, SCM radix),
"Return a number of the maximally precise representation\n"
"expressed by the given @var{string}. @var{radix} must be an\n"
"exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
"is a default radix that may be overridden by an explicit radix\n"
"prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
"supplied, then the default radix is 10. If string is not a\n"
"syntactically valid notation for a number, then\n"
"@code{string->number} returns @code{#f}.")
#define FUNC_NAME s_scm_string_to_number
{
SCM answer;
int base;
SCM_VALIDATE_STRING (1, string);
SCM_VALIDATE_INUM_MIN_DEF_COPY (2,radix,2,10,base);
answer = scm_istring2number (SCM_STRING_CHARS (string),
SCM_STRING_LENGTH (string),
base);
return scm_return_first (answer, string);
}
#undef FUNC_NAME
/*** END strs->nums ***/
SCM
scm_make_real (double x)
{
SCM z;
SCM_NEWCELL2 (z);
SCM_SET_CELL_TYPE (z, scm_tc16_real);
SCM_REAL_VALUE (z) = x;
return z;
}
SCM
scm_make_complex (double x, double y)
{
if (y == 0.0) {
return scm_make_real (x);
} else {
SCM z;
SCM_NEWSMOB (z, scm_tc16_complex, scm_must_malloc (2L * sizeof (double), "complex"));
SCM_COMPLEX_REAL (z) = x;
SCM_COMPLEX_IMAG (z) = y;
return z;
}
}
SCM
scm_bigequal (SCM x, SCM y)
{
#ifdef SCM_BIGDIG
if (0 == scm_bigcomp (x, y))
return SCM_BOOL_T;
#endif
return SCM_BOOL_F;
}
SCM
scm_real_equalp (SCM x, SCM y)
{
return SCM_BOOL (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
}
SCM
scm_complex_equalp (SCM x, SCM y)
{
return SCM_BOOL (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
&& SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
}
SCM_REGISTER_PROC (s_number_p, "number?", 1, 0, 0, scm_number_p);
/* "Return @code{#t} if @var{x} is a number, @code{#f}\n"
* "else. Note that the sets of complex, real, rational and\n"
* "integer values form subsets of the set of numbers, i. e. the\n"
* "predicate will be fulfilled for any number."
*/
SCM_DEFINE (scm_number_p, "complex?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
"otherwise. Note that the sets of real, rational and integer\n"
"values form subsets of the set of complex numbers, i. e. the\n"
"predicate will also be fulfilled if @var{x} is a real,\n"
"rational or integer number.")
#define FUNC_NAME s_scm_number_p
{
return SCM_BOOL (SCM_NUMBERP (x));
}
#undef FUNC_NAME
SCM_REGISTER_PROC (s_real_p, "real?", 1, 0, 0, scm_real_p);
/* "Return @code{#t} if @var{x} is a real number, @code{#f} else.\n"
* "Note that the sets of integer and rational values form a subset\n"
* "of the set of real numbers, i. e. the predicate will also\n"
* "be fulfilled if @var{x} is an integer or a rational number."
*/
SCM_DEFINE (scm_real_p, "rational?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
"otherwise. Note that the set of integer values forms a subset of\n"
"the set of rational numbers, i. e. the predicate will also be\n"
"fulfilled if @var{x} is an integer number. Real numbers\n"
"will also satisfy this predicate, because of their limited\n"
"precision.")
#define FUNC_NAME s_scm_real_p
{
if (SCM_INUMP (x)) {
return SCM_BOOL_T;
} else if (SCM_IMP (x)) {
return SCM_BOOL_F;
} else if (SCM_SLOPPY_REALP (x)) {
return SCM_BOOL_T;
} else if (SCM_BIGP (x)) {
return SCM_BOOL_T;
} else {
return SCM_BOOL_F;
}
}
#undef FUNC_NAME
SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
"else.")
#define FUNC_NAME s_scm_integer_p
{
double r;
if (SCM_INUMP (x))
return SCM_BOOL_T;
if (SCM_IMP (x))
return SCM_BOOL_F;
if (SCM_BIGP (x))
return SCM_BOOL_T;
if (!SCM_SLOPPY_INEXACTP (x))
return SCM_BOOL_F;
if (SCM_SLOPPY_COMPLEXP (x))
return SCM_BOOL_F;
r = SCM_REAL_VALUE (x);
if (r == floor (r))
return SCM_BOOL_T;
return SCM_BOOL_F;
}
#undef FUNC_NAME
SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
"else.")
#define FUNC_NAME s_scm_inexact_p
{
return SCM_BOOL (SCM_INEXACTP (x));
}
#undef FUNC_NAME
SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
/* "Return @code{#t} if all parameters are numerically equal." */
SCM
scm_num_eq_p (SCM x, SCM y)
{
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
return SCM_BOOL (xx == yy);
} else if (SCM_BIGP (y)) {
return SCM_BOOL_F;
} else if (SCM_REALP (y)) {
return SCM_BOOL ((double) xx == SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return SCM_BOOL (((double) xx == SCM_COMPLEX_REAL (y))
&& (0.0 == SCM_COMPLEX_IMAG (y)));
} else {
SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
return SCM_BOOL_F;
} else if (SCM_BIGP (y)) {
return SCM_BOOL (0 == scm_bigcomp (x, y));
} else if (SCM_REALP (y)) {
return SCM_BOOL (scm_i_big2dbl (x) == SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return SCM_BOOL ((scm_i_big2dbl (x) == SCM_COMPLEX_REAL (y))
&& (0.0 == SCM_COMPLEX_IMAG (y)));
} else {
SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) == (double) SCM_INUM (y));
} else if (SCM_BIGP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) == scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return SCM_BOOL ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
&& (0.0 == SCM_COMPLEX_IMAG (y)));
} else {
SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
}
} else if (SCM_COMPLEXP (x)) {
if (SCM_INUMP (y)) {
return SCM_BOOL ((SCM_COMPLEX_REAL (x) == (double) SCM_INUM (y))
&& (SCM_COMPLEX_IMAG (x) == 0.0));
} else if (SCM_BIGP (y)) {
return SCM_BOOL ((SCM_COMPLEX_REAL (x) == scm_i_big2dbl (y))
&& (SCM_COMPLEX_IMAG (x) == 0.0));
} else if (SCM_REALP (y)) {
return SCM_BOOL ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
&& (SCM_COMPLEX_IMAG (x) == 0.0));
} else if (SCM_COMPLEXP (y)) {
return SCM_BOOL ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
&& (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
} else {
SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
}
} else {
SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p);
}
}
SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
/* "Return @code{#t} if the list of parameters is monotonically\n"
* "increasing."
*/
SCM
scm_less_p (SCM x, SCM y)
{
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
return SCM_BOOL (xx < yy);
} else if (SCM_BIGP (y)) {
return SCM_BOOL (!SCM_BIGSIGN (y));
} else if (SCM_REALP (y)) {
return SCM_BOOL ((double) xx < SCM_REAL_VALUE (y));
} else {
SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
return SCM_BOOL (SCM_BIGSIGN (x));
} else if (SCM_BIGP (y)) {
return SCM_BOOL (1 == scm_bigcomp (x, y));
} else if (SCM_REALP (y)) {
return SCM_BOOL (scm_i_big2dbl (x) < SCM_REAL_VALUE (y));
} else {
SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) < (double) SCM_INUM (y));
} else if (SCM_BIGP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) < scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return SCM_BOOL (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
} else {
SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
}
} else {
SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p);
}
}
SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
/* "Return @code{#t} if the list of parameters is monotonically\n"
* "decreasing."
*/
#define FUNC_NAME s_scm_gr_p
SCM
scm_gr_p (SCM x, SCM y)
{
if (!SCM_NUMBERP (x))
SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
else if (!SCM_NUMBERP (y))
SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
else
return scm_less_p (y, x);
}
#undef FUNC_NAME
SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
/* "Return @code{#t} if the list of parameters is monotonically\n"
* "non-decreasing."
*/
#define FUNC_NAME s_scm_leq_p
SCM
scm_leq_p (SCM x, SCM y)
{
if (!SCM_NUMBERP (x))
SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
else if (!SCM_NUMBERP (y))
SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
else
return SCM_BOOL_NOT (scm_less_p (y, x));
}
#undef FUNC_NAME
SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
/* "Return @code{#t} if the list of parameters is monotonically\n"
* "non-increasing."
*/
#define FUNC_NAME s_scm_geq_p
SCM
scm_geq_p (SCM x, SCM y)
{
if (!SCM_NUMBERP (x))
SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
else if (!SCM_NUMBERP (y))
SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
else
return SCM_BOOL_NOT (scm_less_p (x, y));
}
#undef FUNC_NAME
SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
/* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
* "zero."
*/
SCM
scm_zero_p (SCM z)
{
if (SCM_INUMP (z)) {
return SCM_BOOL (SCM_EQ_P (z, SCM_INUM0));
} else if (SCM_BIGP (z)) {
return SCM_BOOL_F;
} else if (SCM_REALP (z)) {
return SCM_BOOL (SCM_REAL_VALUE (z) == 0.0);
} else if (SCM_COMPLEXP (z)) {
return SCM_BOOL (SCM_COMPLEX_REAL (z) == 0.0
&& SCM_COMPLEX_IMAG (z) == 0.0);
} else {
SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p);
}
}
SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
/* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
* "zero."
*/
SCM
scm_positive_p (SCM x)
{
if (SCM_INUMP (x)) {
return SCM_BOOL (SCM_INUM (x) > 0);
} else if (SCM_BIGP (x)) {
return SCM_BOOL (!SCM_BIGSIGN (x));
} else if (SCM_REALP (x)) {
return SCM_BOOL(SCM_REAL_VALUE (x) > 0.0);
} else {
SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p);
}
}
SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
/* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
* "zero."
*/
SCM
scm_negative_p (SCM x)
{
if (SCM_INUMP (x)) {
return SCM_BOOL (SCM_INUM (x) < 0);
} else if (SCM_BIGP (x)) {
return SCM_BOOL (SCM_BIGSIGN (x));
} else if (SCM_REALP (x)) {
return SCM_BOOL(SCM_REAL_VALUE (x) < 0.0);
} else {
SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p);
}
}
SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
/* "Return the maximum of all parameter values."
*/
SCM
scm_max (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
SCM_WTA_DISPATCH_0 (g_max, s_max);
} else if (SCM_NUMBERP (x)) {
return x;
} else {
SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
}
}
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
return (xx < yy) ? y : x;
} else if (SCM_BIGP (y)) {
return SCM_BIGSIGN (y) ? x : y;
} else if (SCM_REALP (y)) {
double z = xx;
return (z <= SCM_REAL_VALUE (y)) ? y : scm_make_real (z);
} else {
SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
return SCM_BIGSIGN (x) ? y : x;
} else if (SCM_BIGP (y)) {
return (1 == scm_bigcomp (x, y)) ? y : x;
} else if (SCM_REALP (y)) {
double z = scm_i_big2dbl (x);
return (z <= SCM_REAL_VALUE (y)) ? y : scm_make_real (z);
} else {
SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
double z = SCM_INUM (y);
return (SCM_REAL_VALUE (x) < z) ? scm_make_real (z) : x;
} else if (SCM_BIGP (y)) {
double z = scm_i_big2dbl (y);
return (SCM_REAL_VALUE (x) < z) ? scm_make_real (z) : x;
} else if (SCM_REALP (y)) {
return (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y)) ? y : x;
} else {
SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
}
} else {
SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
}
}
SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
/* "Return the minium of all parameter values."
*/
SCM
scm_min (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
SCM_WTA_DISPATCH_0 (g_min, s_min);
} else if (SCM_NUMBERP (x)) {
return x;
} else {
SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
}
}
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
return (xx < yy) ? x : y;
} else if (SCM_BIGP (y)) {
return SCM_BIGSIGN (y) ? y : x;
} else if (SCM_REALP (y)) {
double z = xx;
return (z < SCM_REAL_VALUE (y)) ? scm_make_real (z) : y;
} else {
SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
return SCM_BIGSIGN (x) ? x : y;
} else if (SCM_BIGP (y)) {
return (-1 == scm_bigcomp (x, y)) ? y : x;
} else if (SCM_REALP (y)) {
double z = scm_i_big2dbl (x);
return (z < SCM_REAL_VALUE (y)) ? scm_make_real (z) : y;
} else {
SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
double z = SCM_INUM (y);
return (SCM_REAL_VALUE (x) <= z) ? x : scm_make_real (z);
} else if (SCM_BIGP (y)) {
double z = scm_i_big2dbl (y);
return (SCM_REAL_VALUE (x) <= z) ? x : scm_make_real (z);
} else if (SCM_REALP (y)) {
return (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y)) ? x : y;
} else {
SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
}
} else {
SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
}
}
SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
/* "Return the sum of all parameter values. Return 0 if called without\n"
* "any parameters."
*/
SCM
scm_sum (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
return SCM_INUM0;
} else if (SCM_NUMBERP (x)) {
return x;
} else {
SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
}
}
if (SCM_INUMP (x)) {
long int xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long int yy = SCM_INUM (y);
long int z = xx + yy;
if (SCM_FIXABLE (z)) {
return SCM_MAKINUM (z);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (z);
#else /* SCM_BIGDIG */
return scm_make_real ((double) z);
#endif /* SCM_BIGDIG */
}
} else if (SCM_BIGP (y)) {
intbig:
{
long int xx = SCM_INUM (x);
#ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (xx);
return scm_addbig ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(xx < 0) ? SCM_BIGSIGNFLAG : 0, y, 0);
#else /* SCM_DIGSTOOBIG */
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (xx, zdigs);
return scm_addbig (zdigs, SCM_DIGSPERLONG,
(xx < 0) ? SCM_BIGSIGNFLAG : 0, y, 0);
#endif /* SCM_DIGSTOOBIG */
}
} else if (SCM_REALP (y)) {
return scm_make_real (xx + SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (xx + SCM_COMPLEX_REAL (y),
SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
SCM_SWAP (x, y);
goto intbig;
} else if (SCM_BIGP (y)) {
if (SCM_NUMDIGS (x) > SCM_NUMDIGS (y)) {
SCM_SWAP (x, y);
}
return scm_addbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BIGSIGN (x), y, 0);
} else if (SCM_REALP (y)) {
return scm_make_real (scm_i_big2dbl (x) + SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (scm_i_big2dbl (x) + SCM_COMPLEX_REAL (y),
SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) + SCM_INUM (y));
} else if (SCM_BIGP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) + scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
}
} else if (SCM_COMPLEXP (x)) {
if (SCM_INUMP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_INUM (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_BIGP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) + scm_i_big2dbl (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_REALP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
}
} else {
SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
}
}
SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
/* If called with one argument @var{z1}, -@var{z1} returned. Otherwise
* the sum of all but the first argument are subtracted from the first
* argument. */
#define FUNC_NAME s_difference
SCM
scm_difference (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
SCM_WTA_DISPATCH_0 (g_difference, s_difference);
} else if (SCM_INUMP (x)) {
long xx = -SCM_INUM (x);
if (SCM_FIXABLE (xx)) {
return SCM_MAKINUM (xx);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (xx);
#else
return scm_make_real ((double) xx);
#endif
}
} else if (SCM_BIGP (x)) {
SCM z = scm_i_copybig (x, !SCM_BIGSIGN (x));
unsigned int digs = SCM_NUMDIGS (z);
unsigned int size = digs * SCM_BITSPERDIG / SCM_CHAR_BIT;
return size <= sizeof (SCM) ? scm_i_big2inum (z, digs) : z;
} else if (SCM_REALP (x)) {
return scm_make_real (-SCM_REAL_VALUE (x));
} else if (SCM_COMPLEXP (x)) {
return scm_make_complex (-SCM_COMPLEX_REAL (x), -SCM_COMPLEX_IMAG (x));
} else {
SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
}
}
if (SCM_INUMP (x)) {
long int xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long int yy = SCM_INUM (y);
long int z = xx - yy;
if (SCM_FIXABLE (z)) {
return SCM_MAKINUM (z);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (z);
#else
return scm_make_real ((double) z);
#endif
}
} else if (SCM_BIGP (y)) {
#ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (xx);
return scm_addbig ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(xx < 0) ? SCM_BIGSIGNFLAG : 0, y, SCM_BIGSIGNFLAG);
#else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (xx, zdigs);
return scm_addbig (zdigs, SCM_DIGSPERLONG,
(xx < 0) ? SCM_BIGSIGNFLAG : 0, y, SCM_BIGSIGNFLAG);
#endif
} else if (SCM_REALP (y)) {
return scm_make_real (xx - SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (xx - SCM_COMPLEX_REAL (y),
-SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
long int yy = SCM_INUM (y);
#ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (yy);
return scm_addbig ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
(yy < 0) ? 0 : SCM_BIGSIGNFLAG, x, 0);
#else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (yy, zdigs);
return scm_addbig (zdigs, SCM_DIGSPERLONG,
(yy < 0) ? 0 : SCM_BIGSIGNFLAG, x, 0);
#endif
} else if (SCM_BIGP (y)) {
return (SCM_NUMDIGS (x) < SCM_NUMDIGS (y))
? scm_addbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BIGSIGN (x), y, SCM_BIGSIGNFLAG)
: scm_addbig (SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (y) ^ SCM_BIGSIGNFLAG, x, 0);
} else if (SCM_REALP (y)) {
return scm_make_real (scm_i_big2dbl (x) - SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (scm_i_big2dbl (x) - SCM_COMPLEX_REAL (y),
- SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) - SCM_INUM (y));
} else if (SCM_BIGP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) - scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
-SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
}
} else if (SCM_COMPLEXP (x)) {
if (SCM_INUMP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_INUM (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_BIGP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) - scm_i_big2dbl (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_REALP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
SCM_COMPLEX_IMAG (x));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
}
} else {
SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
}
}
#undef FUNC_NAME
SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
/* "Return the product of all arguments. If called without arguments,\n"
* "1 is returned."
*/
SCM
scm_product (SCM x, SCM y)
{
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
return SCM_MAKINUM (1L);
} else if (SCM_NUMBERP (x)) {
return x;
} else {
SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
}
}
if (SCM_INUMP (x)) {
long xx;
intbig:
xx = SCM_INUM (x);
if (xx == 0) {
return x;
} else if (xx == 1) {
return y;
}
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
long kk = xx * yy;
SCM k = SCM_MAKINUM (kk);
if (kk != SCM_INUM (k) || kk / xx != yy) {
#ifdef SCM_BIGDIG
int sgn = (xx < 0) ^ (yy < 0);
#ifndef SCM_DIGSTOOBIG
long i = scm_pseudolong (xx);
long j = scm_pseudolong (yy);
return scm_mulbig ((SCM_BIGDIG *) & i, SCM_DIGSPERLONG,
(SCM_BIGDIG *) & j, SCM_DIGSPERLONG, sgn);
#else /* SCM_DIGSTOOBIG */
SCM_BIGDIG xdigs [SCM_DIGSPERLONG];
SCM_BIGDIG ydigs [SCM_DIGSPERLONG];
scm_longdigs (xx, xdigs);
scm_longdigs (yy, ydigs);
return scm_mulbig (xdigs, SCM_DIGSPERLONG,
ydigs, SCM_DIGSPERLONG,
sgn);
#endif
#else
return scm_make_real (((double) xx) * ((double) yy));
#endif
} else {
return k;
}
} else if (SCM_BIGP (y)) {
#ifndef SCM_DIGSTOOBIG
long z = scm_pseudolong (xx);
return scm_mulbig ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (y) ? (xx > 0) : (xx < 0));
#else
SCM_BIGDIG zdigs [SCM_DIGSPERLONG];
scm_longdigs (xx, zdigs);
return scm_mulbig (zdigs, SCM_DIGSPERLONG,
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (y) ? (xx > 0) : (xx < 0));
#endif
} else if (SCM_REALP (y)) {
return scm_make_real (xx * SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (xx * SCM_COMPLEX_REAL (y),
xx * SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
SCM_SWAP (x, y);
goto intbig;
} else if (SCM_BIGP (y)) {
return scm_mulbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (x) ^ SCM_BIGSIGN (y));
} else if (SCM_REALP (y)) {
return scm_make_real (scm_i_big2dbl (x) * SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
double z = scm_i_big2dbl (x);
return scm_make_complex (z * SCM_COMPLEX_REAL (y),
z * SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
}
} else if (SCM_REALP (x)) {
if (SCM_INUMP (y)) {
return scm_make_real (SCM_INUM (y) * SCM_REAL_VALUE (x));
} else if (SCM_BIGP (y)) {
return scm_make_real (scm_i_big2dbl (y) * SCM_REAL_VALUE (x));
} else if (SCM_REALP (y)) {
return scm_make_real (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
} else {
SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
}
} else if (SCM_COMPLEXP (x)) {
if (SCM_INUMP (y)) {
return scm_make_complex (SCM_INUM (y) * SCM_COMPLEX_REAL (x),
SCM_INUM (y) * SCM_COMPLEX_IMAG (x));
} else if (SCM_BIGP (y)) {
double z = scm_i_big2dbl (y);
return scm_make_complex (z * SCM_COMPLEX_REAL (x),
z * SCM_COMPLEX_IMAG (x));
} else if (SCM_REALP (y)) {
return scm_make_complex (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
} else if (SCM_COMPLEXP (y)) {
return scm_make_complex (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
- SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
+ SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
} else {
SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
}
} else {
SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
}
}
double
scm_num2dbl (SCM a, const char *why)
#define FUNC_NAME why
{
if (SCM_INUMP (a)) {
return (double) SCM_INUM (a);
} else if (SCM_BIGP (a)) {
return scm_i_big2dbl (a);
} else if (SCM_REALP (a)) {
return (SCM_REAL_VALUE (a));
} else {
SCM_WRONG_TYPE_ARG (SCM_ARGn, a);
}
}
#undef FUNC_NAME
/* The code below for complex division is adapted from the GNU
libstdc++, which adapted it from f2c's libF77, and is subject to
this copyright: */
/****************************************************************
Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T Bell Laboratories or
Bellcore or any of their entities not be used in advertising or
publicity pertaining to distribution of the software without
specific, written prior permission.
AT&T and Bellcore disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall AT&T or Bellcore be liable for
any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
****************************************************************/
SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
/* Divide the first argument by the product of the remaining
arguments. If called with one argument @var{z1}, 1/@var{z1} is
returned. */
#define FUNC_NAME s_divide
SCM
scm_divide (SCM x, SCM y)
{
double a;
if (SCM_UNBNDP (y)) {
if (SCM_UNBNDP (x)) {
SCM_WTA_DISPATCH_0 (g_divide, s_divide);
} else if (SCM_INUMP (x)) {
if (SCM_EQ_P (x, SCM_MAKINUM (1L)) || SCM_EQ_P (x, SCM_MAKINUM (-1L))) {
return x;
} else {
return scm_make_real (1.0 / (double) SCM_INUM (x));
}
} else if (SCM_BIGP (x)) {
return scm_make_real (1.0 / scm_i_big2dbl (x));
} else if (SCM_REALP (x)) {
return scm_make_real (1.0 / SCM_REAL_VALUE (x));
} else if (SCM_COMPLEXP (x)) {
double r = SCM_COMPLEX_REAL (x);
double i = SCM_COMPLEX_IMAG (x);
if (r <= i) {
double t = r / i;
double d = i * (1.0 + t * t);
return scm_make_complex (t / d, -1.0 / d);
} else {
double t = i / r;
double d = r * (1.0 + t * t);
return scm_make_complex (1.0 / d, -t / d);
}
} else {
SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
}
}
if (SCM_INUMP (x)) {
long xx = SCM_INUM (x);
if (SCM_INUMP (y)) {
long yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_divide);
} else if (xx % yy != 0) {
return scm_make_real ((double) xx / (double) yy);
} else {
long z = xx / yy;
if (SCM_FIXABLE (z)) {
return SCM_MAKINUM (z);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (z);
#else
return scm_make_real ((double) xx / (double) yy);
#endif
}
}
} else if (SCM_BIGP (y)) {
return scm_make_real ((double) xx / scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return scm_make_real ((double) xx / SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
a = xx;
complex_div: /* y _must_ be a complex number */
{
double r = SCM_COMPLEX_REAL (y);
double i = SCM_COMPLEX_IMAG (y);
if (r <= i) {
double t = r / i;
double d = i * (1.0 + t * t);
return scm_make_complex ((a * t) / d, -a / d);
} else {
double t = i / r;
double d = r * (1.0 + t * t);
return scm_make_complex (a / d, -(a * t) / d);
}
}
} else {
SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
}
} else if (SCM_BIGP (x)) {
if (SCM_INUMP (y)) {
long int yy = SCM_INUM (y);
if (yy == 0) {
scm_num_overflow (s_divide);
} else if (yy == 1) {
return x;
} else {
long z = yy < 0 ? -yy : yy;
if (z < SCM_BIGRAD) {
SCM w = scm_i_copybig (x, SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0));
return scm_divbigdig (SCM_BDIGITS (w), SCM_NUMDIGS (w),
(SCM_BIGDIG) z)
? scm_make_real (scm_i_big2dbl (x) / (double) yy)
: scm_i_normbig (w);
} else {
SCM w;
#ifndef SCM_DIGSTOOBIG
z = scm_pseudolong (z);
w = scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
(SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0), 3);
#else
SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
scm_longdigs (z, zdigs);
w = scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
zdigs, SCM_DIGSPERLONG,
SCM_BIGSIGN (x) ? (yy > 0) : (yy < 0), 3);
#endif
return (!SCM_UNBNDP (w))
? w
: scm_make_real (scm_i_big2dbl (x) / (double) yy);
}
}
} else if (SCM_BIGP (y)) {
SCM w = scm_divbigbig (SCM_BDIGITS (x), SCM_NUMDIGS (x),
SCM_BDIGITS (y), SCM_NUMDIGS (y),
SCM_BIGSIGN (x) ^ SCM_BIGSIGN (y), 3);
return (!SCM_UNBNDP (w))
? w
: scm_make_real (scm_i_big2dbl (x) / scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return scm_make_real (scm_i_big2dbl (x) / SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
a = scm_i_big2dbl (x);
goto complex_div;
} else {
SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
}
} else if (SCM_REALP (x)) {
double rx = SCM_REAL_VALUE (x);
if (SCM_INUMP (y)) {
return scm_make_real (rx / (double) SCM_INUM (y));
} else if (SCM_BIGP (y)) {
return scm_make_real (rx / scm_i_big2dbl (y));
} else if (SCM_REALP (y)) {
return scm_make_real (rx / SCM_REAL_VALUE (y));
} else if (SCM_COMPLEXP (y)) {
a = rx;
goto complex_div;
} else {
SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
}
} else if (SCM_COMPLEXP (x)) {
double rx = SCM_COMPLEX_REAL (x);
double ix = SCM_COMPLEX_IMAG (x);
if (SCM_INUMP (y)) {
double d = SCM_INUM (y);
return scm_make_complex (rx / d, ix / d);
} else if (SCM_BIGP (y)) {
double d = scm_i_big2dbl (y);
return scm_make_complex (rx / d, ix / d);
} else if (SCM_REALP (y)) {
double d = SCM_REAL_VALUE (y);
return scm_make_complex (rx / d, ix / d);
} else if (SCM_COMPLEXP (y)) {
double ry = SCM_COMPLEX_REAL (y);
double iy = SCM_COMPLEX_IMAG (y);
if (ry <= iy) {
double t = ry / iy;
double d = iy * (1.0 + t * t);
return scm_make_complex ((rx * t + ix) / d, (ix * t - rx) / d);
} else {
double t = iy / ry;
double d = ry * (1.0 + t * t);
return scm_make_complex ((rx + ix * t) / d, (ix - rx * t) / d);
}
} else {
SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
}
} else {
SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
}
}
#undef FUNC_NAME
SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_cxr, (SCM (*)()) scm_asinh, g_asinh);
/* "Return the inverse hyperbolic sine of @var{x}."
*/
double
scm_asinh (double x)
{
return log (x + sqrt (x * x + 1));
}
SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_cxr, (SCM (*)()) scm_acosh, g_acosh);
/* "Return the inverse hyperbolic cosine of @var{x}."
*/
double
scm_acosh (double x)
{
return log (x + sqrt (x * x - 1));
}
SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_cxr, (SCM (*)()) scm_atanh, g_atanh);
/* "Return the inverse hyperbolic tangent of @var{x}."
*/
double
scm_atanh (double x)
{
return 0.5 * log ((1 + x) / (1 - x));
}
SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0,
(SCM x),
"Round the inexact number @var{x} towards zero.")
#define FUNC_NAME s_scm_truncate_number
{
if (SCM_INUMP (x) || SCM_BIGP (x))
return x;
else if (SCM_REALP (x))
return scm_make_real (scm_truncate (SCM_REAL_VALUE (x)));
else
SCM_WTA_DISPATCH_1 (g_scm_truncate_number, x, 1, s_scm_truncate_number);
}
#undef FUNC_NAME
double
scm_truncate (double x)
{
if (x < 0.0)
return -floor (-x);
return floor (x);
}
/* scm_round is done using floor(x+0.5) to round to nearest and with
half-way case (ie. when x is an integer plus 0.5) going upwards. Then
half-way cases are identified and adjusted down if the round-upwards
didn't give the desired even integer.
"plus_half == result" identifies a half-way case. If plus_half, which is
x + 0.5, is an integer then x must be an integer plus 0.5.
An odd "result" value is identified with result/2 != floor(result/2).
This is done with plus_half, since that value is ready for use sooner in
a pipelined cpu, and we're already requiring plus_half == result.
Note however that we need to be careful when x is big and already an
integer. In that case "x+0.5" may round to an adjacent integer, causing
us to return such a value, incorrectly. For instance if the hardware is
in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
(ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
returned. Or if the hardware is in round-upwards mode, then other bigger
values like say x == 2^128 will see x+0.5 rounding up to the next higher
representable value, 2^128+2^76 (or whatever), again incorrect.
These bad roundings of x+0.5 are avoided by testing at the start whether
x is already an integer. If it is then clearly that's the desired result
already. And if it's not then the exponent must be small enough to allow
an 0.5 to be represented, and hence added without a bad rounding. */
double
scm_round (double x)
{
double plus_half, result;
if (x == floor (x))
return x;
plus_half = x + 0.5;
result = floor (plus_half);
/* Adjust so that the scm_round is towards even. */
return (plus_half == result && plus_half / 2 != floor (plus_half / 2))
? result - 1 : result;
}
SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0,
(SCM x),
"Round the number @var{x} towards the nearest integer. "
"When it is exactly halfway between two integers, "
"round towards the even one.")
#define FUNC_NAME s_scm_round_number
{
if (SCM_INUMP (x) || SCM_BIGP (x))
return x;
else if (SCM_REALP (x))
return scm_make_real (scm_round (SCM_REAL_VALUE (x)));
else
SCM_WTA_DISPATCH_1 (g_scm_round_number, x, 1, s_scm_round_number);
}
#undef FUNC_NAME
SCM_GPROC1 (s_exact_to_inexact, "exact->inexact", scm_tc7_cxr, (SCM (*)()) scm_exact_to_inexact, g_exact_to_inexact);
/* Convert the number @var{x} to its inexact representation.\n"
*/
double
scm_exact_to_inexact (double z)
{
return z;
}
SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
(SCM x),
"Round the number @var{x} towards minus infinity.")
#define FUNC_NAME s_scm_floor
{
if (SCM_INUMP (x) || SCM_BIGP (x))
return x;
else if (SCM_REALP (x))
return scm_make_real (floor (SCM_REAL_VALUE (x)));
else
SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
}
#undef FUNC_NAME
SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
(SCM x),
"Round the number @var{x} towards infinity.")
#define FUNC_NAME s_scm_ceiling
{
if (SCM_INUMP (x) || SCM_BIGP (x))
return x;
else if (SCM_REALP (x))
return scm_make_real (ceil (SCM_REAL_VALUE (x)));
else
SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
}
#undef FUNC_NAME
SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_cxr, (SCM (*)()) sqrt, g_i_sqrt);
/* "Return the square root of the real number @var{x}."
*/
SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_cxr, (SCM (*)()) fabs, g_i_abs);
/* "Return the absolute value of the real number @var{x}."
*/
SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_cxr, (SCM (*)()) exp, g_i_exp);
/* "Return the @var{x}th power of e."
*/
SCM_GPROC1 (s_i_log, "$log", scm_tc7_cxr, (SCM (*)()) log, g_i_log);
/* "Return the natural logarithm of the real number @var{x}."
*/
SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_cxr, (SCM (*)()) sin, g_i_sin);
/* "Return the sine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_cxr, (SCM (*)()) cos, g_i_cos);
/* "Return the cosine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_cxr, (SCM (*)()) tan, g_i_tan);
/* "Return the tangent of the real number @var{x}."
*/
SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_cxr, (SCM (*)()) asin, g_i_asin);
/* "Return the arc sine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_cxr, (SCM (*)()) acos, g_i_acos);
/* "Return the arc cosine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_cxr, (SCM (*)()) atan, g_i_atan);
/* "Return the arc tangent of the real number @var{x}."
*/
SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_cxr, (SCM (*)()) sinh, g_i_sinh);
/* "Return the hyperbolic sine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_cxr, (SCM (*)()) cosh, g_i_cosh);
/* "Return the hyperbolic cosine of the real number @var{x}."
*/
SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_cxr, (SCM (*)()) tanh, g_i_tanh);
/* "Return the hyperbolic tangent of the real number @var{x}."
*/
struct dpair
{
double x, y;
};
static void scm_two_doubles (SCM x,
SCM y,
const char *sstring,
struct dpair * xy);
static void
scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
{
if (SCM_INUMP (x)) {
xy->x = SCM_INUM (x);
} else if (SCM_BIGP (x)) {
xy->x = scm_i_big2dbl (x);
} else if (SCM_REALP (x)) {
xy->x = SCM_REAL_VALUE (x);
} else {
scm_wrong_type_arg (sstring, SCM_ARG1, x);
}
if (SCM_INUMP (y)) {
xy->y = SCM_INUM (y);
} else if (SCM_BIGP (y)) {
xy->y = scm_i_big2dbl (y);
} else if (SCM_REALP (y)) {
xy->y = SCM_REAL_VALUE (y);
} else {
scm_wrong_type_arg (sstring, SCM_ARG2, y);
}
}
SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
(SCM x, SCM y),
"Return @var{x} raised to the power of @var{y}. This\n"
"procedure does not accept complex arguments.")
#define FUNC_NAME s_scm_sys_expt
{
struct dpair xy;
scm_two_doubles (x, y, FUNC_NAME, &xy);
return scm_make_real (pow (xy.x, xy.y));
}
#undef FUNC_NAME
SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
(SCM x, SCM y),
"Return the arc tangent of the two arguments @var{x} and\n"
"@var{y}. This is similar to calculating the arc tangent of\n"
"@var{x} / @var{y}, except that the signs of both arguments\n"
"are used to determine the quadrant of the result. This\n"
"procedure does not accept complex arguments.")
#define FUNC_NAME s_scm_sys_atan2
{
struct dpair xy;
scm_two_doubles (x, y, FUNC_NAME, &xy);
return scm_make_real (atan2 (xy.x, xy.y));
}
#undef FUNC_NAME
SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
(SCM real, SCM imaginary),
"Return a complex number constructed of the given @var{real} and\n"
"@var{imaginary} parts.")
#define FUNC_NAME s_scm_make_rectangular
{
struct dpair xy;
scm_two_doubles (real, imaginary, FUNC_NAME, &xy);
return scm_make_complex (xy.x, xy.y);
}
#undef FUNC_NAME
SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
(SCM x, SCM y),
"Return the complex number @var{x} * e^(i * @var{y}).")
#define FUNC_NAME s_scm_make_polar
{
struct dpair xy;
scm_two_doubles (x, y, FUNC_NAME, &xy);
return scm_make_complex (xy.x * cos (xy.y), xy.x * sin (xy.y));
}
#undef FUNC_NAME
SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
/* "Return the real part of the number @var{z}."
*/
SCM
scm_real_part (SCM z)
{
if (SCM_INUMP (z)) {
return z;
} else if (SCM_BIGP (z)) {
return z;
} else if (SCM_REALP (z)) {
return z;
} else if (SCM_COMPLEXP (z)) {
return scm_make_real (SCM_COMPLEX_REAL (z));
} else {
SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part);
}
}
SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
/* "Return the imaginary part of the number @var{z}."
*/
SCM
scm_imag_part (SCM z)
{
if (SCM_INUMP (z)) {
return SCM_INUM0;
} else if (SCM_BIGP (z)) {
return SCM_INUM0;
} else if (SCM_REALP (z)) {
return scm_flo0;
} else if (SCM_COMPLEXP (z)) {
return scm_make_real (SCM_COMPLEX_IMAG (z));
} else {
SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part);
}
}
SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
/* "Return the magnitude of the number @var{z}. This is the same as\n"
* "@code{abs} for real arguments, but also allows complex numbers."
*/
SCM
scm_magnitude (SCM z)
{
if (SCM_INUMP (z)) {
long int zz = SCM_INUM (z);
if (zz >= 0) {
return z;
} else if (SCM_POSFIXABLE (-zz)) {
return SCM_MAKINUM (-zz);
} else {
#ifdef SCM_BIGDIG
return scm_i_long2big (-zz);
#else
scm_num_overflow (s_magnitude);
#endif
}
} else if (SCM_BIGP (z)) {
if (!SCM_BIGSIGN (z)) {
return z;
} else {
return scm_i_copybig (z, 0);
}
} else if (SCM_REALP (z)) {
return scm_make_real (fabs (SCM_REAL_VALUE (z)));
} else if (SCM_COMPLEXP (z)) {
double r = SCM_COMPLEX_REAL (z);
double i = SCM_COMPLEX_IMAG (z);
return scm_make_real (sqrt (i * i + r * r));
} else {
SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
}
}
SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
/* "Return the angle of the complex number @var{z}."
*/
SCM
scm_angle (SCM z)
{
if (SCM_INUMP (z)) {
if (SCM_INUM (z) >= 0) {
return scm_make_real (atan2 (0.0, 1.0));
} else {
return scm_make_real (atan2 (0.0, -1.0));
}
} else if (SCM_BIGP (z)) {
if (SCM_BIGSIGN (z)) {
return scm_make_real (atan2 (0.0, -1.0));
} else {
return scm_make_real (atan2 (0.0, 1.0));
}
} else if (SCM_REALP (z)) {
return scm_make_real (atan2 (0.0, SCM_REAL_VALUE (z)));
} else if (SCM_COMPLEXP (z)) {
return scm_make_real (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
} else {
SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle);
}
}
SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
(SCM z),
"Return an exact number that is numerically closest to @var{z}.")
#define FUNC_NAME s_scm_inexact_to_exact
{
if (SCM_INUMP (z)) {
return z;
} else if (SCM_BIGP (z)) {
return z;
} else if (SCM_REALP (z)) {
double u = floor (SCM_REAL_VALUE (z) + 0.5);
long lu = (long) u;
if (SCM_FIXABLE (lu)) {
return SCM_MAKINUM (lu);
#ifdef SCM_BIGDIG
} else if (isfinite (u)) {
return scm_i_dbl2big (u);
#endif
} else {
scm_num_overflow (s_scm_inexact_to_exact);
}
} else {
SCM_WRONG_TYPE_ARG (1, z);
}
}
#undef FUNC_NAME
#ifdef SCM_BIGDIG
/* d must be integer */
SCM
scm_i_dbl2big (double d)
{
size_t i = 0;
long c;
SCM_BIGDIG *digits;
SCM ans;
double u = (d < 0) ? -d : d;
while (0 != floor (u))
{
u /= SCM_BIGRAD;
i++;
}
ans = scm_i_mkbig (i, d < 0);
digits = SCM_BDIGITS (ans);
while (i--)
{
u *= SCM_BIGRAD;
c = floor (u);
u -= c;
digits[i] = c;
}
#ifndef SCM_RECKLESS
if (u != 0)
scm_num_overflow ("dbl2big");
#endif
return ans;
}
double
scm_i_big2dbl (SCM b)
{
double ans = 0.0;
size_t i = SCM_NUMDIGS (b);
SCM_BIGDIG *digits = SCM_BDIGITS (b);
while (i--)
ans = digits[i] + SCM_BIGRAD * ans;
if (SCM_BIGSIGN (b))
return - ans;
return ans;
}
#endif
#ifdef HAVE_LONG_LONGS
# ifndef LLONG_MAX
# define ULLONG_MAX ((unsigned long long) (-1))
# define LLONG_MAX ((long long) (ULLONG_MAX >> 1))
# define LLONG_MIN (~LLONG_MAX)
# endif
#endif
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) (-1))
#endif
#ifndef PTRDIFF_MIN
/* the below is not really guaranteed to work (I think), but probably does: */
#define PTRDIFF_MIN ((ptrdiff_t) ((ptrdiff_t)1 << (sizeof (ptrdiff_t)*8 - 1)))
#endif
#ifndef PTRDIFF_MAX
#define PTRDIFF_MAX (~ PTRDIFF_MIN)
#endif
#define NUM2INTEGRAL scm_num2short
#define INTEGRAL2NUM scm_short2num
#define INTEGRAL2BIG scm_i_short2big
#define ITYPE short
#define MIN_VALUE SHRT_MIN
#define MAX_VALUE SHRT_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2ushort
#define INTEGRAL2NUM scm_ushort2num
#define INTEGRAL2BIG scm_i_ushort2big
#define UNSIGNED
#define ITYPE unsigned short
#define MAX_VALUE USHRT_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2int
#define INTEGRAL2NUM scm_int2num
#define INTEGRAL2BIG scm_i_int2big
#define ITYPE int
#define MIN_VALUE INT_MIN
#define MAX_VALUE INT_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2uint
#define INTEGRAL2NUM scm_uint2num
#define INTEGRAL2BIG scm_i_uint2big
#define UNSIGNED
#define ITYPE unsigned int
#define MAX_VALUE UINT_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2long
#define INTEGRAL2NUM scm_long2num
#define INTEGRAL2BIG scm_i_long2big
#define ITYPE long
#define MIN_VALUE LONG_MIN
#define MAX_VALUE LONG_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2ulong
#define INTEGRAL2NUM scm_ulong2num
#define INTEGRAL2BIG scm_i_ulong2big
#define UNSIGNED
#define ITYPE unsigned long
#define MAX_VALUE ULONG_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2ptrdiff
#define INTEGRAL2NUM scm_ptrdiff2num
#define INTEGRAL2BIG scm_i_ptrdiff2big
#define ITYPE ptrdiff_t
#define MIN_VALUE PTRDIFF_MIN
#define MAX_VALUE PTRDIFF_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2size
#define INTEGRAL2NUM scm_size2num
#define INTEGRAL2BIG scm_i_size2big
#define UNSIGNED
#define ITYPE size_t
#define MAX_VALUE SIZE_MAX
#include "libguile/num2integral.i.c"
#ifdef HAVE_LONG_LONGS
#ifndef ULONG_LONG_MAX
#define ULONG_LONG_MAX (~0ULL)
#endif
#define NUM2INTEGRAL scm_num2long_long
#define INTEGRAL2NUM scm_long_long2num
#define INTEGRAL2BIG scm_i_long_long2big
#define ITYPE long long
#define MIN_VALUE LLONG_MIN
#define MAX_VALUE LLONG_MAX
#include "libguile/num2integral.i.c"
#define NUM2INTEGRAL scm_num2ulong_long
#define INTEGRAL2NUM scm_ulong_long2num
#define INTEGRAL2BIG scm_i_ulong_long2big
#define UNSIGNED
#define ITYPE unsigned long long
#define MAX_VALUE ULLONG_MAX
#include "libguile/num2integral.i.c"
#endif /* HAVE_LONG_LONGS */
#define NUM2FLOAT scm_num2float
#define FLOAT2NUM scm_float2num
#define FTYPE float
#include "libguile/num2float.i.c"
#define NUM2FLOAT scm_num2double
#define FLOAT2NUM scm_double2num
#define FTYPE double
#include "libguile/num2float.i.c"
#ifdef GUILE_DEBUG
#define CHECK(type, v) \
do { \
if ((v) != scm_num2##type (scm_##type##2num (v), 1, "check_sanity")) \
abort (); \
} while (0);
static void
check_sanity ()
{
CHECK (short, 0);
CHECK (ushort, 0U);
CHECK (int, 0);
CHECK (uint, 0U);
CHECK (long, 0L);
CHECK (ulong, 0UL);
CHECK (size, 0);
CHECK (ptrdiff, 0);
CHECK (short, -1);
CHECK (int, -1);
CHECK (long, -1L);
CHECK (ptrdiff, -1);
CHECK (short, SHRT_MAX);
CHECK (short, SHRT_MIN);
CHECK (ushort, USHRT_MAX);
CHECK (int, INT_MAX);
CHECK (int, INT_MIN);
CHECK (uint, UINT_MAX);
CHECK (long, LONG_MAX);
CHECK (long, LONG_MIN);
CHECK (ulong, ULONG_MAX);
CHECK (size, SIZE_MAX);
CHECK (ptrdiff, PTRDIFF_MAX);
CHECK (ptrdiff, PTRDIFF_MIN);
#ifdef HAVE_LONG_LONGS
CHECK (long_long, 0LL);
CHECK (ulong_long, 0ULL);
CHECK (long_long, -1LL);
CHECK (long_long, LLONG_MAX);
CHECK (long_long, LLONG_MIN);
CHECK (ulong_long, ULLONG_MAX);
#endif
}
#undef CHECK
#define CHECK \
scm_internal_catch (SCM_BOOL_T, check_body, &data, check_handler, &data); \
if (!SCM_FALSEP (data)) abort();
static SCM
check_body (void *data)
{
SCM num = *(SCM *) data;
scm_num2ulong (num, 1, NULL);
return SCM_UNSPECIFIED;
}
static SCM
check_handler (void *data, SCM tag, SCM throw_args)
{
SCM *num = (SCM *) data;
*num = SCM_BOOL_F;
return SCM_UNSPECIFIED;
}
SCM_DEFINE (scm_sys_check_number_conversions, "%check-number-conversions", 0, 0, 0,
(),
"Number conversion sanity checking.")
#define FUNC_NAME s_scm_sys_check_number_conversions
{
SCM data = SCM_MAKINUM (-1);
CHECK;
data = scm_int2num (INT_MIN);
CHECK;
data = scm_ulong2num (ULONG_MAX);
data = scm_difference (SCM_INUM0, data);
CHECK;
data = scm_ulong2num (ULONG_MAX);
data = scm_sum (SCM_MAKINUM (1), data); data = scm_difference (SCM_INUM0, data);
CHECK;
data = scm_int2num (-10000); data = scm_product (data, data); data = scm_product (data, data);
CHECK;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
#endif
void
scm_init_numbers ()
{
abs_most_negative_fixnum = scm_i_long2big (- SCM_MOST_NEGATIVE_FIXNUM);
scm_permanent_object (abs_most_negative_fixnum);
/* It may be possible to tune the performance of some algorithms by using
* the following constants to avoid the creation of bignums. Please, before
* using these values, remember the two rules of program optimization:
* 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
scm_c_define ("most-positive-fixnum",
SCM_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
scm_c_define ("most-negative-fixnum",
SCM_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
scm_add_feature ("complex");
scm_add_feature ("inexact");
scm_flo0 = scm_make_real (0.0);
#ifdef DBL_DIG
scm_dblprec = (DBL_DIG > 20) ? 20 : DBL_DIG;
#else
{ /* determine floating point precision */
double f = 0.1;
double fsum = 1.0 + f;
while (fsum != 1.0) {
if (++scm_dblprec > 20) {
fsum = 1.0;
} else {
f /= 10.0;
fsum = f + 1.0;
}
}
scm_dblprec = scm_dblprec - 1;
}
#endif /* DBL_DIG */
#ifdef GUILE_DEBUG
check_sanity ();
#endif
#include "libguile/numbers.x"
}
#if (SCM_DEBUG_DEPRECATED == 0)
SCM
scm_mkbig (size_t len, int sign)
{
scm_c_issue_deprecation_warning ("`scm_mkbig' is deprecated. "
"Use `scm_i_mkbig' instead.");
return scm_i_mkbig (len, sign);
}
SCM
scm_big2inum (SCM b, size_t l)
{
scm_c_issue_deprecation_warning ("`scm_big2inum' is deprecated. "
"Use `scm_i_big2num' instead.");
return scm_i_big2inum (b, l);
}
SCM
scm_adjbig (SCM b, size_t nlen)
{
scm_c_issue_deprecation_warning ("`scm_adjbig' is deprecated. "
"Use `scm_i_adjbig' instead.");
return scm_i_adjbig (b, nlen);
}
SCM
scm_normbig (SCM b)
{
scm_c_issue_deprecation_warning ("`scm_normbig' is deprecated. "
"Use `scm_i_normbig' instead.");
return scm_i_normbig (b);
}
SCM
scm_copybig (SCM b, int sign)
{
scm_c_issue_deprecation_warning ("`scm_copybig' is deprecated. "
"Use `scm_i_copybig' instead.");
return scm_i_copybig (b, sign);
}
SCM
scm_2ulong2big (unsigned long *np)
{
unsigned long n;
size_t i;
SCM_BIGDIG *digits;
SCM ans;
ans = scm_i_mkbig (2 * SCM_DIGSPERLONG, 0);
digits = SCM_BDIGITS (ans);
n = np[0];
for (i = 0; i < SCM_DIGSPERLONG; ++i)
{
digits[i] = SCM_BIGLO (n);
n = SCM_BIGDN ((unsigned long) n);
}
n = np[1];
for (i = 0; i < SCM_DIGSPERLONG; ++i)
{
digits[i + SCM_DIGSPERLONG] = SCM_BIGLO (n);
n = SCM_BIGDN ((unsigned long) n);
}
return ans;
}
SCM
scm_dbl2big (double d)
{
scm_c_issue_deprecation_warning ("`scm_dbl2big' is deprecated. "
"Use `scm_double2num' instead,"
"or `scm_i_dbl2big'.");
return scm_i_dbl2big (d);
}
double
scm_big2dbl (SCM b)
{
scm_c_issue_deprecation_warning ("`scm_big2dbl' is deprecated. "
"Use `scm_num2dbl' instead,"
"or `scm_i_big2dbl'.");
return scm_i_big2dbl (b);
}
#endif
/*
Local Variables:
c-file-style: "gnu"
End:
*/
|