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?

5
  • 3
    I don't see what it has to do with being a compiled or interpreted language. Commented Jan 9, 2018 at 15:00
  • 4
    Different languages, different rules. There’s logic behind each result; the logic is different, that’s all. Commented Jan 9, 2018 at 15:01
  • 1
    @JonathanLeffler op simply asks what the logic is. That's all Commented Jan 9, 2018 at 15:01
  • Just add 3 if you have a negative number. Technically this two numbers represents the same number under the module world Commented Jan 9, 2018 at 15:04
  • @jsheeran then why such similarity in answers? Commented Jan 9, 2018 at 15:09

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.