The behavior of the /
and %
operators on integers.
If you divide an integer by an integer ** Integer ** vs ** Floating Point ** Which?
---5 / 4
is **-1.25 **
- PHP
--Floating point number because it is not divisible
- Perl
--Floating point number because it is not divisible
--Declaring ʻuse integerchanges to C language school - Python --Floating point number if divided regardless of whether it is divisible --There is also an operator
// for the C language group - JavaScript --There is no distinction between integers and floating point numbers from the beginning --
-5 / 4 is **-1 ** --C language / C ++ / Scala / Java / Rust / Go language --Statically typed language --
-5 / 4` is **-2 **
- Ruby
--Out of friends
Which is the negative remainder (division remainder %
operator) ** positive ** vs ** negative **?
---5% 4
is **-1 **
--C language / C ++ / Scala / Java / Rust / Go language
--Statically typed language
- PHP / JavaScript
---5% 4
is ** 3 **
- Perl
--Declaring ʻuse integer` changes to C language school
- Python / Ruby
/
--When an integer is divided by an integer, it becomes an integer.
--Rounded closer to 0
--%
(remainder)
--(a / b * b) + (a% b)
matches ʻa`a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1 | 1 |
5 | -4 | -1 | 1 |
-5 | 4 | -1 | -1 |
-5 | -4 | 1 | -1 |
PHP
/
--If you divide an integer by an integer, it becomes a floating point number if it is not divisible.
--%
(remainder)
--(int) (a / b) * b + (a% b)
matches ʻa` (cast to int rounds closer to 0)a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1.25 | 1 |
5 | -4 | -1.25 | 1 |
-5 | 4 | -1.25 | -1 |
-5 | -4 | 1.25 | -1 |
JavaScript
/
--If you divide an integer by an integer, it becomes a floating point number if it is not divisible.
--JavaScript does not distinguish between integers and floating point numbers in the first place
-Complete understanding of JavaScript numeric types --Qiita
--%
(remainder)
--Math.trunc (a / b) * b + (a% b)
matches ʻa` (Math.trunc is rounded closer to 0)a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1.25 | 1 |
5 | -4 | -1.25 | 1 |
-5 | 4 | -1.25 | -1 |
-5 | -4 | 1.25 | -1 |
Perl
If you have not declared ʻuse integer`.
/
--If you divide an integer by an integer, it becomes a floating point number if it is not divisible.
--%
(remainder)
--floor (a / b) * b + (a% b)
matches ʻa` (floor rounds in the direction of negative infinity)a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1.25 | 1 |
5 | -4 | -1.25 | -3 |
-5 | 4 | -1.25 | 3 |
-5 | -4 | 1.25 | -1 |
Perl (use integer)
If you declare ʻuse integer`.
/
--When an integer is divided by an integer, it becomes an integer.
--Rounded closer to 0
--%
(remainder)
--(a / b * b) + (a% b)
matches ʻa`a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1 | 1 |
5 | -4 | -1 | 1 |
-5 | 4 | -1 | -1 |
-5 | -4 | 1 | -1 |
Python
For Python3. (2 is different)
/
--When you divide an integer by an integer, it always becomes a floating point number.
--//
(integer division)
--When an integer is divided by an integer, it becomes an integer.
--Rounded in the direction of negative infinity
--%
(remainder)
--(a // b * b) + (a% b)
matches ʻa`a | b | a / b |
a // b |
a % b |
---|---|---|---|---|
5 | 4 | 1.25 | 1 | 1 |
5 | -4 | -1.25 | -2 | -3 |
-5 | 4 | -1.25 | -2 | 3 |
-5 | -4 | 1.25 | 1 | -1 |
Ruby
/
--When an integer is divided by an integer, it becomes an integer.
--Rounded in the direction of negative infinity
--%
(remainder)
--(a / b * b) + (a% b)
matches ʻa`a | b | a / b |
a % b |
---|---|---|---|
5 | 4 | 1 | 1 |
5 | -4 | -2 | -3 |
-5 | 4 | -2 | 3 |
-5 | -4 | 1 | -1 |
My article in a multilingual series
-[Summary of how to write increment decrement (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby, JavaScript)](https://qiita.com/suzuki-navi/items/ 6611b9f16a391bac5ac5) -Summary of how to write if statement (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Recommended Posts