Why is modulus different in different programming languages?

前端 未结 4 1556
失恋的感觉
失恋的感觉 2020-12-17 09:24

Perl

print 2 % -18;

-->

-16

Tcl

puts [expr {2 % -18}]

-->



        
4条回答
  •  一个人的身影
    2020-12-17 09:26

    Wikipedia's "Modulo operation" page explains it quite well. I won't try to do any better here, as I'm likely to make a subtle but important mistake.

    The rub of it is that you can define "remainder" or "modulus" in different ways, and different languages have chosen different options to implement.

提交回复
热议问题