FORMAT-AUX-EXP: adjust scale if scale-exponent return 1.0
authorDavid Vázquez <davazp@es.gnu.org>
Mon, 28 Jun 2010 16:53:30 +0000 (18:53 +0200)
committerNikodemus Siivola <nikodemus@random-state.net>
Sat, 11 Jun 2011 18:43:52 +0000 (21:43 +0300)
 lp#308961, part 2.

 FORMAT-EXP-AUX scales a number such that it will have K digits before of the
 decimal point. This relies on scale-exponent.

  (format nil "~E" 0.1) ; => "1.e-1"

  0: (FORMAT-EXP-AUX #<SB-IMPL::STRING-OUTPUT-STREAM {AFE5DE9}> 0.1 NIL NIL
                     NIL 1 NIL #\ NIL NIL)
    1: (SB-IMPL::SCALE-EXPONENT 0.1)
    1: SB-IMPL::SCALE-EXPONENT returned 0.1 0
  0: FORMAT-EXP-AUX returned "1"

 0.1 is 0.1 * 10^0 according to scale-exponent. Then, it is multiplied by 10^K
 before printing. Everything works out fine.

 However!

  (format nil "~E" 0.01) ; => "10.e-3" ... oops

  0: (FORMAT-EXP-AUX #<SB-IMPL::STRING-OUTPUT-STREAM {AA744C1}> 0.01 NIL NIL
                     NIL 1 NIL #\ NIL NIL)
    1: (SB-IMPL::SCALE-EXPONENT 0.01)
    1: SB-IMPL::SCALE-EXPONENT returned 1.0 -2
  0: FORMAT-EXP-AUX returned "2"

 In this example, scale-exponent returns 1.0 and -2, meaning 0.01 = 1.0 *
 10^-2. Again, format-aux-exp multiply it by 10^K. But as 1.0 has a digit
 before of the decimal point, the result will have K+1 digits.

 This is due to format-exp-aux assumed the returned value will be lesser to
 1.0. In order to fix this, we decrement K when scale-exponent return 1.0 as
 primary value.

src/code/target-format.lisp

index bbf357d..6419712 100644 (file)
           (float-nan-p number))
       (prin1 number stream)
       (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
-        (let* ((expt (- expt k))
+        (let* ((k (if (= num 1.0) (1- k) k))
+               (expt (- expt k))
                (estr (decimal-string (abs expt)))
                (elen (if e (max (length estr) e) (length estr)))
                spaceleft)