]> gitweb.factorcode.org Git - factor.git/commitdiff
formatting, workaround bug for "%.0e" and "%.0f" on windows
authorJon Harper <jon.harper87@gmail.com>
Thu, 6 Jul 2017 22:21:38 +0000 (00:21 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Sat, 8 Jul 2017 16:40:08 +0000 (18:40 +0200)
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

basis/formatting/formatting-tests.factor
basis/formatting/formatting.factor

index 543eff4fdd182e61a94bd89a6df295a5ba8edabc..80b5e41c0649ba013dffcf9148f8dd1486b5bad2 100755 (executable)
@@ -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
index 388d5852c6fd30b0e9fbc3c9e1f5678080b1eb04..4a4c7ca66c8f4e97694ec7fcbe001c85b76d3ec0 100644 (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 ;