ruby picture

RCR 319: Add a backslash operator for integer and matrix division.

Submitted by Olathe (Tue Sep 13 16:08:58 UTC 2005)

Abstract

Make mathn the default way of doing math in Ruby. Make #/ work as it does in mathematics and add a #\ operator for concise integer division and matrix left-division.

Problem

There is no concise way of doing both normal or integer division. There is no concise way of doing both right and left matrix division.

Since mathn is optional, library developers have the added headache of making sure their integer division #/ is changed to #div, just in case; if they don't, oops !

All code writers must use the Java-like num.div(other) if they want to do both integer division and use the niceties of mathn.

Proposal

Integer division :
 p 25 / 3  # displays 25/3
 p 25 \ 3  # displays 8

Matrices :

 a = Matrix.new(...)
 b = Matrix.new(...)
 p [a / b, b \ a]  # Displays both right and left division.

Sets :

 a = Set.new(...)
 b = Set.new(...)
 p a \ b  # Displays all elements in a that aren't in b.

Line continuation :

 A really, really long line that must be \\
 broken into two.

Analysis

This leads to a nice and concise way of telling Ruby which division you mean. It's nice mathematically, as #/ corresponds to normal math notation and it allows #\ to be used, as it is with sets and sometimes with matrices.

Integer division is not the common meaning of #/, but it is used frequently in computing. #\ will easily be picked up after a few uses and lets programming newbies know something different from normal math notation is happening.

The #\ operator has been used successfully in both BASIC (at least QBASIC) and MATLAB.

Implementation

A small change would need to be made to the parser, the Integer classes, and any Matrix and Set classes in the standard library.

I'll have to leave those to more Ruby-internals-experienced people.

ruby picture
Comments Current voting
I don't like it, because \ is an escape character. I don't honestly think that it's a big deal overall. With a different character (but which), maybe. But \ has too much meaning already. I also think that this would be *too* dramatic a change, changing the very nature of working with Ruby. --Austin


Symbol characters are too rare to be used so lightly. I think you'd want much broader use before considering something like this. This also breaks code that assumes integer division yields an integer (sounds like this RCR wants it to be a float). Just put a .to_f on one of your operands and be done with it. Eric


Actually, I agree in part with the RCR. The fact that 3/2 is either 1 or three-halfs depending on whether mathn is loaded or not is not a good thing.

Also, I would argue that changing the behaviour of / depending upon whether its arguments are float or int is not good either. Integer and floating point divisions are rarely substitutable for each other in any algorithm. Therefore almost every use of the division operator with non-literals should apply to_i or to_f to its non-literal arguments to ensure the proper operation.

That being said, I don't like the choice of backslash for integer division, I think there are too many problems with that choice. Other choices are not much better. // is possible, but it looks like an empty regular expression. @/ is another possibilty (but its kinda ugly, don'tcha think?).

In the end, we already have .div for integer division. Mathn adds .rdiv for rational division. What would make me happy is something that unconditionally represents floating point division, perhaps a .fdiv ? Then leave / for everyone who doesn't care what kind of division they are performing.

Sorry for the rant. I'm going to remain neutral on this RCR for now. I agree something should be done, but I don't like the backslash operator.

-- Jim Weirich


(Responding to an earlier comment):

This also breaks code that assumes integer division yields an integer

Code that assumes integer division yields an integer is already broken in that it cannot be used in any program that might also require the mathn library.

That's part of the current problem. -- Jim


The behavior of / is the thing I like least about Ruby.

This is one of those things that has come up again and again. Olathe, you should go read the several discussions which have taken place on ruby-talk and try to strengthen the argument or I fear this RCR will be quickly rejected.


I'd prefer the syntax be different ('//' perhaps?), but I like it otherwise.


Strongly opposed 3
Opposed 4
Neutral 1
In favor 3
Strongly advocate 2
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 .