cl-format rounding bugs
Reported to me by Carlos Ungil (and confirmed by me):
Unfortunately there are still some issues with rounding. I've noticed that when the
mantissa is rounded up from 9 to 10 the exponent is not changed
accordingly. As a result, we get for example
clojure.contrib.pprint> (cl-format nil "~,0F" 9.4) => "9."
clojure.contrib.pprint> (cl-format nil "~,0F" 9.5) => "1.0"
I've compared the output from Clojure and Common Lisp for a handful of
cases. Below we see how "0.10" is returned instead of "1.0" when
printing 0.99 with a single digit after the decimal point. There is
also a minor problem with "01." being printed instead of "1.", but
returning a different representation for a number is of course not as
bad as returning a wrong representation.
;;(map #(cl-format nil % 0.99) ["~,0F" "~,1F" "~,2F" "~,3F"])
("01." "0.10" "0.99" "0.990")
;;(loop for fmt in '("~,0F" "~,1F" "~,2F" "~,3F") collect (format nil
fmt 0.99))
("1." "1.0" "0.99" "0.990")
There are also issues with negative numbers, the add-sign variable has
the same bug in fixed-float that it had in dollar-float but fixing
that is not enough to correct the problem.
;; > (map #(cl-format nil % -0.99) ["~,0F" "~,1F" "~,2F" "~,3F"])
("010." "0-1.0" "0-0.99" "0-0.990")
Unfortunately there are still some issues with rounding. I've noticed that when the
mantissa is rounded up from 9 to 10 the exponent is not changed
accordingly. As a result, we get for example
clojure.contrib.pprint> (cl-format nil "~,0F" 9.4) => "9."
clojure.contrib.pprint> (cl-format nil "~,0F" 9.5) => "1.0"
I've compared the output from Clojure and Common Lisp for a handful of
cases. Below we see how "0.10" is returned instead of "1.0" when
printing 0.99 with a single digit after the decimal point. There is
also a minor problem with "01." being printed instead of "1.", but
returning a different representation for a number is of course not as
bad as returning a wrong representation.
;;(map #(cl-format nil % 0.99) ["~,0F" "~,1F" "~,2F" "~,3F"])
("01." "0.10" "0.99" "0.990")
;;(loop for fmt in '("~,0F" "~,1F" "~,2F" "~,3F") collect (format nil
fmt 0.99))
("1." "1.0" "0.99" "0.990")
There are also issues with negative numbers, the add-sign variable has
the same bug in fixed-float that it had in dollar-float but fixing
that is not enough to correct the problem.
;; > (map #(cl-format nil % -0.99) ["~,0F" "~,1F" "~,2F" "~,3F"])
("010." "0-1.0" "0-0.99" "0-0.990")
Leave a comment
on 2010-05-01 01:59 *
By tomfaulhaber
(In revision:912e5671cc8237addf55753ff524bf283830e684) clojure.contrib.pprint (cl-format): Fixes to rounding and width issues in ~f and ~$. See #47.
Branch: master
Branch: master
on 2010-05-01 02:03 *
By tomfaulhaber
(In revision:5a703b33391e9486548af9ae7d8c3601d6f44505) Remove debug prints :-(. See #47
Branch: master
Branch: master
on 2010-05-01 02:04 *
By tomfaulhaber
Status changed from Accepted to Fixed
Work remaining changed from 2.0 to 0.0