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 )
"+-Z" read-until [
[ string>number ] [ length 10 swap ^ ] bi / +
[ string>number ] [ length 10^ ] bi / +
] dip ;
: (rfc3339>timestamp) ( -- timestamp )

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -73,7 +73,7 @@ MEMO: units ( -- seq ) ! up to 10^99
} cond ;
: 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 ;
: 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 ;
IN: math.text.utils
HELP: 3digit-groups
{ $values { "n" "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." } ;
HELP: digit-groups
{ $values { "n" "a positive integer" } { "k" "a positive integer" } { "seq" "a sequence" } }
{ $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 ;
[ { 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.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences ;
USING: kernel fry math.functions math sequences ;
IN: math.text.utils
: 3digit-groups ( n -- seq )
[ dup 0 > ] [ 1000 /mod ] produce nip ;
: digit-groups ( n k -- seq )
[ 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@
] keep length
10 swap ^ / + swap [ neg ] when ;
10^ / + swap [ neg ] when ;
SYNTAX: DECIMAL: scan parse-decimal parsed ;

View File

@ -1,6 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer.
! 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
! http://projecteuler.net/index.php?section=problems&id=48
@ -17,7 +18,7 @@ IN: project-euler.048
! --------
: euler048 ( -- answer )
1000 [1,b] [ dup ^ ] sigma 10 10 ^ mod ;
1000 [1,b] [ dup ^ ] sigma 10 10^ mod ;
! [ euler048 ] 100 ave-time
! 276 ms run / 1 ms GC ave time - 100 trials

View File

@ -1,11 +1,11 @@
! Copyright (c) 2007, 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
USING: continuations fry io kernel make math math.functions math.parser
math.statistics memory tools.time ;
USING: continuations fry io kernel make math math.functions
math.parser math.statistics memory tools.time ;
IN: project-euler.ave-time
: nth-place ( x n -- y )
10 swap ^ [ * round >integer ] keep /f ;
10^ [ * round >integer ] keep /f ;
: 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 )
{ { 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 ;
: degrees ( deg -- rad ) pi * 180.0 / ;