Patrick R. Michaud wrote: > On Wed, Jul 08, 2009 at 02:44:24PM -0700, Carl Mäsak wrote: >> <p6eval> rakudo 70bfd5: OUTPUT«1.48547529722733» >> <moritz_> our Num multi method log ( Num $x: Num :$base = Num::e ) is export >> <moritz_> it *should* work. > > Please please please don't constrain the arguments to Num when > you actually write this.
Aye. Attached is a patch that tries to move log() to the setting and implements the two-arg form, but it doesn't seem to call the Complex::log anymore (and thus regresses one test). I've got no idea why; comments appreciated. Cheers, Moritz
>From 83244d4ab2559c08717462b3348906a5e248d883 Mon Sep 17 00:00:00 2001 From: Moritz Lenz <mor...@faui2k3.org> Date: Thu, 9 Jul 2009 00:13:39 +0200 Subject: [PATCH] move log() to the setting; implement 2-arg form --- build/Makefile.in | 1 + src/builtins/any-num.pir | 19 +------------------ src/classes/Complex.pir | 15 +-------------- src/setting/Any-num.pm | 20 ++++++++++++++++++++ src/setting/Complex.pm | 9 +++++++++ src/setting/Num.pm | 2 ++ 6 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 src/setting/Complex.pm diff --git a/build/Makefile.in b/build/Makefile.in index 891978f..ec6267d 100644 --- a/build/Makefile.in +++ b/build/Makefile.in @@ -115,6 +115,7 @@ SETTING = \ src/setting/Any-str.pm \ src/setting/Array.pm \ src/setting/Bool.pm \ + src/setting/Complex.pm \ src/setting/Hash.pm \ src/setting/IO.pm \ src/setting/IO/Socket.pm \ diff --git a/src/builtins/any-num.pir b/src/builtins/any-num.pir index 6eed39c..c0f2f2e 100644 --- a/src/builtins/any-num.pir +++ b/src/builtins/any-num.pir @@ -21,7 +21,7 @@ the size of that file down and to emphasize their generic, .namespace [] .sub 'onload' :anon :init :load $P0 = get_hll_namespace ['Any'] - '!EXPORT'('abs,int,log,polar,sqrt,truncate,unpolar', 'from'=>$P0) + '!EXPORT'('abs,int,polar,sqrt,truncate,unpolar', 'from'=>$P0) ## pre-seed a random number generator $P0 = root_new ['parrot';'Random'] @@ -46,23 +46,6 @@ the size of that file down and to emphasize their generic, .tailcall self.'truncate'() .end -=item log - - our Num multi Num::log ( Num $x: Num :$base ) - our Num multi Math::Basic::log ( Num $x, Num :$base ) - -Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an -error. - -=cut - -.sub 'log' :method :multi(_) - $N0 = self - $N1 = ln $N0 - .return ($N1) -.end - - =item polar =cut diff --git a/src/classes/Complex.pir b/src/classes/Complex.pir index 3bf78d8..e6e9f02 100644 --- a/src/classes/Complex.pir +++ b/src/classes/Complex.pir @@ -27,7 +27,7 @@ Implementation is a bit different from other basic objects (Int...) because p6meta.'register'('Complex', 'parent'=>complexproto, 'protoobject'=>complexproto) $P0 = get_hll_namespace ['Perl6Complex'] - '!EXPORT'('log,polar', 'from'=>$P0) + '!EXPORT'('polar', 'from'=>$P0) .end @@ -61,19 +61,6 @@ Returns the exponential of a Complex. .return ($P0) .end - -=item log - -Returns the logarithm of a Complex. - -=cut - -.namespace ['Perl6Complex'] -.sub 'log' :method :multi('Complex') - $P0 = self.'ln'() - .return ($P0) -.end - =item polar Returns the polar representation of a Complex. diff --git a/src/setting/Any-num.pm b/src/setting/Any-num.pm index 80a8821..dcad7c3 100644 --- a/src/setting/Any-num.pm +++ b/src/setting/Any-num.pm @@ -66,6 +66,26 @@ class Any is also { default { die "Unable to convert to base: $base" } } } + + multi method log() is export { + Q:PIR { + $N0 = self + $N1 = ln $N0 + %r = box $N1 + } + } + multi method log($base) is export { + Q:PIR { + .local num base_log, log_e + $N0 = self + $P0 = find_lex "$base" + $N1 = $P0 + log_e = ln $N0 + base_log = ln $N1 + $N4 = log_e / base_log + %r = box $N4 + } + } } our Num sub rand (*...@args) { diff --git a/src/setting/Complex.pm b/src/setting/Complex.pm new file mode 100644 index 0000000..076b59e --- /dev/null +++ b/src/setting/Complex.pm @@ -0,0 +1,9 @@ +class Complex is also { + multi method log() is export { + Q:PIR { + %r = self.'ln'() + } + } +} + +# vim: ft=perl6 diff --git a/src/setting/Num.pm b/src/setting/Num.pm index 157d18f..8038c5b 100644 --- a/src/setting/Num.pm +++ b/src/setting/Num.pm @@ -257,3 +257,5 @@ class Num is also { self!from-radians($r, $base) } } + +# vim: ft=perl6 -- 1.5.6.5