
Mod Function and Negative Numbers (2000) - booleandilemma
http://mathforum.org/library/drmath/view/52343.html
======
FabHK
Julia goes the ADA way, supporting both. There's

    
    
        div(x,y)   # truncated division; quotient rounded towards zero
        fld(x,y)   # floored division; quotient rounded towards -Inf
    

and correspondingly

    
    
        rem(x,y)   # remainder; satisfies x == div(x,y)*y + rem(x,y); sign matches x
        mod(x,y)   # modulus; satisfies x == fld(x,y)*y + mod(x,y); sign matches y
    

and the convenience functions

    
    
        divrem(x,y) # returns (div(x,y),rem(x,y))  ## Lotus 123 convention
        fldmod(x,y)	# returns (fld(x,y),mod(x,y))  ## Excel convention
    

(There's also mod1 and fld1, and there's mod2pi(x) which is more precise than
mod(x, 2pi).)

------
ZenPsycho
I run into this issue in javascript all the time. I want a number to basically
act like pacman leaving the left or right side of a screen. pure sawtooth
function. unfortunately js’s % operator mirrors at zero. so i always end up
having to search for this:

    
    
        Number.prototype.mod = function(n) {
          return ((this % n) + n) % n;
        }

~~~
antidesitter
Agreed. In my experience, people want to use _this_ modulo operator (e.g. for
implementing a ring buffer or wrap-around as you mentioned), but many
languages use the _other_ modulo which leads to all sorts of bugs. It doesn’t
make sense to me since this modulo is more natural from both a mathematical
and an engineering perspective.

~~~
jacobolus
It’s because that’s the way CPUs / C do it. Many other language implementors
are too lazy to fix it.

I have often been thankful that Python does the right thing, and often
frustrated that Javascript does not.

------
kccqzy
Haskell supports both. The documentation explains the two pretty clearly, IMO:

    
    
        quot :: a -> a -> a 
    
          integer division truncated toward zero
    
        rem :: a -> a -> a 
    
          integer remainder, satisfying
    
            (x `quot` y)*y + (x `rem` y) == x
    
        div :: a -> a -> a 
    
          integer division truncated toward negative infinity
    
        mod :: a -> a -> a 
    
          integer modulus, satisfying
    
           (x `div` y)*y + (x `mod` y) == x
    

Here's an example

    
    
        ghci> (-340) `divMod` 60
        (-6,20)
        ghci> (-340) `quotRem` 60
        (-5,-40)

~~~
thaumasiotes
> integer division truncated toward negative infinity

Truncated?

------
js2
See also “Mod and remainder are not the same” discussed here recently:

[https://news.ycombinator.com/item?id=17817758](https://news.ycombinator.com/item?id=17817758)

------
adamnemecek
I find true module to be an insanely useful operator. Python negative indexing
(array[-1]) is essentially that. I’ve proposed it to swift but people didn’t
see the point [https://forums.swift.org/t/double-modulo-
operator/2718](https://forums.swift.org/t/double-modulo-operator/2718)

~~~
Shoop
Python negative indexing isn't modulo. It's just shorthand for
array[len(array)-i].

    
    
      >>> a = [1, 2, 3]
      >>> a[-1]
      3
      >>> a[-2]
      2
      >>> a[-3]
      1
      >>> a[-4]
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      IndexError: list index out of range

~~~
adamnemecek
The problem is that you need to have a conditional to figure out if I is
negative or not. With modulo it’s all the same.

------
jimhefferon
It is a sad thing but there is no standard:
[https://en.wikipedia.org/wiki/Modulo_operation](https://en.wikipedia.org/wiki/Modulo_operation).

