# Commits

committed 6ea9b8e

• Participants
• Parent commits ab556e3
• Branches default

# Files changed (5)

`+;;;; The code below can be used freely under the following conditions:`
`+;;;; * Due credit is given to the original author.`
`+;;;; * You use it at your own risk. The code is experimental and meant`
`+;;;;   as a case study to show that "lazy reals" can be implemented.`
`+;;;;   In particular, no guarantee is given that the code conforms to`
`+;;;;   the specification given below.`
`+;;;;`
`+;;;; If you use it in publications (including software), please let me`
`+;;;; know.`
`+;;;;`
`+;;;; Note: The main focus here is on correct results (which are, however,`
`+;;;; not guaranteed, see above). It is certainly possible to improve the`
`+;;;; efficiency of the code quite a bit.`
`+;;;;`
`+;;;; Michael Stoll, December 11, 2009`
`+;;;; firstname.lastname@uni-bayreuth.de`
`+`

`+;;;;                        I N T R O D U C T I O N`
`+;;;;                        =======================`
`+`
`+;;;; Computable real numbers x are interpreted as (potentially) infinite`
`+;;;; fractions in base 2 that are specified through a rule for computation`
`+;;;; of an integer a with |(2^k)*x - a| <= 1 for any k>=0.`
`+`
`+;;;; The internal data structure should not be accessed.`
`+;;;; The interface for the outside world is as follows:`
`+`
`+;;;; The type CREAL is a supertype of the type RATIONAL. (CREAL-P x), for`
`+;;;; an object x, returns T if x is of type CREAL, otherwise NIL.`
`+;;;;`
`+;;;; (APPROX-R x k), for a CREAL x and an integer k>=0, returns an integer`
`+;;;; a with |(2^k)*x - a| < 1.`
`+;;;;`
`+;;;; (MAKE-REAL fun) returns the real number given by fun. Here fun is a`
`+;;;; function taking an argument k, that computes a as above.`
`+`
`+;;;; CREALs are output by print etc. as a decimal fraction. The error hereby`
`+;;;; is at most one unit in the last digit that was output. The number of`
`+;;;; decimal digits after the decimal point is defined through the dynamic`
`+;;;; variable *PRINT-PREC*.`
`+;;;;`
`+;;;; For comparison operations etc. a precision threshold is used. It is`
`+;;;; defined through the dynamic variable *CREAL-TOLERANCE*. Its value should`
`+;;;; be a nonnegative integer n, meaning that numbers are considered equal`
`+;;;; if they differ by at most 2^(-n).`
`+`
`+`
`+;;;;      E X P O R T E D   F U N C T I O N S   A N D   C O N S T A N T S`
`+;;;;      ===============================================================`
`+`
`+;;;; The following functions, constants and variables are exported. (The`
`+;;;; package is named "REALS".)`
`+;;;; CREAL                  type        type of the computable real numbers`
`+;;;; CREAL-P object         function    tests for type CREAL`
`+;;;; *PRINT-PREC*           variable    specifies precision of output`
`+;;;; *CREAL-TOLERANCE*      variable    precision threshold for comparison`
`+;;;; APPROX-R x:creal k:int>=0`
`+;;;;                        function    returns approximation of x to k digits`
`+;;;; MAKE-REAL function     function    creates object of type CREAL`
`+;;;; RAW-APPROX-R x:creal   function    returns 3 values a,n,s with:`
`+;;;;                                    if a = 0: |x| <= 2^(-n), s = 0`
`+;;;;                                        and n >= *CREAL-TOLERANCE*`
`+;;;;                                    else: a0 integer > 4, n0 integer >=0,`
`+;;;;                                        s = +1 or -1, and sign(x) = s,`
`+;;;;                                        (a-1)*2^(-n) <= |x| <= (a+1)*2^(-n)`
`+;;;; PRINT-R x:creal k:int>=0 &optional (flag t)`
`+;;;;                        function    outputs x with k decimal digits.`
`+;;;;                                    If flag is true, first a newline.`
`+;;;; +R {creal}*            function    computes the sum of the arguments`
`+;;;; -R creal {creal}*      function    computes negative or difference`
`+;;;; *R {creal}*            function    computes the product of the arguments`
`+;;;; /R creal {creal}*      function    computes reciprocal or quotient`
`+;;;; SQRT-R creal           function    computes the square root`
`+;;;; LOG2-R                 constant    log(2)`
`+;;;; PI-R                   constant    pi`
`+;;;; 2PI-R                  constant    2*pi`
`+;;;; PI/2-R                 constant    pi/2`
`+;;;; PI/4-R                 constant    pi/4`
`+;;;; LOG-R x:creal &optional b:creal`
`+;;;;                        function    computes the logarithm of n in base b;`
`+;;;;                                    default is the natural logarithm`
`+;;;; EXP-R creal            function    computes the exponential function`
`+;;;; EXPT-R x:creal y:creal function    computes x^y`
`+;;;; SIN-R creal            function    computes the sine`
`+;;;; COS-R creal            function    computes the cosine`
`+;;;; TAN-R creal            function    computes the tangent`
`+;;;; ATAN-R x:creal &optional y:creal`
`+;;;;                        function    computes the arctangent of x or`
`+;;;;                                    the phase angle of (x,y)`
`+;;;; ASH-R x:creal n:int    function    computes x * 2^n`
`+;;;; ROUND-R x:creal &optional y:creal`
`+;;;;                        function    computes two values q (integer) and r`
`+;;;;                                    (creal) with x = q*y + r and |r|<=|y|/2`
`+;;;;                                    according to the precision specified by`
`+;;;;                                    *CREAL-TOLERANCE*`
`+;;;; FLOOR-R x:creal &optional y:creal`
`+;;;;                        function    like ROUND-R, corresponding to floor`
`+;;;; CEILING-R x:creal &optional y:creal`
`+;;;;                        function    like ROUND-R, corresponding to ceiling`
`+;;;; TRUNCATE-R x:creal &optional y:creal`
`+;;;;                        function    like ROUND-R, corresponding to truncate`
`+`
`+`
`+;;;; In the code, exported symbols at their location of definition are`
`+;;;; emphasized in UPPERCASE.`
`+`
`+;;;; ==========================================================================`
`+`

# File computable-reals.asd

`+(defpackage #:computable-reals-asd`
`+  (:use #:cl #:asdf))`
`+`
`+(in-package #:computable-reals-asd)`
`+`
`+(defsystem computable-reals`
`+  :name "computable-reals"`
`+  :version "1.0"`
`+  :author "Michael Stoll"`
`+  :maintainer "Robert Smith"`
`+  :description "Computable real numbers."`
`+  :long-description "Arbitrary-precision, re-computing real-numbers."`
`+`
`+  :serial t`
`+  :components ((:file "package")`
`+               (:file "reals")))`

# File package.lisp

`+(defpackage #:computable-reals`
`+  (:use #:common-lisp)`
`+  (:export #:creal`
`+           #:approx-r`
`+           #:make-real`
`+           #:creal-p`
`+           #:print-r`
`+           #:+r`
`+           #:-r`
`+           #:*r`
`+           #:/r`
`+           #:sqrt-r`
`+           #:log2-r`
`+           #:log-r`
`+           #:exp-r`
`+           #:pi-r`
`+           #:2pi-r`
`+           #:sin-r`
`+           #:cos-r`
`+           #:*print-prec*`
`+           #:round-r`
`+           #:*creal-tolerance*`
`+           #:ash-r`
`+           #:raw-approx-r`
`+           #:floor-r`
`+           #:ceiling-r`
`+           #:truncate-r`
`+           #:pi/2-r`
`+           #:pi/4-r`
`+           #:atan-r`
`+           #:expt-r`
`+           #:tan-r))`

# File reals.lisp

`-;;;; The code below can be used freely under the following conditions:`
`-;;;; * Due credit is given to the original author.`
`-;;;; * You use it at your own risk. The code is experimental and meant`
`-;;;;   as a case study to show that "lazy reals" can be implemented.`
`-;;;;   In particular, no guarantee is given that the code conforms to`
`-;;;;   the specification given below.`
`-;;;;`
`-;;;; If you use it in publications (including software), please let me`
`-;;;; know.`
`-;;;;`
`-;;;; Note: The main focus here is on correct results (which are, however,`
`-;;;; not guaranteed, see above). It is certainly possible to improve the`
`-;;;; efficiency of the code quite a bit.`
`-;;;;`
`-;;;; Michael Stoll, December 11, 2009`
`-;;;; firstname.lastname@uni-bayreuth.de`
`-`
`-;;;; ======================================================================`
`-`
` ;;;; Computable real numbers`
` ;;;; Michael Stoll`
` ;;;; 1989-06-11, 1989-06-12, 1989-06-13, 1989-06-14, 1989-06-17, 1989-06-30`
` `
` `
`-;;;;                        I N T R O D U C T I O N`
`-;;;;                        =======================`
`-`
`-;;;; Computable real numbers x are interpreted as (potentially) infinite`
`-;;;; fractions in base 2 that are specified through a rule for computation`
`-;;;; of an integer a with |(2^k)*x - a| <= 1 for any k>=0.`
`-`
`-;;;; The internal data structure should not be accessed.`
`-;;;; The interface for the outside world is as follows:`
`-`
`-;;;; The type CREAL is a supertype of the type RATIONAL. (CREAL-P x), for`
`-;;;; an object x, returns T if x is of type CREAL, otherwise NIL.`
`-;;;;`
`-;;;; (APPROX-R x k), for a CREAL x and an integer k>=0, returns an integer`
`-;;;; a with |(2^k)*x - a| < 1.`
`-;;;;`
`-;;;; (MAKE-REAL fun) returns the real number given by fun. Here fun is a`
`-;;;; function taking an argument k, that computes a as above.`
`-`
`-;;;; CREALs are output by print etc. as a decimal fraction. The error hereby`
`-;;;; is at most one unit in the last digit that was output. The number of`
`-;;;; decimal digits after the decimal point is defined through the dynamic`
`-;;;; variable *PRINT-PREC*.`
`-;;;;`
`-;;;; For comparison operations etc. a precision threshold is used. It is`
`-;;;; defined through the dynamic variable *CREAL-TOLERANCE*. Its value should`
`-;;;; be a nonnegative integer n, meaning that numbers are considered equal`
`-;;;; if they differ by at most 2^(-n).`
`-`
`-`
`-;;;;      E X P O R T E D   F U N C T I O N S   A N D   C O N S T A N T S`
`-;;;;      ===============================================================`
`-`
`-;;;; The following functions, constants and variables are exported. (The`
`-;;;; package is named "REALS".)`
`-;;;; CREAL                  type        type of the computable real numbers`
`-;;;; CREAL-P object         function    tests for type CREAL`
`-;;;; *PRINT-PREC*           variable    specifies precision of output`
`-;;;; *CREAL-TOLERANCE*      variable    precision threshold for comparison`
`-;;;; APPROX-R x:creal k:int>=0`
`-;;;;                        function    returns approximation of x to k digits`
`-;;;; MAKE-REAL function     function    creates object of type CREAL`
`-;;;; RAW-APPROX-R x:creal   function    returns 3 values a,n,s with:`
`-;;;;                                    if a = 0: |x| <= 2^(-n), s = 0`
`-;;;;                                        and n >= *CREAL-TOLERANCE*`
`-;;;;                                    else: a0 integer > 4, n0 integer >=0,`
`-;;;;                                        s = +1 or -1, and sign(x) = s,`
`-;;;;                                        (a-1)*2^(-n) <= |x| <= (a+1)*2^(-n)`
`-;;;; PRINT-R x:creal k:int>=0 &optional (flag t)`
`-;;;;                        function    outputs x with k decimal digits.`
`-;;;;                                    If flag is true, first a newline.`
`-;;;; +R {creal}*            function    computes the sum of the arguments`
`-;;;; -R creal {creal}*      function    computes negative or difference`
`-;;;; *R {creal}*            function    computes the product of the arguments`
`-;;;; /R creal {creal}*      function    computes reciprocal or quotient`
`-;;;; SQRT-R creal           function    computes the square root`
`-;;;; LOG2-R                 constant    log(2)`
`-;;;; PI-R                   constant    pi`
`-;;;; 2PI-R                  constant    2*pi`
`-;;;; PI/2-R                 constant    pi/2`
`-;;;; PI/4-R                 constant    pi/4`
`-;;;; LOG-R x:creal &optional b:creal`
`-;;;;                        function    computes the logarithm of n in base b;`
`-;;;;                                    default is the natural logarithm`
`-;;;; EXP-R creal            function    computes the exponential function`
`-;;;; EXPT-R x:creal y:creal function    computes x^y`
`-;;;; SIN-R creal            function    computes the sine`
`-;;;; COS-R creal            function    computes the cosine`
`-;;;; TAN-R creal            function    computes the tangent`
`-;;;; ATAN-R x:creal &optional y:creal`
`-;;;;                        function    computes the arctangent of x or`
`-;;;;                                    the phase angle of (x,y)`
`-;;;; ASH-R x:creal n:int    function    computes x * 2^n`
`-;;;; ROUND-R x:creal &optional y:creal`
`-;;;;                        function    computes two values q (integer) and r`
`-;;;;                                    (creal) with x = q*y + r and |r|<=|y|/2`
`-;;;;                                    according to the precision specified by`
`-;;;;                                    *CREAL-TOLERANCE*`
`-;;;; FLOOR-R x:creal &optional y:creal`
`-;;;;                        function    like ROUND-R, corresponding to floor`
`-;;;; CEILING-R x:creal &optional y:creal`
`-;;;;                        function    like ROUND-R, corresponding to ceiling`
`-;;;; TRUNCATE-R x:creal &optional y:creal`
`-;;;;                        function    like ROUND-R, corresponding to truncate`
`-`
`-`
`-;;;; In the code, exported symbols at their location of definition are`
`-;;;; emphasized in UPPERCASE.`
`-`
`-;;;; ==========================================================================`
`-`
` ;;;;   I N T E R N A L   S T R U C T U R E S   A N D   I N T E R F A C E`
` ;;;;   -----------------------------------------------------------------`
` `
` ;; Package for computable real numbers`
`-(defpackage #:reals`
`-  (:use #:common-lisp))`
`+`
` `
` (in-package #:reals)`
` `
`-(export '(creal approx-r make-real creal-p print-r +r -r *r /r sqrt-r`
`-          log2-r log-r exp-r pi-r 2pi-r sin-r cos-r *print-prec* round-r`
`-          *creal-tolerance* ash-r raw-approx-r floor-r ceiling-r truncate-r`
`-          pi/2-r pi/4-r atan-r expt-r tan-r))`
`+(export ')`
` `
` ;;; Computable reel numbers are rational numbers or structures:`
` `