formatting: don't force the presence of ".0" for %f and %e when precision is 0

It's misleading because you can think you have more precision than you really
do when looking at the output. So instead of "1.0", we can format it as "1" or
"1.".  And instead of "1.0e+00" we can format "1e+00" or "1.e+00".  In C,
printf does not print the radix character, it's prettier. But since the factor
parser accepts both styles, and keeping the radix character gives a stronger sense
that the number is approximate, I prefer to keep the radix character..
modern-harvey2
Jon Harper 2017-02-26 16:55:49 +01:00 committed by John Benediktsson
parent 762b22e1a8
commit 552791d1cf
2 changed files with 42 additions and 11 deletions

View File

@ -35,8 +35,10 @@ IN: formatting.tests
{ "100000000000000000000000.000000" } [ 23 10^ "%f" sprintf ] unit-test
{ "1.2" } [ 125/100 "%.1f" sprintf ] unit-test
{ "1.4" } [ 135/100 "%.1f" sprintf ] unit-test
{ "2.0" } [ 5/2 "%.0f" sprintf ] unit-test
{ "4.0" } [ 7/2 "%.0f" sprintf ] unit-test
{ "2." } [ 5/2 "%.0f" sprintf ] unit-test
{ "4." } [ 7/2 "%.0f" sprintf ] unit-test
{ "2.e+00" } [ 5/2 "%.0e" sprintf ] unit-test
{ "4.e+00" } [ 7/2 "%.0e" sprintf ] unit-test
{ " 1.23" } [ 1.23456789 "%6.2f" sprintf ] unit-test
{ "001100" } [ 12 "%06b" sprintf ] unit-test
{ "==14" } [ 12 "%'=4o" sprintf ] unit-test
@ -85,6 +87,30 @@ IN: formatting.tests
{ "-9007199254740992.0" } [ 53 2^ neg "%.1f" sprintf ] unit-test
{ "-9007199254740993.0" } [ 53 2^ 1 + neg "%.1f" sprintf ] unit-test
{ "987654321098765432" } [ 987654321098765432 "%d" sprintf ] unit-test
{ "987654321098765432.0" } [ 987654321098765432 "%.1f" sprintf ] unit-test
{ "987654321098765432." } [ 987654321098765432 "%.0f" sprintf ] unit-test
{ "9.8765432109876543200e+17" } [ 987654321098765432 "%.19e" sprintf ] unit-test
{ "9.876543210987654320e+17" } [ 987654321098765432 "%.18e" sprintf ] unit-test
{ "9.87654321098765432e+17" } [ 987654321098765432 "%.17e" sprintf ] unit-test
{ "9.8765432109876543e+17" } [ 987654321098765432 "%.16e" sprintf ] unit-test
{ "9.876543210987654e+17" } [ 987654321098765432 "%.15e" sprintf ] unit-test
{ "9.87654321098765e+17" } [ 987654321098765432 "%.14e" sprintf ] unit-test
{ "9.8765432109877e+17" } [ 987654321098765432 "%.13e" sprintf ] unit-test
{ "9.876543210988e+17" } [ 987654321098765432 "%.12e" sprintf ] unit-test
{ "9.87654321099e+17" } [ 987654321098765432 "%.11e" sprintf ] unit-test
{ "9.8765432110e+17" } [ 987654321098765432 "%.10e" sprintf ] unit-test
{ "9.876543211e+17" } [ 987654321098765432 "%.9e" sprintf ] unit-test
{ "9.87654321e+17" } [ 987654321098765432 "%.8e" sprintf ] unit-test
{ "9.8765432e+17" } [ 987654321098765432 "%.7e" sprintf ] unit-test
{ "9.876543e+17" } [ 987654321098765432 "%.6e" sprintf ] unit-test
{ "9.87654e+17" } [ 987654321098765432 "%.5e" sprintf ] unit-test
{ "9.8765e+17" } [ 987654321098765432 "%.4e" sprintf ] unit-test
{ "9.877e+17" } [ 987654321098765432 "%.3e" sprintf ] unit-test
{ "9.88e+17" } [ 987654321098765432 "%.2e" sprintf ] unit-test
{ "9.9e+17" } [ 987654321098765432 "%.1e" sprintf ] unit-test
{ "1.e+18" } [ 987654321098765432 "%.0e" sprintf ] unit-test
{ "1.5625" } [ 1.5625 "%d" sprintf ] unit-test
{ "1.9p0" } [ 1.5625 "%x" sprintf ] unit-test
{ "1.9P0" } [ 1.5625 "%X" sprintf ] unit-test

View File

@ -5,7 +5,7 @@ combinators.smart fry generalizations io io.streams.string
kernel macros math math.functions math.parser namespaces
peg.ebnf present prettyprint quotations sequences
sequences.generalizations strings unicode vectors
math.functions.integer-logs math.order ;
math.functions.integer-logs splitting ;
FROM: math.parser.private => format-float ;
IN: formatting
@ -33,8 +33,8 @@ IN: formatting
[
[ abs ] dip
[ 10^ * round-to-even >integer number>string ]
[ 1 + CHAR: 0 pad-head 2 CHAR: 0 pad-tail ]
[ 1 max cut* ] tri "." glue
[ 1 + CHAR: 0 pad-head ]
[ cut* ] tri "." glue
] curry keep neg? [ CHAR: - prefix ] when ;
: format-scientific-mantissa ( x log10x digits -- string )
@ -63,10 +63,14 @@ IN: formatting
[ [ [ >float ] dip ] when ] keep
] if ;
: ?fix-nonsignificant-zero ( string digits -- string )
[ ".0" "." replace ] [ drop ] if-zero ;
: format-scientific ( x digits -- string )
format-fast-scientific?
[ "e" format-float-fast ]
[ format-scientific-simple ] if ;
format-fast-scientific? [
[ "e" format-float-fast ]
[ ?fix-nonsignificant-zero ] bi
] [ format-scientific-simple ] if ;
: format-fast-decimal? ( x digits -- x' digits ? )
over float? [ t ]
@ -82,9 +86,10 @@ IN: formatting
] if ; inline
: format-decimal ( x digits -- string )
format-fast-decimal?
[ "f" format-float-fast ]
[ format-decimal-simple ] if ;
format-fast-decimal? [
[ "f" format-float-fast ]
[ ?fix-nonsignificant-zero ] bi
] [ format-decimal-simple ] if ;
ERROR: unknown-printf-directive ;