Behavior of division operators between integers (C, C ++, Scala, Java, Rust, Go, PHP, JavaScript, Perl, Python, Ruby)

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

C language / C ++ / Scala / Java / Rust / Go language

a b a / b a % b
5 4 1 1
5 -4 -1 1
-5 4 -1 -1
-5 -4 1 -1

PHP

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

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`.

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`.

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)

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

a b a / b a % b
5 4 1 1
5 -4 -2 -3
-5 4 -2 3
-5 -4 1 -1

Link

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

Behavior of division operators between integers (C, C ++, Scala, Java, Rust, Go, PHP, JavaScript, Perl, Python, Ruby)
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Java VS PHP VS Python VS Ruby
About Perl, Python, PHP, Ruby
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Multi-stage selection (Go / C # / Ruby / Python)
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Closure 4 language comparison (Python, JavaScript, Java, C ++)
[Basic grammar] Differences between Ruby / Python / PHP
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash