Sins of a Logger

I’ve been having a terrible time getting the log method to work in the Numeric / Real framework. Essentially, the problem is that there are two different concepts of log going on: log with respect to a specified base, which is defined in terms of the natural log. Perl 6’s log command combines these two into one function, with a default base of e. Still, splitting up the function into a multi was the easiest way to implement this: multi method log(Num $x:), which is used to implement multi method log(Num $x: Num $base).

That worked fine when log was not a part of Numeric or Real, but failed in different mysterious ways each time I tried to extend it to those roles. Finally this week I realized the problem: multis and roles do not mix yet in Rakudo. At all.

Knowing that, it was easy to figure out what the next step should be. I just renamed the natural log function to be log-e. (Think I will rename it to ln in a second.) Then I implemented method log(Numeric $x: Numeric $base = e) using it. This has the additional advantage of properly supporting logs where $x and $base are different types. As long as each type has log-e defined for it, and the results of those calls can be divided, the mixed type will be handled properly.

This works well in current Rakudo. Pleased with this, I went on to move the sin method to Numeric and Real as well — the trick there turned out to be removing multi from the declarations of Num.sin and Complex.sin. With that established, porting all the trig functions to the new system is just a matter of time. I do need to sit down and rethink the trig tests a bit.

Thanks to the Perl Foundation and Ian Hague for supporting this work.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: