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

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -