0

-14%3=?

C gives me -2 and python gives me 1. I have also tried many languages, all of them are having either giving 1or -2. Then I checked again, and found out that the compilers are giving -2 and the interpreters are giving 1. But JavaScript being an interpreter language, gives -2. Why?

1 Answers1

2

This is not a matter of compiling vs interpreted languages. Each language has its own rules.

For Python, you always get a result with the same sign as the second operator.

The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand

https://docs.python.org/2/reference/expressions.html

Wai Ha Lee
  • 8,173
  • 68
  • 59
  • 86
klutt
  • 28,260
  • 15
  • 48
  • 83