]> gitweb.factorcode.org Git - factor.git/commitdiff
units: Add exponentiation operator and a d-cube.
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 5 Jan 2019 20:27:42 +0000 (14:27 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 5 Jan 2019 20:37:00 +0000 (14:37 -0600)
extra/units/units-tests.factor
extra/units/units.factor

index b5ff1776c10ff4bef9fd69310d3b430e1e39c256..b191aaf5f114df3a87b3f5381e1e2d154ba07049 100644 (file)
@@ -15,6 +15,12 @@ IN: units.tests
 { t } [ 1 m 2 m 3 m 3array d-product 6 m^3 = ] unit-test
 { t } [ 3 m d-recip 1/3 { } { m } <dimensioned> = ] unit-test
 
+{ t } [ 2 m  3 d^  2 m d-cube = ] unit-test
+
+{ t } [ 2 m  3 d^  8 { m m m } { } <dimensioned> = ] unit-test
+{ t } [ 2 m  -3 d^  1/8 { } { m m m } <dimensioned> = ] unit-test
+{ t } [ 2 m  0 d^  1 scalar = ] unit-test
+
 : km/L ( n -- d ) km 1 L d/ ;
 : mpg ( n -- d ) miles 1 gallons d/ ;
 
index 050956109c3176f3eedc9ddb615f39dd19a4976f..f98483d5e5ce075b3876809ebbcd9ba548a926f7 100644 (file)
@@ -1,6 +1,5 @@
-USING: accessors arrays io kernel math namespaces splitting
-prettyprint sequences sorting vectors words inverse summary
-shuffle math.functions sets ;
+USING: accessors arrays combinators fry inverse kernel math
+math.functions sequences sets shuffle sorting splitting summary ;
 IN: units
 
 TUPLE: dimensioned value top bot ;
@@ -61,11 +60,23 @@ M: dimensions-not-equal summary drop "Dimensions do not match" ;
 
 : d-sq ( d -- d ) dup d* ;
 
+: d-cube ( d -- d ) dup dup d* d* ;
+
 : d-recip ( d -- d' )
     >dimensioned< recip dimension-op> ;
 
 : d/ ( d d -- d ) d-recip d* ;
 
+ERROR: dimensioned-power-op-expects-integer d n ;
+
+: d^ ( d n -- d^n )
+    dup integer? [ dimensioned-power-op-expects-integer ] unless
+    {
+        { [ dup 0 > ] [ 1 - over '[ _ d* ] times ] }
+        { [ dup 0 < ] [ 1 - abs over '[ _ d/ ] times ] }
+        { [ dup 0 = ] [ 2drop 1 scalar ] }
+    } cond ;
+
 : comparison-op ( d d -- n n ) 2dup check-dimensions 2values ;
 
 : d< ( d d -- ? ) comparison-op < ;