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.modern-harvey2
parent
4df21818f5
commit
1dceb069ad
|
@ -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
|
||||
|
|
|
@ -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-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 ( log10x -- string )
|
||||
number>string 2 CHAR: 0 pad-head
|
||||
: 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 )
|
||||
|
|
Loading…
Reference in New Issue