Skip to content

Commit

Permalink
clojure.contrib.pprint (cl-format): Fixes to rounding and width issue…
Browse files Browse the repository at this point in the history
…s in ~f and ~$. See #47.
  • Loading branch information
tomfaulhaber committed May 1, 2010
1 parent 661dcfd commit 912e567
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
29 changes: 16 additions & 13 deletions src/main/clojure/clojure/contrib/pprint/cl_format.clj
Expand Up @@ -585,7 +585,8 @@ Note this should only be used for the last one in the sequence"
round-up-result (str leading-zeros round-up-result (str leading-zeros
(String/valueOf (+ result-val (String/valueOf (+ result-val
(if (neg? result-val) -1 1)))) (if (neg? result-val) -1 1))))
expanded (> (count round-up-result) (count result))] expanded (> (count round-up-result) (count result))
_ (prlabel round-str round-up-result e1 expanded)]
[round-up-result e1 expanded]) [round-up-result e1 expanded])
[result e1 false])) [result e1 false]))
[m e false])) [m e false]))
Expand Down Expand Up @@ -624,32 +625,34 @@ Note this should only be used for the last one in the sequence"
(let [w (:w params) (let [w (:w params)
d (:d params) d (:d params)
[arg navigator] (next-arg navigator) [arg navigator] (next-arg navigator)
[mantissa exp] (float-parts arg) [sign abs] (if (neg? arg) ["-" (- arg)] ["+" arg])
[mantissa exp] (float-parts abs)
scaled-exp (+ exp (:k params)) scaled-exp (+ exp (:k params))
add-sign (and (:at params) (not (neg? arg))) add-sign (or (:at params) (neg? arg))
prepend-zero (< -1.0 arg 1.0)
append-zero (and (not d) (<= (dec (count mantissa)) scaled-exp)) append-zero (and (not d) (<= (dec (count mantissa)) scaled-exp))
[rounded-mantissa scaled-exp] (round-str mantissa scaled-exp [rounded-mantissa scaled-exp expanded] (round-str mantissa scaled-exp
d (if w (- w (if add-sign 1 0)))) d (if w (- w (if add-sign 1 0))))
fixed-repr (get-fixed rounded-mantissa scaled-exp d)] _ (prlabel f-f mantissa exp rounded-mantissa scaled-exp)
fixed-repr (get-fixed rounded-mantissa (if expanded (inc scaled-exp) scaled-exp) d)
prepend-zero (= (first fixed-repr) \.)]
(if w (if w
(let [len (count fixed-repr) (let [len (count fixed-repr)
signed-len (if add-sign (inc len) len) signed-len (if add-sign (inc len) len)
prepend-zero (and prepend-zero (not (= signed-len w))) prepend-zero (and prepend-zero (not (>= signed-len w)))
append-zero (and append-zero (not (= signed-len w))) append-zero (and append-zero (not (>= signed-len w)))
full-len (if (or prepend-zero append-zero) full-len (if (or prepend-zero append-zero)
(inc signed-len) (inc signed-len)
signed-len)] signed-len)]
(if (and (> full-len w) (:overflowchar params)) (if (and (> full-len w) (:overflowchar params))
(print (apply str (repeat w (:overflowchar params)))) (print (apply str (repeat w (:overflowchar params))))
(print (str (print (str
(apply str (repeat (- w full-len) (:padchar params))) (apply str (repeat (- w full-len) (:padchar params)))
(if add-sign "+") (if add-sign sign)
(if prepend-zero "0") (if prepend-zero "0")
fixed-repr fixed-repr
(if append-zero "0"))))) (if append-zero "0")))))
(print (str (print (str
(if add-sign "+") (if add-sign sign)
(if prepend-zero "0") (if prepend-zero "0")
fixed-repr fixed-repr
(if append-zero "0")))) (if append-zero "0"))))
Expand Down Expand Up @@ -761,8 +764,8 @@ Note this should only be used for the last one in the sequence"
n (:n params) ; minimum digits before the decimal n (:n params) ; minimum digits before the decimal
w (:w params) ; minimum field width w (:w params) ; minimum field width
add-sign (or (:at params) (neg? arg)) add-sign (or (:at params) (neg? arg))
[rounded-mantissa scaled-exp _] (round-str mantissa exp d nil) [rounded-mantissa scaled-exp expanded] (round-str mantissa exp d nil)
#^String fixed-repr (get-fixed rounded-mantissa scaled-exp d) #^String fixed-repr (get-fixed rounded-mantissa (if expanded (inc scaled-exp) scaled-exp) d)
full-repr (str (apply str (repeat (- n (.indexOf fixed-repr (int \.))) \0)) fixed-repr) full-repr (str (apply str (repeat (- n (.indexOf fixed-repr (int \.))) \0)) fixed-repr)
full-len (+ (count full-repr) (if add-sign 1 0))] full-len (+ (count full-repr) (if add-sign 1 0))]
(print (str (print (str
Expand Down
25 changes: 23 additions & 2 deletions src/test/clojure/clojure/contrib/pprint/test_cl_format.clj
Expand Up @@ -194,10 +194,31 @@
(cl-format nil "~1,1,6$" 0.001) " 0.0" (cl-format nil "~1,1,6$" 0.001) " 0.0"
(cl-format nil "~1,1,6$" 0.0015) " 0.0" (cl-format nil "~1,1,6$" 0.0015) " 0.0"
(cl-format nil "~2,1,6$" 0.005) " 0.01" (cl-format nil "~2,1,6$" 0.005) " 0.01"
(cl-format nil "~2,1,6$" 0.01) " 0.01") (cl-format nil "~2,1,6$" 0.01) " 0.01"
(cl-format nil "~$" 0.099) "0.10"
(cl-format nil "~1$" 0.099) "0.1"
(cl-format nil "~1$" 0.1) "0.1"
(cl-format nil "~1$" 0.99) "1.0"
(cl-format nil "~1$" -0.99) "-1.0")


(simple-tests f-tests (simple-tests f-tests
(cl-format nil "~,1f" -12.0) "-12.0") (cl-format nil "~,1f" -12.0) "-12.0"
(cl-format nil "~,0f" 9.4) "9."
(cl-format nil "~,0f" 9.5) "10."
(cl-format nil "~,0f" -0.99) "-1."
(cl-format nil "~,1f" -0.99) "-1.0"
(cl-format nil "~,2f" -0.99) "-0.99"
(cl-format nil "~,3f" -0.99) "-0.990"
(cl-format nil "~,0f" 0.99) "1."
(cl-format nil "~,1f" 0.99) "1.0"
(cl-format nil "~,2f" 0.99) "0.99"
(cl-format nil "~,3f" 0.99) "0.990"
(cl-format nil "~f" -1) "-1.0"
(cl-format nil "~2f" -1) "-1."
(cl-format nil "~3f" -1) "-1."
(cl-format nil "~4f" -1) "-1.0"
(cl-format nil "~8f" -1) " -1.0"
(cl-format nil "~1,1f" 0.1) ".1")


(simple-tests ampersand-tests (simple-tests ampersand-tests
(cl-format nil "The quick brown ~a jumped over ~d lazy dogs" 'elephant 5) (cl-format nil "The quick brown ~a jumped over ~d lazy dogs" 'elephant 5)
Expand Down

0 comments on commit 912e567

Please sign in to comment.