Project

General

Profile

Actions

Feature #14924

closed

// floor division operator

Added by Anonymous over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:88018]

Description

Hello!

In my job, I have to do many math operations. And it's kind of a pain because of integer division (3 / 2 == 1). I have to constantly cast integers to floats.

Would it be a good idea to always return a float when you divide two numbers? I mean, 99% of the time you just want that. (The principle of least surprise.)

Python is doing that just right:

3 / 2 == 1.5
3 // 2 == 1

http://python-reference.readthedocs.io/en/latest/docs/operators/floor_division.html

I don't know if it's too much of a breaking change though? Maybe for Ruby3x3?

Thanks.

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

There was an exactly same proposal (I can't remember the issue number), but rejected because it conflicted with a Regexp literal.

Updated by mame (Yusuke Endoh) over 5 years ago

Changing the behavior of Integer#/ has been already rejected.

https://bugs.ruby-lang.org/issues/5512#note-12

Too big incompatibility. Abandoned.

Matz.

It might be possible to add a new operator // as an alias to Integer#quo, but it also brings (relatively small) incompatibility. For example,

x = str.split //
foobar

the program above might be interpreted as x = (str.split) // (foobar) if an operator // is added.

Updated by Anonymous over 5 years ago

Oh too bad regular expressions use the same synthax. :(

Yes, it's a big breaking change to always return a float when dividing two integers. I understand that Matz doesn't want that.

So you can close this ticket then.

Thanks guys!

Updated by Anonymous over 5 years ago

In my job, I have to do many math operations. And it's kind of a pain because of integer division (3 / 2 == 1). I have to constantly cast integers to floats.

I'm not sure if this is better for your use case, but as an
alternative to conversions, we may use:

3.fdiv 2

Would it be a good idea to always return a float when you divide two numbers? I mean, 99% of the time you just want that. (The principle of least surprise.)

I think it may be subjective, 99 % of the time I do want integer
division :-)

Updated by Anonymous over 5 years ago

I think it may be subjective, 99 % of the time I do want integer

Out of curiosity what are you doing that needs integer division? :)

Still I think that 99% of people expect that when you divide 3 / 2 you get 1.5 (it's like that in every calculators in the world).

3 / 2 == 1 is against the principle of least surprise, but hey I understand that we can't change that anymore in Ruby. :)

Updated by Anonymous over 5 years ago

Out of curiosity what are you doing that needs integer division? :)

For example in my window manager I have some rules to determine how
windows should be sized and positioned (tiling).

# how many windows with 484px width would fit in a given screen?
windows_count = screen.width / 484
# what height to use for multiple windows in a "column"?
window_height = column.height / column.windows.size

(this is not my actual code, I changed it slightly for clarity)

In another case, I wanted to overlay some CSV values from telemetry
data on videos. My values are within -1024..1024, and I want to
overlay a "bar chart" that is 720px high for example, so I had code
similar to this:

(telemetry_value + 1024) * 720 / 2048

Last example, I use ruby to generate "templates" for my keyboards
(to draw and cut holes for switches/keys). I was worried about Float
approximation (maybe wrongly), and preferred to calculate
everything with integers representing 1/10 millimeter (the most
precise measurement I work with is only 0.5 mm).

class Key < Struct.new :x, :y, :u, :direction
def urect
Rect.new(
x * U + CASE_PADDING[3],
y * U + CASE_PADDING[0],
U * (direction == :horizontal ? u : 1),
U * (direction == :vertical ? u : 1),
:blue
)
end

def switch
cx, cy = urect.center
Rect.new(
cx - SWITCH_SIZE / 2,
cy - SWITCH_SIZE / 2,
SWITCH_SIZE,
SWITCH_SIZE,
:red
)
end
end

class Rect < Struct.new :x, :y, :width, :height, :color
def center
[x + (width / 2), y + (height / 2)]
end
end

(https://raw.githubusercontent.com/tjouan/keyboards/master/lib/keyboards/layout.rb
for the rest of the code, sorry for ugliness and that github is the
only place I have it public for now)

So in most cases, it was about coordinates : I can't split a pixel
for example. But I also remember using integer division at work for
financial amounts, I cannot split a "cent" either and can't rely on
approximations in such case.

Still I think that 99% of people expect that when you divide 3 / 2 you get 1.5 (it's like that in every calculators in the world).

3 / 2 == 1 is against the principle of least surprise, but hey I understand that we can't change that anymore in Ruby. :)

Yes I have exaggerated in saying the opposite, sorry :-) I
understand that for math and scientific usage it's very different. My
background is mostly C and Ruby, and my "calculator" is almost always
`bc' program so I might be biased here.

--
Thibault Jouan

Updated by mame (Yusuke Endoh) over 5 years ago

  • Status changed from Open to Rejected

Closing this ticket as per OP's request. Thank you for your understanding.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0