Is Ruby's addition, subtraction, multiplication and division the same as in other languages?

An article for beginners in Ruby that describes basic numerical operations such as addition, subtraction, multiplication, and division.

Ruby's arithmetic operations (or four arithmetic operations, addition, subtraction, multiplication, division, etc.) are the same as in other languages.

And so on, for example

puts 20 + 30 # => 50
puts 16 - 9  # => 7
puts 20 * 3  # => 60
puts 60 / 3  # => 20
puts 10 % 3  # => 1

A code sample such as may be written. Even in Qiita, several such articles are written each year.

Of course, whether or not it "changes" depends on what the "other languages" are, but I think it is written with the intention of "many relatively major, ordinary languages."

Regardless of the sum, difference, and product, be careful about quotients and remainders. This varies from language to language.

Is it intended that the division of this code sample chooses a divisible number? I want to know what happens if the number is not divisible. In Ruby, if both the dividend (divided number) and the divisor (divided number) are Integer objects, the division by / is "division". Division is a division that takes the remainder (remainder) into consideration. Therefore, the result is always an integer.

puts 10 / 4 # => 2

If one is not an Integer object, it will not be trimmed.

puts 10.0 / 4 # => 2.5
puts 10r / 4  # => 5/2

In the example above, 10r is a Rational object (rational number object) that is mathematically equal to 10. "It's a rational object with a denominator of 1 and a numerator of 10. The result of 10r / 4 is a Rational object of" 5/2 ".

It is a remarkable feature of Ruby that the division by / between integer objects becomes division. Both JavaScript and PHP

10 / 4

Will be the floating point number 2.5 [^ php].

[^ php]: In PHP, the result is an integer type only when both the divisor and the divisor are integer type, and when it is not divisible, it is a floating point number.

Now, there is one point that needs to be firmly grasped about divorce. That is what happens when at least one of the divisor and divisor is negative. In Ruby, when the divisor is positive, the quotient (quotient in division) is 0 or more and less than the divisor regardless of the sign of the divisor. This is the most familiar definition of natural divorce mathematically [^ quo], but I think it's rather a minority in programming languages.

[^ quo]: But this is not the only definition of divisibility. There are many definitions of divisibility.

puts -10 / 4 # => -3
puts 10 / -4 # => -3
puts -10 / -4 # => 2

Another important thing is that Ruby division isn't just about /. First, if you want a floating-point quotient regardless of the divisor / divisor type, you can use Numeric # fdiv.

puts 10.fdiv(4) # => 2.5

On the other hand, if you want commerce regardless of the divisor / divisor type, use Numeric # div.

puts 10.0.div(4.0) # => 2
puts 3.5.div(1.5)  # => 2

As in the second example of the above code, it is important that you can get the rule even for non-integer numbers [^ nid].

[^ nid]: In mathematics, we often think of dividends only in the world of integers, but in elementary school mathematics we teach decimal dividends. The significance of decimal fractionation is clear when one considers "how many 1.5 m strings he can get from a 3.5 m string".

Conversely, if you want an ordinary quotient regardless of the divisor / divisor type, use Numeric # quo.

puts 10.quo(4) # => 5/2
puts 10.0.quo(4) # => 2.5

In the case of Integers, if it is divisible, it is returned as an Integer, and if it is not divisible, it is returned as Rational.

As you can see from the above, it is not possible to say that "Ruby division is the same as other languages" just by looking at the result of 60/3.

If you talk about division, you should also talk about the remainder, but that's all I wanted to call attention to in this article, so I'll omit it.

Recommended Posts

Is Ruby's addition, subtraction, multiplication and division the same as in other languages?
[Java] Something is displayed as "-0.0" in the output
Implement the same function as C, C ++ system ("cls"); in Java