From 1dceb069ada015096580c08db3723fd6b91688d3 Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Sun, 9 Apr 2017 20:29:11 +0200 Subject: [PATCH] formatting: fix bug when using %e and the number rounds up in magnitude 99/10 0 format-scientific-simple was outputting 1.0e0 because the mantissa was rounded up, so it had one extra character, and the exponent was wrong. --- basis/formatting/formatting-tests.factor | 3 +++ basis/formatting/formatting.factor | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/basis/formatting/formatting-tests.factor b/basis/formatting/formatting-tests.factor index 710347ef3e..957eb20df7 100755 --- a/basis/formatting/formatting-tests.factor +++ b/basis/formatting/formatting-tests.factor @@ -112,6 +112,9 @@ IN: formatting.tests { "9.88e+417" } [ 987654321098765432 10 400 ^ * "%.2e" sprintf ] unit-test { "9.9e+417" } [ 987654321098765432 10 400 ^ * "%.1e" sprintf ] unit-test { "1e+418" } [ 987654321098765432 10 400 ^ * "%.0e" sprintf ] unit-test +{ "9e+417" } [ 937654321098765432 10 400 ^ * "%.0e" sprintf ] unit-test +{ "1.0e+418" } [ 997654321098765432 10 400 ^ * "%.1e" sprintf ] unit-test +{ "1.00e+418" } [ 999654321098765432 10 400 ^ * "%.2e" sprintf ] unit-test { "1.5625" } [ 1.5625 "%d" sprintf ] unit-test { "1.9p0" } [ 1.5625 "%x" sprintf ] unit-test diff --git a/basis/formatting/formatting.factor b/basis/formatting/formatting.factor index ef0a2ec3b9..7cad3b9448 100644 --- a/basis/formatting/formatting.factor +++ b/basis/formatting/formatting.factor @@ -37,12 +37,15 @@ IN: formatting [ cut* ] tri [ "." glue ] unless-empty ] curry keep neg? [ CHAR: - prefix ] when ; -: format-scientific-mantissa ( x log10x digits -- string ) - swap - 10^ * round-to-even >integer - number>string 1 cut [ "." glue ] unless-empty ; - -: format-scientific-exponent ( log10x -- string ) - number>string 2 CHAR: 0 pad-head +: format-scientific-mantissa ( x log10x digits -- string rounded-up? ) + [ swap - 10^ * round-to-even >integer number>string ] keep + over length 1 - < [ + [ but-last >string ] when ! 9.9 rounded to 1e+01 + 1 cut [ "." glue ] unless-empty + ] keep ; + +: format-scientific-exponent ( rounded-up? log10x -- string ) + swap [ 1 + ] when number>string 2 CHAR: 0 pad-head dup CHAR: - swap index "e" "e+" ? prepend ; : format-scientific-simple ( x digits -- string ) -- 2.34.1