The C# mod operator, %, takes the sign of the dividend as the sign of the result. Simply, this means that -1 % 3 is -1, and not 2 as I was expecting. Apparently every language I've ever programmed professionally in behaves the same as C#, so I'm surprised I never noticed it before. Of the other 2 languages I have handy, F# behaves just like C#, while Ruby gives me the result I expected.
A more comprehensive list can be found at Wikipedia.
This came up while trying to learn some WPF. I have a main window and a bunch of child windows that I want to cycle through with ctrl-tab. When I went to add cycling backwards with ctrl-shift-tab I was rewarded with a nice ArgumentOutOfRangeException.
Saturday, September 13, 2008
Subscribe to:
Post Comments (Atom)
3 comments:
Interesting. I didn't know that before either.
Good words.
while i am practicing some programs on arithmetic operators , my sir raised what is the answer of -7%-3 , 7%-3 and -7%3, so given some answer to him then sir looked at me and said " you just give me the answer tomorrow and just think for a while why i said so". So i came to know by seeing this web page.
Thanks a lot.
Post a Comment