From 4fef246ca4aa4d43c345fe8a98e54f9f982b40cf Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Tue, 11 Aug 2009 18:00:24 -0500 Subject: [PATCH] add 10^ to math.functions and update usages --- basis/calendar/format/format.factor | 2 +- basis/formatting/formatting.factor | 2 +- basis/io/files/info/windows/windows.factor | 6 +++--- basis/math/functions/functions.factor | 6 +++++- extra/math/analysis/analysis.factor | 4 ++-- extra/math/text/english/english.factor | 2 +- extra/math/text/french/french.factor | 2 +- extra/math/text/utils/utils-docs.factor | 6 +++--- extra/math/text/utils/utils-tests.factor | 2 +- extra/math/text/utils/utils.factor | 6 +++--- extra/money/money.factor | 2 +- extra/project-euler/048/048.factor | 5 +++-- extra/project-euler/ave-time/ave-time.factor | 6 +++--- extra/svg/svg.factor | 2 +- 14 files changed, 29 insertions(+), 24 deletions(-) mode change 100644 => 100755 extra/math/text/utils/utils-docs.factor mode change 100644 => 100755 extra/math/text/utils/utils-tests.factor mode change 100644 => 100755 extra/math/text/utils/utils.factor diff --git a/basis/calendar/format/format.factor b/basis/calendar/format/format.factor index ad43cc2f1d..a187f0c9af 100644 --- a/basis/calendar/format/format.factor +++ b/basis/calendar/format/format.factor @@ -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 ) diff --git a/basis/formatting/formatting.factor b/basis/formatting/formatting.factor index f8b9ba501b..55ebdf1442 100644 --- a/basis/formatting/formatting.factor +++ b/basis/formatting/formatting.factor @@ -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 ) [ diff --git a/basis/io/files/info/windows/windows.factor b/basis/io/files/info/windows/windows.factor index 81e43f8dd9..88e1547b7b 100755 --- a/basis/io/files/info/windows/windows.factor +++ b/basis/io/files/info/windows/windows.factor @@ -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 ; diff --git a/basis/math/functions/functions.factor b/basis/math/functions/functions.factor index 314062591d..3cbe8e19d4 100644 --- a/basis/math/functions/functions.factor +++ b/basis/math/functions/functions.factor @@ -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 ; diff --git a/extra/math/analysis/analysis.factor b/extra/math/analysis/analysis.factor index a1fc0bd07b..16a45fc691 100755 --- a/extra/math/analysis/analysis.factor +++ b/extra/math/analysis/analysis.factor @@ -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 text) ( n -- str ) - [ negative-text ] [ abs 3digit-groups recombine ] bi append ; + [ negative-text ] [ abs 3 digit-groups recombine ] bi append ; PRIVATE> diff --git a/extra/math/text/french/french.factor b/extra/math/text/french/french.factor index f8b97103eb..46e326b7e7 100644 --- a/extra/math/text/french/french.factor +++ b/extra/math/text/french/french.factor @@ -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 ; diff --git a/extra/math/text/utils/utils-docs.factor b/extra/math/text/utils/utils-docs.factor old mode 100644 new mode 100755 index e1d1a005d3..2352ab9488 --- a/extra/math/text/utils/utils-docs.factor +++ b/extra/math/text/utils/utils-docs.factor @@ -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." } ; diff --git a/extra/math/text/utils/utils-tests.factor b/extra/math/text/utils/utils-tests.factor old mode 100644 new mode 100755 index d14bb06a2a..04fbcdc1a7 --- a/extra/math/text/utils/utils-tests.factor +++ b/extra/math/text/utils/utils-tests.factor @@ -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 diff --git a/extra/math/text/utils/utils.factor b/extra/math/text/utils/utils.factor old mode 100644 new mode 100755 index 422a79a1f3..13551f19e4 --- a/extra/math/text/utils/utils.factor +++ b/extra/math/text/utils/utils.factor @@ -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 ; diff --git a/extra/money/money.factor b/extra/money/money.factor index 994d214335..36dedb2a65 100644 --- a/extra/money/money.factor +++ b/extra/money/money.factor @@ -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 ; diff --git a/extra/project-euler/048/048.factor b/extra/project-euler/048/048.factor index 640a3a68f6..fde3fa6026 100644 --- a/extra/project-euler/048/048.factor +++ b/extra/project-euler/048/048.factor @@ -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 diff --git a/extra/project-euler/ave-time/ave-time.factor b/extra/project-euler/ave-time/ave-time.factor index a7762836f1..6c555f92b5 100644 --- a/extra/project-euler/ave-time/ave-time.factor +++ b/extra/project-euler/ave-time/ave-time.factor @@ -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 ) [ diff --git a/extra/svg/svg.factor b/extra/svg/svg.factor index 2ed5d21707..2d2d38314a 100644 --- a/extra/svg/svg.factor +++ b/extra/svg/svg.factor @@ -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 / ;