How come on a calculator -1 mod 26 = 25, but in C or Java -1 % 26 == -1. I need a program which solves it like the calculator. Is there a difference between the two?
How come on a calculator -1 mod 26 = 25, but in C or
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both answers (25 and -1) are valid. It’s just that different systems have different conventions.
The one I see the most common (in mathematics) is:
Where
floor()is to round towards negative infinity.This is the convention that your calculator is giving you. (Mathematica also uses this convention.)
The one I think most programming languages use is:
Where
integerpart()is the same as a (float -> integer) cast. (round towards zero)Some conventions like to keep the remainder the same sign as one of the operands.
The same thing applies to the sign of the divider. Different conventions are different.