Why is modulus different in different programming languages?

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

Perl

print 2 % -18;

-->

-16

Tcl

puts [expr {2 % -18}]

-->



        
4条回答
  •  悲哀的现实
    2020-12-17 09:28

    After dividing a number and a divisor, one of which is negative, you have at least two ways to separate them into a quotient and a remainder, such that quotient * divisor + remainder = number: you can either round the quotient towards negative infinity, or towards zero.

    Many languages just choose one.

    I can't resist pointing out that Common Lisp offers both.

提交回复
热议问题