Further Thoughts On Real

So, in my first post on this subject I referred to a lingua franca type RealX for the Real role, without specifying what type RealX should be. There was a very good reason for that: I don’t know what type it should be. But I realized today that might be a feature rather than a bug.

My original assumption, when I first started thinking about these issues, was that RealX obviously had to be Num. That’s what the current source effectively does, after all. And Nums are wonderfully versatile.

For a while, though, I worried about precision. Obviously a FatRat can be much more precise than a Num. After consideration, I’m not sure how worried I am about that — as long as you are not doing abstract math, tolerance is still an issue, no matter what, and using FatRat borders on fooling yourself into not worrying about it.

But there is a much bigger catch to using Num. Once Rakudo has proper bignums, it will be child’s play to create an Int which cannot be converted to a meaningful Num. And that seems like a big problem with the idea of using Num for RealX.

On the other hand, FatRat would be a pretty reasonable RealX, I think, except that it doesn’t exist yet!

What to do? Well, look back at my example code for .RealX back in the previous post:

    multi method RealX() {
         $.cents.RealX / 100.RealX;
    }

What jumps out at me today is that this code works no matter what type RealX is! Basically it's all a matter of expressing the value of you Real in terms of simpler Real types that already have RealX defined. All you need is for your Perl 6 implementation to define valid RealX methods for Int and Num.

So my proposal is we come up with a better name for the RealX method, and let the type it returns be implementation-defined. With any luck, that would let Rakudo use the practical current solution of Num, and still seamlessly switch over to something like FatRat when it is available.

(BTW, I think if you combine this with a great suggestion from Larry Wall, you also can extend this solution to work for general Numeric types, at least for comparisons. But that's the subject for another post.)

About these ads

2 Responses to “Further Thoughts On Real”

  1. Numeric Plan of Attack « Just Rakudo It Says:

    [...] like to sketch out my current plan of attack on this grant. I’ve already blogged twice on changes I think the Numeric/Real spec needs. Those ideas still need some more thought, and [...]

  2. Dipping My Toe In « Just Rakudo It Says:

    [...] At any rate, I definitely consider this a good start. I think my next step is probably to figure out a better (if still short-term) name than RealX… [...]

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: