add 10^ to math.functions and update usages

db4
Doug Coleman 2009-08-11 18:00:24 -05:00
parent 14ef1649d4
commit 4fef246ca4
14 changed files with 29 additions and 24 deletions

View File

@ -162,7 +162,7 @@ M: timestamp year. ( timestamp -- )
: read-rfc3339-seconds ( s -- s' ch ) : read-rfc3339-seconds ( s -- s' ch )
"+-Z" read-until [ "+-Z" read-until [
[ string>number ] [ length 10 swap ^ ] bi / + [ string>number ] [ length 10^ ] bi / +
] dip ; ] dip ;
: (rfc3339>timestamp) ( -- timestamp ) : (rfc3339>timestamp) ( -- timestamp )

View File

@ -32,7 +32,7 @@ IN: formatting
[ "." split1 ] dip [ CHAR: 0 pad-tail ] [ head-slice ] bi "." glue ; [ "." split1 ] dip [ CHAR: 0 pad-tail ] [ head-slice ] bi "." glue ;
: max-digits ( n digits -- n' ) : max-digits ( n digits -- n' )
10 swap ^ [ * round ] keep / ; inline 10^ [ * round ] keep / ; inline
: >exp ( x -- exp base ) : >exp ( x -- exp base )
[ [

View File

@ -9,11 +9,11 @@ calendar ascii combinators.short-circuit locals ;
IN: io.files.info.windows IN: io.files.info.windows
:: round-up-to ( n multiple -- n' ) :: round-up-to ( n multiple -- n' )
n multiple rem dup 0 = [ n multiple rem [
drop n n
] [ ] [
multiple swap - n + multiple swap - n +
] if ; ] if-zero ;
TUPLE: windows-file-info < file-info attributes ; TUPLE: windows-file-info < file-info attributes ;

View File

@ -104,10 +104,12 @@ PRIVATE>
: divisor? ( m n -- ? ) : divisor? ( m n -- ? )
mod 0 = ; mod 0 = ;
ERROR: non-trivial-divisor n ;
: mod-inv ( x n -- y ) : mod-inv ( x n -- y )
[ nip ] [ gcd 1 = ] 2bi [ nip ] [ gcd 1 = ] 2bi
[ dup 0 < [ + ] [ nip ] if ] [ dup 0 < [ + ] [ nip ] if ]
[ "Non-trivial divisor found" throw ] if ; foldable [ non-trivial-divisor ] if ; foldable
: ^mod ( x y n -- z ) : ^mod ( x y n -- z )
over 0 < [ over 0 < [
@ -116,6 +118,8 @@ PRIVATE>
-rot (^mod) -rot (^mod)
] if ; foldable ] if ; foldable
: 10^ ( n -- n' ) 10 swap ^ ; inline
GENERIC: absq ( x -- y ) foldable GENERIC: absq ( x -- y ) foldable
M: real absq sq ; M: real absq sq ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Doug Coleman, Slava Pestov, Aaron Schaefer. ! Copyright (C) 2008 Doug Coleman, Slava Pestov, Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel math math.constants math.functions USING: combinators.short-circuit kernel math math.constants
math.vectors sequences ; math.functions math.vectors sequences ;
IN: math.analysis IN: math.analysis
<PRIVATE <PRIVATE

View File

@ -87,7 +87,7 @@ SYMBOL: and-needed?
] if ; ] if ;
: (number>text) ( n -- str ) : (number>text) ( n -- str )
[ negative-text ] [ abs 3digit-groups recombine ] bi append ; [ negative-text ] [ abs 3 digit-groups recombine ] bi append ;
PRIVATE> PRIVATE>

View File

@ -73,7 +73,7 @@ MEMO: units ( -- seq ) ! up to 10^99
} cond ; } cond ;
: over-1000000 ( n -- str ) : over-1000000 ( n -- str )
3digit-groups [ 1+ units nth n-units ] map-index sift 3 digit-groups [ 1+ units nth n-units ] map-index sift
reverse " " join ; reverse " " join ;
: decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip complete ; : decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip complete ;

6
extra/math/text/utils/utils-docs.factor Normal file → Executable file
View File

@ -1,6 +1,6 @@
USING: help.markup help.syntax ; USING: help.markup help.syntax ;
IN: math.text.utils IN: math.text.utils
HELP: 3digit-groups HELP: digit-groups
{ $values { "n" "a positive integer" } { "seq" "a sequence" } } { $values { "n" "a positive integer" } { "k" "a positive integer" } { "seq" "a sequence" } }
{ $description "Decompose a number into 3 digits groups and return them in a sequence, starting with the units, then the tenths, etc." } ; { $description "Decompose a number into groups of " { $snippet "k" } " digits and return them in a sequence starting with the least significant grouped digits first." } ;

2
extra/math/text/utils/utils-tests.factor Normal file → Executable file
View File

@ -1,3 +1,3 @@
USING: math.text.utils tools.test ; USING: math.text.utils tools.test ;
[ { 1 999 2 } ] [ 2999001 3digit-groups ] unit-test [ { 1 999 2 } ] [ 2999001 3 digit-groups ] unit-test

6
extra/math/text/utils/utils.factor Normal file → Executable file
View File

@ -1,7 +1,7 @@
! Copyright (c) 2007, 2008 Aaron Schaefer. ! Copyright (c) 2007, 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences ; USING: kernel fry math.functions math sequences ;
IN: math.text.utils IN: math.text.utils
: 3digit-groups ( n -- seq ) : digit-groups ( n k -- seq )
[ dup 0 > ] [ 1000 /mod ] produce nip ; [ dup 0 > ] swap '[ _ 10^ /mod ] produce nip ;

View File

@ -28,6 +28,6 @@ ERROR: not-an-integer x ;
[ [
[ dup string>number [ nip ] [ not-an-integer ] if* ] bi@ [ dup string>number [ nip ] [ not-an-integer ] if* ] bi@
] keep length ] keep length
10 swap ^ / + swap [ neg ] when ; 10^ / + swap [ neg ] when ;
SYNTAX: DECIMAL: scan parse-decimal parsed ; SYNTAX: DECIMAL: scan parse-decimal parsed ;

View File

@ -1,6 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer. ! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math math.functions math.ranges project-euler.common sequences ; USING: kernel math math.functions math.ranges
project-euler.common sequences ;
IN: project-euler.048 IN: project-euler.048
! http://projecteuler.net/index.php?section=problems&id=48 ! http://projecteuler.net/index.php?section=problems&id=48
@ -17,7 +18,7 @@ IN: project-euler.048
! -------- ! --------
: euler048 ( -- answer ) : euler048 ( -- answer )
1000 [1,b] [ dup ^ ] sigma 10 10 ^ mod ; 1000 [1,b] [ dup ^ ] sigma 10 10^ mod ;
! [ euler048 ] 100 ave-time ! [ euler048 ] 100 ave-time
! 276 ms run / 1 ms GC ave time - 100 trials ! 276 ms run / 1 ms GC ave time - 100 trials

View File

@ -1,11 +1,11 @@
! Copyright (c) 2007, 2008 Aaron Schaefer. ! Copyright (c) 2007, 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: continuations fry io kernel make math math.functions math.parser USING: continuations fry io kernel make math math.functions
math.statistics memory tools.time ; math.parser math.statistics memory tools.time ;
IN: project-euler.ave-time IN: project-euler.ave-time
: nth-place ( x n -- y ) : nth-place ( x n -- y )
10 swap ^ [ * round >integer ] keep /f ; 10^ [ * round >integer ] keep /f ;
: collect-benchmarks ( quot n -- seq ) : collect-benchmarks ( quot n -- seq )
[ [

View File

@ -11,7 +11,7 @@ XML-NS: inkscape-name http://www.inkscape.org/namespaces/inkscape
: svg-string>number ( string -- number ) : svg-string>number ( string -- number )
{ { CHAR: E CHAR: e } } substitute "e" split1 { { CHAR: E CHAR: e } } substitute "e" split1
[ string>number ] [ [ string>number 10 swap ^ ] [ 1 ] if* ] bi* * [ string>number ] [ [ string>number 10^ ] [ 1 ] if* ] bi* *
>float ; >float ;
: degrees ( deg -- rad ) pi * 180.0 / ; : degrees ( deg -- rad ) pi * 180.0 / ;