modulo of a number - python vs c# -


lets take basic arithmetic operation - modulo

i different outputs depending on different languages.

python

>>> -1 % 12 11 

c#

var res = -1 % 12; output: res = -1 

why seeing such behaviour? ideally i'd output 11 in both cases.

also know if can achieve in c#?

the premise of question incorrect. % operator in c# not modulus operator, remainder operator, while in python is modulus operator.

as eric lippert describes, modulus , remainder same positive numbers, handle negative numbers differently.

despite both c# , python having % operator, doesn't mean both represent modulus.

it's worth noting other languages, such c++ , java use remainder % operator, not modulus, contributed why c# choose use remainder well. since there isn't lot of consistency in meant % operator, suggest looking in language docs whenever working new language.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -