formatting, workaround bug for "%.0e" and "%.0f" on windows

On windows, the underlying formatting implementation
uses a precison of 6 when asked to do a precision of 0.
Improve the post-processing so that it doesn't fully break
the formatted number. The previous implementation would change
"1.00000" to "10000", and 1.00000e0 to 10000e0...

With this, windows has a different formatting for "%.0f" and "%.0e"
but at least it's the right number
modern-harvey2
Jon Harper 2017-07-07 00:21:38 +02:00 committed by Björn Lindqvist
parent 7c2e964fc9
commit e470a5bb28
2 changed files with 6 additions and 7 deletions

View File

@ -106,6 +106,9 @@ IN: formatting.tests
{ "9.877e+417" } [ 987654321098765432 10 400 ^ * "%.3e" sprintf ] unit-test
{ "9.88e+417" } [ 987654321098765432 10 400 ^ * "%.2e" sprintf ] unit-test
{ "9.9e+417" } [ 987654321098765432 10 400 ^ * "%.1e" sprintf ] unit-test
! This works even on windows (even though %.0e is special on
! windows) because it doesn't use the fast formatter from the
! system
{ "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
@ -167,5 +170,4 @@ ${ os windows? "3" "2" ? } [ 5/2 "%.0f" sprintf ] unit-test
! Differences on Windows due to setprecision(0)
${ os windows? "2.500000e+00" "2e+00" ? } [ 5/2 "%.0e" sprintf ] unit-test
${ os windows? "3.500000e+00" "4e+00" ? } [ 7/2 "%.0e" sprintf ] unit-test
{ "1e+00" } [ 1.0 "%.0e" sprintf ] unit-test
${ os windows? "1.000000e+00" "1e+00" ? } [ 1.0 "%.0e" sprintf ] unit-test

View File

@ -66,13 +66,10 @@ 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 ]
[ ?fix-nonsignificant-zero ] bi
[ [ ".0e" "e" replace ] [ drop ] if-zero ] bi
] [ format-scientific-simple ] if ;
: format-fast-decimal? ( x digits -- x' digits ? )
@ -91,7 +88,7 @@ IN: formatting
: format-decimal ( x digits -- string )
format-fast-decimal? [
[ "f" format-float-fast ]
[ ?fix-nonsignificant-zero ] bi
[ [ ".0" ?tail drop ] [ drop ] if-zero ] bi
] [ format-decimal-simple ] if ;
ERROR: unknown-printf-directive ;