Ruby language curious integer arithmetic : (-5/2) != -(5/2)
Why does this happen?
It's not quite clear what kind of answer your are looking for other than because that is how it is specified (bold emphasis mine):
15.2.8.3.4
Integer#/
/(
other)
- Visibility: public
- Behavior:
- a) If other is an instance of the class
Integer
:
- 1) If the value of other is 0, raise a direct instance of the class
ZeroDivisionError
.- 2) Otherwise, let n be the value of the receiver divided by the value of other. Return an instance of the class
Integer
whose value is the largest integer smaller than or equal to n.
NOTE The behavior is the same even if the receiver has a negative value. For example,-5 / 2
returns-3
.
As you can see, the specification even contains your exact example.
It is also specified in the Ruby/Spec:
it "supports dividing negative numbers" do (-1 / 10).should == -1end
Compare this with the specification for Float#to_i
(bold emphasis mine):
15.2.9.3.14
Float#to_i
to_i
- Visibility: public
- Behavior: The method returns an instance of the class
Integer
whose value is the integer part of the receiver.
And in the Ruby/Spec:
it "returns self truncated to an Integer" do 899.2.send(@method).should eql(899) -1.122256e-45.send(@method).should eql(0) 5_213_451.9201.send(@method).should eql(5213451) 1.233450999123389e+12.send(@method).should eql(1233450999123) -9223372036854775808.1.send(@method).should eql(-9223372036854775808) 9223372036854775808.1.send(@method).should eql(9223372036854775808)end