]> gitweb.factorcode.org Git - factor.git/commitdiff
decimals: adding prettyprint.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 19:47:37 +0000 (12:47 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 18 Mar 2021 19:47:37 +0000 (12:47 -0700)
extra/decimals/decimals-tests.factor
extra/decimals/decimals.factor

index 4a9baced073fb1bf0a025db5c27f49d24b5d970b..ce415d87cbbf1cc30b33be98c740d75e3c8cf72a 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: continuations decimals grouping kernel kernel.private literals
-locals math math.functions math.order random tools.test ;
+USING: continuations decimals grouping kernel kernel.private
+literals locals math math.functions math.order prettyprint
+random tools.test ;
 IN: decimals.tests
 
 { t } [
@@ -49,3 +50,11 @@ ERROR: decimal-test-failure D1 D2 quot ;
 { f } [ DECIMAL: -2 DECIMAL: -2 before? ] unit-test
 { t } [ DECIMAL: -3 DECIMAL: -2 before? ] unit-test
 { t } [ DECIMAL: .5 DECIMAL: 0 DECIMAL: 1.0 between? ] unit-test
+
+{ "DECIMAL: 0" } [ DECIMAL: 0 unparse ] unit-test
+{ "DECIMAL: 0.1" } [ DECIMAL: 0.1 unparse ] unit-test
+{ "DECIMAL: 1" } [ DECIMAL: 1.0 unparse ] unit-test
+{ "DECIMAL: 1.01" } [ DECIMAL: 1.01 unparse ] unit-test
+{ "DECIMAL: -0.1" } [ DECIMAL: -0.1 unparse ] unit-test
+{ "DECIMAL: -1" } [ DECIMAL: -1.0 unparse ] unit-test
+{ "DECIMAL: -1.01" } [ DECIMAL: -1.01 unparse ] unit-test
index d371a5932a3c99a8a261e7b0b9169b987062ce62..cb33fb4e2c9c5229f8eefbc2e12fdfe33cd9cd84 100644 (file)
@@ -1,7 +1,9 @@
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors combinators.short-circuit kernel lexer locals
-math math.functions math.order math.parser sequences splitting ;
+USING: accessors combinators.short-circuit formatting.private
+kernel lexer locals math math.functions math.order math.parser
+prettyprint.backend prettyprint.custom prettyprint.sections
+sequences splitting ;
 IN: decimals
 
 TUPLE: decimal { mantissa read-only } { exponent read-only } ;
@@ -81,3 +83,8 @@ M: decimal before?
 
 M: decimal <=>
     2dup before? [ 2drop +lt+ ] [ equal? +eq+ +gt+ ? ] if ; inline
+
+M: decimal pprint*
+    \ DECIMAL: [
+        >decimal< [ 10^ * ] [ abs format-decimal ] bi text
+    ] pprint-prefix ;