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.