((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/numbers" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "egg") (toc)) (section 2 "Numbers" (p "This egg provides support for the full numeric tower. It adds support for:") (ul (li "Integers larger than " (tt "most-positive-fixnum") " and smaller than " (tt "most-negative-fixnum") " through bignums, instead of the default behaviour of falling back to inexact flonums.") (li "Rational numbers (fractions), instead of falling back to inexact flonums.") (li "Complex numbers.")) (p "All of these are supported by exporting different versions of the standard numerical procedures. Be sure to import the " (tt "numbers") " module after the " (tt "scheme") " module, or to hide the standard numerical procedures from " (tt "scheme") ". These number types are also supported as read-time literals, but only after loading the extension and only in the interpreter (but see below for limited compile-time support).") (p "This egg also adds a handful of extra procedures. See below.")) (section 2 "Interface" (p "This extension provides support for large exact integers, exact rational and complex numbers.") (p "The following standard procedures are redefined:") (pre "+     -      *     /\n=     >      <     >=    <=\nabs   max    min\neqv?  equal?\nexp   expt   log   sin   cos   tan   atan   asin   acos   sqrt\nquotient     modulo      remainder\nexact?       inexact?\nexact->inexact           inexact->exact\npositive?    negative?   even?       odd?   zero?\nnumber?      complex?    real?       rational?     integer?\ngcd   lcm\ntruncate     ceiling     floor       round\nnumber->string           string->number\nnumerator    denominator\nrationalize\nmagnitude         angle\nreal-part         imag-part") (p "The " (tt "log") " procedure is extended to allow a second argument specifying the logarithm base, as per R7RS.") (p "The following standard procedures are provided:") (pre "make-rectangular  make-polar") (p "The following non-standard extended procedures are redefined:") (pre "add1         sub1\nsignum\nbitwise-and  bitwise-ior  bitwise-xor  bitwise-not  arithmetic-shift\nrandomize    random       finite?      bit-set?") (p "Additionally the following procedures are available:") (def (sig (procedure "(infinite? Z)" (id infinite?))) (p "Is the number " (tt "Z") " " (tt "+inf.0") " or " (tt "-inf.0") ", or a complex number containing either infinity as its real or imaginary part?") (p "From R6RS and R7RS.") (p (b "Note") ": This is " (b "not") " the inverse of " (tt "finite?") ", since it produces " (tt "#f") " on " (tt "nan.0") " (or complex numbers containing " (tt "nan.0") "), while " (tt "finite?") " also returns " (tt "#f") " in that case.")) (def (sig (procedure "(nan? Z)" (id nan?))) (p "Is the object " (tt "Z") " a real representing " (tt "nan.0") "?") (p "From R6RS and R7RS.")) (def (sig (procedure "(exact Z)" (id exact)) (procedure "(inexact Z)" (id inexact))) (p "Convert " (tt "Z") " to an exact or an inexact number, respectively.  These are just the R6RS and R7RS names for " (tt "inexact->exact") " and " (tt "exact->inexact") ".")) (def (sig (procedure "(exact-integer-sqrt K)" (id exact-integer-sqrt))) (p "Returns two values " (tt "s") " and " (tt "r") ", where " (tt "s^2 + r = K") " and " (tt "K < (s+1)^2") ". In other words, " (tt "s") " is the closest square root we can find that's equal to or smaller than " (tt "K") ", and " (tt "r") " is the rest if " (tt "K") " isn't a neat square of two numbers.") (p "This procedure is compatible with the R7RS specification.")) (def (sig (procedure "(exact-integer-nth-root K N)" (id exact-integer-nth-root))) (p "Like " (tt "exact-integer-sqrt") ", but with any base value.  Calculates " (tt "\\sqrt[N]{K") "}, the " (tt "N") "th root of " (tt "K") " and returns two values " (tt "s") " and " (tt "r") " where " (tt "s^N + r = K") " and " (tt "K < (s+1)^N") ".")) (def (sig (procedure "(exact-integer? X)" (id exact-integer?))) (p "Is " (tt "X") " an exact integer?")) (def (sig (procedure "(quotient&remainder A B)" (id quotient&remainder))) (p "Return the quotient " (i "and") " the remainder of A divided by B.") (p "This is especially useful for bignums, since both numbers are derived simultaneously.  This saves performing the division algorithm twice.") (p "This name is deprecated in favor of its more \"regular\" alias from R7RS, " (tt "truncate/") ".")) (def (sig (procedure "(quotient&modulo A B)" (id quotient&modulo))) (p "Like " (tt "quotient&remainder") ", except return the modulo instead of remainder.") (p "This procedure has been deprecated.")) (def (sig (procedure "(floor/ A B)" (id floor/)) (procedure "(floor-quotient A B)" (id floor-quotient)) (procedure "(floor-remainder A B)" (id floor-remainder)) (procedure "(truncate/ A B)" (id truncate/)) (procedure "(truncate-quotient A B)" (id truncate-quotient)) (procedure "(truncate-remainder A B)" (id truncate-remainder))) (p "These procedures are from R7RS.") (p "The " (tt "floor/") " and " (tt "truncate/") " versions return two values: the quotient and remainder obtained after dividing " (tt "A") " by " (tt "B") ".  The " (tt "-quotient") " and " (tt "-remainder procedures") " simply return only the corresponding value of the \"full\" computation.") (p "The difference is in how the values are derived for negative values.") (p "If both values are desired, the two-value versions are recommended, as they perform the computation only once.") (p "All the procedures compute a quotient " (tt "q") " and remainder " (tt "r") " such that " (tt "A = nq + r") ".  The remainder is determined by the choice for " (tt "q") ", through the relation " (tt "r = A - nq") ".  The " (tt "floor") " procedures compute " (tt "q = floor(A/B)") " and the " (tt "truncate") " procedures compute " (tt "q = truncate(A/B)") ".") (p "See " (link "http://people.csail.mit.edu/riastradh/tmp/division.txt" "Riastradh's proposal for Division operators in Scheme") " for a full explanation of the intricacies of these procedures.")) (def (sig (procedure "(conj Z)" (id conj))) (p "Returns the conjugate of the complex number Z.")) (def (sig (procedure "(bignum? X)" (id bignum?))) (p "Is X an extended-precision integer?")) (def (sig (procedure "(ratnum? X)" (id ratnum?))) (p "Is X a ratio?")) (def (sig (procedure "(cplxnum? X)" (id cplxnum?))) (p "Is X a complex?")) (def (sig (procedure "(rectnum? X)" (id rectnum?))) (p "Is X an exact-complex? (Treats an integer-floatingpoint as \"exact\".)")) (def (sig (procedure "(compnum? X)" (id compnum?))) (p "Is X an inexact-complex?")) (def (sig (procedure "(cflonum? X)" (id cflonum?))) (p "Is X a floatingpoint-complex or a floatingpoint?")) (def (sig (procedure "(cintnum? X)" (id cintnum?))) (p "Is X an integer-complex or an integer?")) (def (sig (procedure "(integer-length X)" (id integer-length))) (p "From " (link "http://srfi.schemers.org/srfi-60" "SRFI-60") "; get the number of bits required to represent the integer " (tt "X") " in 2s complement notation."))) (section 2 "Example" (highlight scheme "(define (fac n)\n  (if (zero? n)\n      1\n      (* n (fac (- n 1))) ) ) \n\n(fac 100)   \n; => 9332621544394415268169923885626670049071596826438162146859296389\n     5217599993229915608941463976156518286253697920827223758251185210\n     916864000000000000000000000000")) (section 2 "Compiled code" (p "Starting with version 2.8, extended number literals can be used in compiled code.  To make this work, compile your code with:") (pre " csc -X numbers-syntax foo.scm") (p (b "IMPORTANT") ": Extended number literals only work when the code will be run on exactly the same platform as the Scheme compiler ran on.  Cross-compilation and compiling to C and compiling that on the target platform is " (i "not supported") ". (You will get an error message when you try to do it anyway)")) (section 2 "Bugs and limitations" (ul (li "Extended number literals can not be used portably in compiled code (use " (tt "string->number") " for maximum portability).") (li "For best results, use Chicken 4.7.4 or later.  Extended numbers syntax isn't reliably supported by the reader of earlier Chicken versions (here as well, use " (tt "string->number") " for maximum portability)."))) (section 2 "About this egg" (section 3 "Author" (p (int-link "/users/felix-winkelmann" "felix")) (p "The code for complex arithmetic was mostly taken from Thomas Chust's " (int-link "/eggref/3/complex" "complex egg for CHICKEN 3") ".")) (section 3 "Maintainer" (p "The CHICKEN Team")) (section 3 "Version history" (dl (dt "4.6.3") (dd "Fix a bug introduced in 4.6.2 related to printing negative floating-point numbers.") (dt "4.6.2") (dd "Fix an edge case in printing floating-point numbers resulting in " (tt "0.0") " for numbers that will be normalized to " (tt "2^64") ".") (dt "4.6.1") (dd "Fix a few warnings during compilation due to invalid " (tt "exclude") " directives in module definition (thanks to Evan Hanson).") (dt "4.6") (dd "Proper fix for Burnikel-Ziegler performance problem from release 4.3, thereby fixing another pathological case.") (dt "4.5") (dd "Fix \"overlap\" handling in number->string for power of two radices which have a bit length that's not a multiple of the word size.") (dt "4.4") (dd "Add support for \"argvector\" CHICKEN, while maintaining support for pre-argvector versions.") (dt "4.3") (dd "Fix pathological performance problem in Burnikel-Ziegler division routines triggered by \"pidigits\" benchmark from the Great Languages Shootout Game (thanks to \"Balkenbrij\" on Reddit). Fix a double " (tt "C_fix()") " call in " (tt "bitwise-and") " that would sometimes cause invalid result values due to 1 limb of uninitialized memory.  Fix a similar bug in comparisons of ratnums with other numbers.  Fix error reporting when passed non-numeric objects to elementary arithmetic operators.  Make bignum to floating-point more stable across platforms by disabling extended precision FP registers (on Linux/i386).") (dt "4.2") (dd "Fix an error in Burnikel-Ziegler division that caused wrong results in some edge cases.  Fix compilation error in pre-4.8.0 CHICKENs due to use of missing C macro.") (dt "4.1") (dd "Fix a few errors in specialization rules and a strange use of \"eval\" in the unit tests.") (dt "4.0") (dd "Convert internal digit representation to use full words. Improve performance by accessing halfdigits directly.  Improve performance of number<->string conversions for radixes that are a power of two by directly operating on digits, avoiding in-place shifts.  Improve performance of number->string by using a divide and conquer algorithm.  Implement Burnikel&Ziegler recursive division which is O(r*s^{log(3)-1} + r*log(s)) instead of O(n^2) (this is mostly useful for very large numbers due to high constant overhead).  Make comparisons inlineable and implement vararg comparison procedures in C to avoid consing up a rest list.  Implement Karatsuba square root, which is more efficient than Newton's algorithm.  Implement " (tt "bit-set?") ".  Rewrite the last remaining s48 code (very little was left) to ensure clean license/copyright.") (dt "3.1") (dd "Improve performance of string<->number conversions by chunking bignum digits off instead of using a binary digit at a time.  Use a faster and shorter division algorithm from Hacker's Delight.  Implement Lehmer's hybrid Euclidian GCD algorithm which is O(n^2/log n) instead of O(n^2) like Euclid and Stein's algorithms. Implement Karatsuba multiplication which is O(n^log2(3)) instead of O(n^2), but is only effective for larger bignums.  Make a few more things inlineable.  Fix stupid mistake which caused exact operations involving rational numbers to be needlessly slow.  Improve performance for string<->number conversion using radix powers of two, and for multiplication of bignums with small powers of two.") (dt "3.0.1") (dd "Fix quotient&remainder and derived procedures for 2 bignum arguments (it returned only the remainder).  Fix crash bug in flonum comparison.") (dt "3.0") (dd "Improve performance of " (tt "exact-integer-sqrt") " and " (tt "exact-integer-nth-root") " by using a better initial guess.  Improve read/write performance.  Major refactoring to make things a lot more consistent with the core system.  Added some more specializations.  Change " (tt "integer-length") ", " (tt "bitwise-and") ", " (tt "bitwise-ior") ", " (tt "bitwise-ior") ", " (tt "bitwise-not") ", " (tt "arithmetic-shift") " and " (tt "integer-length") " to only allow exact integers.  Handling of rationals should be better, as well as a few other division procedures.") (dt "2.10.1") (dd "Fix a few inexact number tests to use nonzero epsilons.") (dt "2.10") (dd "Fix error reporting of " (tt ">=") ", " (tt "<=") ", " (tt "nan?") ", " (tt "finite?") " and " (tt "infinite?") " [thanks to Felix Winkelmann].  Fix types DB entry for " (tt "inexact->exact") " (found by adding insanely paranoid checks to CHICKEN itself)") (dt "2.9") (dd "Fix a few compiler warnings. R7RS support: Add support for a 2nd \"base\" argument to the " (tt "log") " procedure, export " (tt "exact") " and " (tt "inexact") " aliases for " (tt "exact->inexact") " and " (tt "inexact->exact") ".  Add " (tt "floor/") ", " (tt "floor-quotient") ", " (tt "floor-remainder") ", " (tt "truncate/") ", " (tt "truncate-quotient") " and " (tt "truncate-remainder") ".") (dt "2.8.2") (dd "Add CL-compatible support for complex numbers to " (tt "signum") " (" (link "https://bugs.call-cc.org/ticket/953" "#953") "), and support " (tt "numerator") " and " (tt "denominator") " on flonums (as per RnRS) (" (link "https://bugs.call-cc.org/ticket/1016" "#1016") ") [Thanks to John Cowan]") (dt "2.8.1") (dd "Remove long-deprecated and undocumented " (tt "numbers:") "-prefixed variants of " (tt "+") ", " (tt "-") ", etc.  Fix compilation on 4.8.1, which failed due to invalid +inf/-inf literals.  Remove " (tt "(declare (disable-interrupts))") ", which should make numbers play better with threaded code.") (dt "2.8") (dd "Correct handling of exactness prefix (requires recent Chicken to work, at least 4.7.4). Division by inexact zero is no longer considered an error, but returns NaN or +Inf/-Inf.  " (tt "inexact->exact") " raises an error on NaN instead of returning 0 (reported by Felix Winkelmann).  Fix " (tt "log") " so it doesn't erroneously fail on complex numbers.  Generalize " (tt "asin") " and " (tt "acos") " so they can return complex numbers. Fix " (tt "<=") " and " (tt ">=") " so they work when given a " (tt "nan") " argument in rest position. Various fixes for code that expected inexact numbers so it calls " (tt "exact->inexact") " before. Add types database for scrutinizer (Only in Chicken 4.7.4 and later).") (dt "2.7") (dd "Fix several bugs (" (tt "expt") ", " (tt "integer?") ", " (tt "rational?") ", " (tt "<=") ", " (tt ">=") ", " (tt "eqv?") ", " (tt "-") ") found by importing the number tests from Gauche and writing an extensive test for number syntax edge cases. Complete rewrite of number parser (it should fully conform to the R7RS superset of R5RS number syntax now).  Dropped dependency on the regex egg.  Improved precision of " (tt "exact->inexact") " so it can handle more extreme numbers. Provide the " (tt "nan?") ", " (tt "finite?") " and " (tt "infinite?") " predicates and " (tt "integer-length") " procedure.") (dt "2.6.1") (dd "Fix " (tt "string->number") " so it raises an exception instead of crashing when passed a negative, zero or too large base (reported by Peter Hendrickson).  Update test scripts so they exit with nonzero status in case of failed tests (reported by Mario Goulart)") (dt "2.6") (dd "Fix expt and log so they work for numbers which produce complex results.") (dt "2.5") (dd "Fix expt so it doesn't use flonums and loses precision in case of bignums") (dt "2.4") (dd "Added regex to dependency list") (dt "2.3") (dd "Added regex requirement to make it work in Chicken 4.6.2+") (dt "2.2") (dd "Fixed ratnum normalization in case of negative divisors") (dt "2.1") (dd "Changed " (tt "quotient") ", " (tt "remainder") " and " (tt "quotient&remainder") " semantics so they accept fractional flonums to match Chicken's implementation of " (tt "quotient") " and " (tt "remainder") ".  This also affects " (tt "modulo") " and " (tt "quotient&modulo") ".") (dt "2.0") (dd "Removed dependency on GMP, replacing it by the Scheme48 numbers code. Improved performance by integrating with GC.  Added benchmarks and more tests. Converted testsuite to use the " (int-link "/eggref/4/test" "test") " egg instead of " (int-link "/eggref/4/testeez" "testeez") ". [by Peter Bex]") (dt "1.82") (dd "removed dependency on " (int-link "/eggref/4/easyffi" "easyffi") " (Thanks to Peter Bex)") (dt "1.81") (dd "fixed dependencies") (dt "1.809") (dd "Fix for " (tt "(expt <number> <ratio>)") ", would fail [Bug noted by John Cowan]") (dt "1.808") (dd "Fix for 1.0+1i not treated as a rectnum or compnum [Bug noted by John Cowan] Added 'cintnum?' & 'cplxnum?' [kon lovett]") (dt "1.806") (dd "Fix for equal? when given keywords as args [Bug noted by papasi] [elf]") (dt "1.804") (dd "Using more accurate exact->inexact conversion for rationals [Alex Shinn]") (dt "1.803") (dd "fixed bug in fixnum bitwise operations [Thanks to Jeremy Sydik]") (dt "1.802") (dd "not quite complete support for static linking") (dt "1.801") (dd "Fixed a bug in " (tt "angle") " [zb, mario]") (dt "1.8") (dd "Single-argument " (tt "+") " and " (tt "*") " didn't check argument type [Thanks to Stephen Gilardi]") (dt "1.701") (dd "Bugfix for compiler macros, undefined symbol [Kon Lovett]") (dt "1.7") (dd "Added compiler macros for " (tt "bitwise-...") " operations") (dt "1.6") (dd "Compiler macros now work without -X numbers") (dt "1.5") (dd "Added support for speculative inlining of fixnum ops") (dt "1.4") (dd "Fixed bug in ratnum/bignum division [Thanks to Ivan Shmakov]") (dt "1.3") (dd "Fixing round for rational numbers [Alex Shinn]") (dt "1.2") (dd "Added " (tt "bignum? ratnum? cflonum? rectnum? compnum?") " [Kon Lovett]") (dt "1.1") (dd "Uses easyffi properly [reported by John Cowan]") (dt "1.0") (dd "Fix for fixnum/rational comparison [by Zbigniew]") (dt "0.999") (dd (tt "rational?") " always returned false for flonums") (dt "0.998") (dd "Added some missing definitions to numbers.scm [found by Kon Lovett]") (dt "0.997") (dd "Fixed memory leak in some bitwise operations [found by Dan Muresan]") (dt "0.996") (dd "Fixed a bug in " (tt "expt") " [Daishi Kato]") (dt "0.995") (dd (tt "round") " rounds to even (as specified by R5RS) [Thanks to Benedikt Rosenau]") (dt "0.994") (dd "Reimplemented " (tt "=") " in C") (dt "0.993") (dd "Fixed " (tt "equal?") " which didn't handle extended numbers nested in other data") (dt "0.992") (dd "Slight performance tuning for " (tt "expt") " [Again by Daishi]") (dt "0.991") (dd "Fixed bug in fixnum/bignum subtraction [Thanks to Daishi Kato and Alex Shinn]") (dt "0.99") (dd "Speed improvements for " (tt "expt") " by Daishi Kato") (dt "0.98") (dd "Added " (tt "random") " and " (tt "randomize") " [Suggested by Daishi Kato]") (dt "0.97") (dd "bignum/fixnum subtraction used wrong argument order [Thanks to Kon Lovett]") (dt "0.96") (dd "Several bug fixes by Alex Shinn; " (tt "signum") " is exactness preserving") (dt "0.95") (dd "Alex Shinn contributed a working version of " (tt "expt")) (dt "0.94") (dd "Yet another bug-fix by Michal") (dt "0.93") (dd "Several bug-fixes by Michal Janeczek") (dt "0.92") (dd "exactness handling of " (tt "expt") " is slightly better but still not perfect.") (dt "0.91") (dd "Fixed bug in " (tt "integer?")) (dt "0.9") (dd "(beta)"))) (section 3 "License" (pre "Copyright (c) 2008-2017 The CHICKEN Team\nCopyright (c) 2000-2007, Felix L. Winkelmann\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions\nare met:\n1. Redistributions of source code must retain the above copyright\n   notice, this list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright\n   notice, this list of conditions and the following disclaimer in the\n   documentation and/or other materials provided with the distribution.\n3. The name of the authors may not be used to endorse or promote products\n   derived from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR\nIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\nOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\nIN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,\nINCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\nNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\nTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."))))