home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / dragon4.scm < prev    next >
Text File  |  1999-01-02  |  12KB  |  350 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: dragon4.scm,v 1.14 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1989-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; Floating Point Number Unparser
  23. ;;; package: (runtime number)
  24.  
  25. #|
  26.  
  27. The Dragon4 algorithm is described in "How to print floating point
  28. numbers accurately" by Guy L Steele Jr and Jon L White in ACM SIGPLAN
  29. Conference on Programming Language Design and Implementation 1990
  30. (PLDI '90).
  31.  
  32. Burger & Dybvig ("Printing Floating-Point Numbers Quickly and
  33. Accurately" by Robert G Burger and R Kent Dybvig, PLDI '96) describe a
  34. variant of the Dragon4 algorithm that addresses some of the efficiency
  35. issues.  It is much faster for very large or very small numbers, but
  36. not much different to numbers within a few orders of magnitude of 1.
  37.  
  38. |#
  39.  
  40. (declare (usual-integrations))
  41.  
  42. (define (flo:->string x radix)
  43.   (let ((inf?
  44.      (lambda (x)
  45.        (and (flo:> x 1.)
  46.         (flo:= x (flo:/ x 2.)))))
  47.     (x>0
  48.      (lambda (x)
  49.        (let ((p flo:significand-digits-base-2))
  50.          (call-with-values (lambda () (dragon4-normalize x p))
  51.            (lambda (f e)
  52.          (call-with-values flonum-unparser-cutoff-args
  53.            (lambda (cutoff-mode cutoff display-procedure)
  54.              (dragon4 f e p radix cutoff-mode cutoff
  55.                (lambda (u k generate)
  56.              (let ((digits
  57.                 (list->string
  58.                  (let loop ((u u) (k k) (generate generate))
  59.                    k    ;ignore
  60.                    (if (negative? u)
  61.                        '()
  62.                        (cons (digit->char u radix)
  63.                          (generate loop)))))))
  64.                (display-procedure digits k radix))))))))))))
  65.     (or (and flonum-unparser-hook
  66.          (flonum-unparser-hook x radix))
  67.     (cond ((flo:positive? x)
  68.            (if (inf? x)
  69.            (string-copy "#[+inf]")
  70.            (x>0 x)))
  71.           ((flo:negative? x)
  72.            (let ((x (flo:negate x)))
  73.          (if (inf? x)
  74.              (string-copy "#[-inf]")
  75.              (string-append "-" (x>0 x)))))
  76.           ((flo:zero? x)
  77.            (string-copy "0."))
  78.           (else
  79.            (string-copy "#[NaN]"))))))
  80.  
  81. (define (flonum-unparser:normal-output digits k radix)
  82.   (let ((k+1 (+ k 1)))
  83.     (let ((k+1-l (- k+1 (string-length digits)))
  84.       (n (flo:significand-digits radix)))
  85.       (cond ((zero? (string-length digits))
  86.          (string-copy "0."))
  87.         ((< k+1-l (- n))
  88.          (scientific-output digits k radix 0))
  89.         ((negative? k)
  90.          (string-append "." (make-string (- k+1) #\0) digits))
  91.         ((negative? k+1-l)
  92.          (string-append (string-head digits k+1)
  93.                 "."
  94.                 (string-tail digits k+1)))
  95.         ((<= k n)
  96.          (string-append digits (make-string k+1-l #\0) "."))
  97.         (else
  98.          (scientific-output digits k radix 0))))))
  99.  
  100. (define (flonum-unparser:scientific-output digits k radix)
  101.   (scientific-output digits k radix 0))
  102.  
  103. (define (flonum-unparser:engineering-output digits k radix)
  104.   (scientific-output digits k radix (modulo k 3)))
  105.  
  106. (define (scientific-output digits k radix kr)
  107.   (let ((l (string-length digits))
  108.     (i (+ kr 1))
  109.     (exponent (int:->string (- k kr) radix)))
  110.     (cond ((= l 0)
  111.        (string-append "0e" exponent))
  112.       ((< l i)
  113.        (string-append digits (make-string (- i l) #\0) "e" exponent))
  114.       ((= l i)
  115.        (string-append digits "e" exponent))
  116.       (else
  117.        (string-append (string-head digits i)
  118.               "."
  119.               (string-tail digits i)
  120.               "e"
  121.               exponent)))))
  122.  
  123. (define (flonum-unparser-cutoff-args)
  124.   (cond ((eq? 'NORMAL flonum-unparser-cutoff)
  125.      (values 'NORMAL 0 flonum-unparser:normal-output))
  126.     ((and (pair? flonum-unparser-cutoff)
  127.           (pair? (cdr flonum-unparser-cutoff))
  128.           (let ((mode (car flonum-unparser-cutoff))
  129.             (place (cadr flonum-unparser-cutoff)))
  130.         (and (memq mode '(ABSOLUTE RELATIVE NORMAL))
  131.              (exact-integer? place)
  132.              (or (not (eq? 'RELATIVE mode))
  133.              (positive? place))))
  134.           (or (null? (cddr flonum-unparser-cutoff))
  135.           (and (pair? (cddr flonum-unparser-cutoff))
  136.                (null? (cdddr flonum-unparser-cutoff))
  137.                (let ((mode (caddr flonum-unparser-cutoff)))
  138.              (or (memq mode '(NORMAL SCIENTIFIC ENGINEERING))
  139.                  (and (procedure? mode)
  140.                   (procedure-arity-valid? mode 3)))))))
  141.      (values (car flonum-unparser-cutoff)
  142.          (- (cadr flonum-unparser-cutoff))
  143.          (if (null? (cddr flonum-unparser-cutoff))
  144.              flonum-unparser:normal-output
  145.              (lookup-symbolic-display-mode
  146.               (caddr flonum-unparser-cutoff)))))
  147.     (else
  148.      (warn "illegal flonum unparser cutoff parameter"
  149.            flonum-unparser-cutoff)
  150.      (values 'NORMAL 0 flonum-unparser:normal-output))))
  151.  
  152. (define (lookup-symbolic-display-mode mode)
  153.   (case mode
  154.     ((ENGINEERING) flonum-unparser:engineering-output)
  155.     ((SCIENTIFIC) flonum-unparser:scientific-output)
  156.     ((NORMAL) flonum-unparser:normal-output)
  157.     (else mode)))
  158.  
  159. (define flonum-unparser-hook #f)
  160. (define flonum-unparser-cutoff 'NORMAL)
  161.  
  162. (define (dragon4-normalize x precision)
  163.   (call-with-values (lambda () (flo:normalize x))
  164.     (lambda (f e-p)
  165.       (values (flo:->integer (flo:denormalize f precision))
  166.           (- e-p precision)))))
  167.  
  168. (define (dragon4 f e p radix cutoff-mode cutoff format)
  169.   (call-with-values
  170.       (lambda ()
  171.     (cond ((positive? e)
  172.            (let ((shift (int:expt 2 e)))
  173.          (dragon4-fixup f e p radix cutoff-mode cutoff
  174.                 (int:* f shift) 1 shift)))
  175.           ((negative? e)
  176.            (dragon4-fixup f e p radix cutoff-mode cutoff
  177.                   f (int:expt 2 (- e)) 1))
  178.           (else
  179.            (dragon4-fixup f e p radix cutoff-mode cutoff f 1 1))))
  180.     (lambda (k r s m- m+ cutoff round-up?)
  181.       (if (<= k cutoff)
  182.       ((dragon4-fill (- k 1)) format)
  183.       (let ((2s (int:* 2 s)))
  184.         (let loop ((r r) (m- m-) (m+ m+) (k k) (format format))
  185.           (let ((qr (integer-divide (int:* r radix) s)))
  186.         (let ((k (- k 1))
  187.               (u (integer-divide-quotient qr))
  188.               (r (integer-divide-remainder qr))
  189.               (m- (int:* m- radix))
  190.               (m+ (int:* m+ radix)))
  191.           (let ((2r (int:* 2 r)))
  192.             (let ((high?
  193.                (if round-up?
  194.                    (int:>= 2r (int:- 2s m+))
  195.                    (int:> 2r (int:- 2s m+))))
  196.               (round
  197.                (lambda ()
  198.                  (dragon4-done format
  199.                        (if (int:<= 2r s) u (+ u 1))
  200.                        k))))
  201.               (cond ((int:< 2r m-)
  202.                  (if high? (round) (dragon4-done format u k)))
  203.                 (high?
  204.                  (dragon4-done format (+ u 1) k))
  205.                 ((= k cutoff)
  206.                  (round))
  207.                 (else
  208.                  (format u k
  209.                      (lambda (format)
  210.                        (loop r m- m+ k format)))))))))))))))
  211.  
  212. (define (dragon4-done format u k)
  213.   (format u k (dragon4-fill (- k 1))))
  214.  
  215. (define (dragon4-fill k)
  216.   (lambda (format)
  217.     (format -1 k (dragon4-fill (- k 1)))))
  218.  
  219. (define (dragon4-fixup f e p radix cutoff-mode cutoff r s m-)
  220.  
  221.   (define (adjust k r s m- m+)
  222.     (let ((2r (int:* 2 r)))
  223.       (let loop ((k k) (s s) (m- m-) (m+ m+) (round-up? #f))
  224.  
  225.     (define (adjust-for-mode s k)
  226.       (define (cutoff-adjust cutoff)
  227.         (let ((a (- cutoff k)))
  228.           (let ((y (ceiling (* s (expt-radix radix a)))))
  229.         (let ((m- (int:max y m-))
  230.               (m+ (int:max y m+)))
  231.           (let ((round-up? (or (int:= m+ y) round-up?)))
  232.             (if (int:<= (int:* 2 s) (int:+ 2r m+))
  233.             (loop k s m- m+ round-up?)
  234.             (values k r s m- m+ cutoff round-up?)))))))
  235.       (case cutoff-mode
  236.         ((NORMAL)
  237.          (values k r s m- m+
  238.              (- k (flo:significand-digits radix) 2) ; i.e. ignore cutoff
  239.              round-up?))
  240.         ((ABSOLUTE) (cutoff-adjust cutoff))
  241.         ((RELATIVE) (cutoff-adjust (+ k cutoff)))
  242.         (else (error:wrong-type-datum cutoff-mode #f))))
  243.  
  244.     (let ((2r+m+ (int:+ 2r m+)))
  245.       (let loop ((s s) (k k))
  246.         (if (int:<= (int:* 2 s) 2r+m+)
  247.         (loop (int:* s radix) (+ k 1))
  248.         (adjust-for-mode s k)))))))
  249.  
  250.   (define (scale r s m+)
  251.     (let ((est-k
  252.        (ceiling->exact (- (* (+ e p -1) (/ (flo:log 2.) (log radix)))
  253.                   1e-9))))    ; fudge factor ensures K never too big
  254.       (if (< est-k 0)
  255.       (let ((factor (expt-radix radix (- est-k))))
  256.         (let loop ((k est-k)
  257.                (r (int:* r factor))
  258.                (m- (int:* m- factor))
  259.                (m+ (int:* m+ factor)))
  260.           (if (int:< (int:* r radix) s)
  261.           (loop (- k 1)
  262.             (int:* r radix)
  263.             (int:* m- radix)
  264.             (int:* m+ radix))
  265.           (adjust k r s m- m+))))
  266.       (adjust est-k r (int:* s (expt-radix radix est-k)) m- m+))))
  267.  
  268.   (if (int:= f (int:expt 2 (- p 1)))
  269.       (scale (int:* 2 r) (int:* 2 s) (int:* 2 m-))
  270.       (scale r s m-)))
  271.  
  272.  
  273. (define expt-radix
  274.   (let ((v (make-initialized-vector 310 (lambda (i) (expt 10 i)))))
  275.     (lambda (base exponent)
  276.       (if (and (= base 10)
  277.            (>= exponent 0)
  278.            (< exponent (vector-length v)))
  279.       (vector-ref v exponent)
  280.       (rat:expt base exponent)))))
  281.  
  282. #|  Test code.  Re-run after changing anything.
  283.  
  284. (define (test)
  285.   (define (try n settings . expecteds)
  286.     (let ((got (fluid-let ((flonum-unparser-cutoff settings))
  287.          (number->string (exact->inexact n)))))
  288.       (if (member got expecteds)
  289.       (set! successes (+ successes 1))
  290.       (begin
  291.         (set! failures (+ failures 1))
  292.         (display "\nTest failed ") (write n) (display " ") (write settings)
  293.         (display "\n  expected:")
  294.         (for-each (lambda (s) (display " ") (write s))
  295.           expecteds)
  296.         (display "\n       got: ")  (write got)))))
  297.  
  298.   (define failures 0)
  299.   (define successes 0)
  300.  
  301.   ;; From the MIT Scheme Reference Manual:
  302.   (try (* 4 (atan 1 1))     '(relative 5)              "3.1416")
  303.   (try (* 4000 (atan 1 1))  '(relative 5)              "3141.6")
  304.   (try (* 4000 (atan 1 1))  '(relative 5 scientific)   "3.1416e3")
  305.   (try (* 40000 (atan 1 1)) '(relative 5 scientific)   "3.1416e4")
  306.   (try (* 40000 (atan 1 1)) '(relative 5 engineering)  "31.416e3")
  307.   (try (* 4 (atan 1 1))     '(absolute 5)              "3.14159")
  308.   (try (* 4000 (atan 1 1))  '(absolute 5)              "3141.59265")
  309.   (try (* 4e10 (atan 1 1))  '(absolute -4)             "31415930000.")
  310.   (try (* 4e10 (atan 1 1))  '(absolute -4 scientific)  "3.141593e10")
  311.   (try (* 4e10 (atan 1 1))  '(absolute -4 engineering) "31.41593e9")
  312.   (try (* 4e10 (atan 1 1))  '(absolute -5)             "31415900000.")
  313.  
  314.   ;; Harder tests:
  315.   (try 0.          'normal  "0.")
  316.   (try 0.0123456   'normal  ".0123456")
  317.   (try 0.000123456 'normal  ".000123456")
  318.  
  319.   (try 1/3       '(relative 4) ".3333")
  320.   (try 2/3       '(relative 4) ".6667")
  321.  
  322.   (try 12345.67   '(absolute  1 normal) "12345.7")
  323.   (try 12345.67   '(absolute -4 normal) "10000.")
  324.   (try 4999.      '(absolute -4 normal) "0.")
  325.   (try 5001.      '(absolute -4 normal) "10000.")
  326.  
  327.   (try 12345.67   '(absolute  1 scientific) "1.23457e4")
  328.   (try 12345.67   '(absolute -4 scientific) "1e4")
  329.   (try 4999.      '(absolute -4 scientific) "0." "0e4" "0e3")
  330.   (try 5001.      '(absolute -4 scientific) "1e4")
  331.  
  332.   (try 12345.67   '(absolute  1 engineering) "12.3457e3")
  333.   (try 12345.67   '(absolute -4 engineering) "10e3")
  334.   (try 4999.      '(absolute -4 engineering) "0." "0e3")
  335.   (try 5001.      '(absolute -4 engineering) "10e3")
  336.   (try 5001.      '(absolute -5 engineering) "0." "0e3")
  337.   (try 5001.      '(absolute -6 engineering) "0." "0e3")
  338.   (try -5001.     '(absolute -6 engineering) "0." "-0e3")
  339.  
  340.   (try 0.00499   '(absolute  2 normal) "0." ".00")  ; "0." would be prefereable
  341.  
  342.   (try 0.00500   '(absolute  2 normal) ".01") ; (rounds up in binary)
  343.   (try 0.00501   '(absolute  2 normal) ".01")
  344.   (try 0.00499   '(absolute -3 normal) "0.")
  345.  
  346.  
  347.   (display "\n\nSuccesses: ") (display successes)  
  348.   (display "    Failures: ") (display failures))
  349. |#
  350.