ruby picture

RCR 260: Make 'Rational' a built-in class

Submitted by Gavin Sinclair (Mon Jun 14 09:06:14 UTC 2004)

Abstract

Rational is a class to represent rational numbers available via rational.rb in the standard library.

This RCR proposed that it would make sense for the Ruby interpreter to implement Rational directly.

Problem

Sometimes a user will have 'rational' loaded indirectly (e.g. if they load 'date'). That means that they need to be aware of its existence anyway, otherwise they might see their numbers acting in strange ways.

Proposal

Implement the Rational class in the interpreter. There are design decisions to be made (see Analysis), so a more detailed proposal might appear in a future RCR after some discussion. It is probably not enough to simply implement all of rational.rb in C.

Analysis

Benefits

In terms of benefits, I believe that Rational is a fundamental type, and its use would be encouraged by the exposure and quality of implementation that inclusion in the core would bring.

It might in fact be a better model of programming to always use rational numbers as the result of integer division, and explicitly convert them to Floats when required.

Performance

I doubt that making Rational a built-in class would negatively impact interpreter performance to any noticable degree. Clearly there would be a performance benefit to classes _using_ Rational, but I can't comment on the significance of that.

More Consideration Required

The behaviour of many regular numerical methods depends on whether 'rational' has been loaded. While I can't provide any If Rational were a built-in class, then one set of behaviour would need to be defined, which might be a difficult design choice to make. It's probably better, however, for numbers to behave in a consistent -- and preferably convenient and powerful -- way.

Future RCRs

Future RCRs might consider the elevation of Complex to a built-in class as well. Concerns have been expressed about the integrity of the various standard mathematical packages, in that Matrix misbehaves unless Rational is also used. (Search ruby-core for discussions.) It would therefore be prudent to _consider_ the codification of the behaviour of numbers for Ruby 2.0.

If this RCR is sufficiently discussed to elicit more detail, then a more detailed RCR to supercede this one will likely appear in future.

Implementation

I can't provide an implementation at the moment, and I won't be able to procide a C implementation ever.

If I had more detail about the preferred default behaviour of integers, assuming Rational was built in, I could offer a Ruby implementation. (That would probably be a small variation on require 'rational'.)

ruby picture
Comments Current voting
Strongly opposed 0
Opposed 1
Neutral 0
In favor 4
Strongly advocate 5
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .