Monday, December 26, 2011

Fractional Modulo Operation in Haskell

Today I ran into a situation where I wanted to use the mod function on some numbers. Numbers that had decimal points. Haskell had things to say about it... something along the lines of:


 ghci>6 `mod` 2.2
 Ambiguous type variable `a0' in the constraints:  
    (Num a0) arising from the literal `6' at <interactive>:1:1  
    (Integral a0) arising from a use of `mod' at <interactive>:1:3-7  
    (Fractional a0) arising from the literal `2.2'  
            at <interactive>:1:9-11  
   Probable fix: add a type signature that fixes these type variable(s)  
   In the first argument of `mod', namely `6'  
   In the expression: 6 `mod` 2.2  
   In an equation for `it': it = 6 `mod` 2.2  


Well, that's problematic. I shouldn't be *too* surprised... C++ and PHP apparently don't support doing fractional modulo either. But I want it to work in Haskell, so, I'm going to make it work. This is also a nice introduction to defining custom infix operators!

Modulo Concepts
As it turns out, there's more than one way that modulo can be defined.  Basically, the modulus is the remainder of the dividend divided by the divisor. In people terms, it means "what is left over when you try to divide X (dividend) by Y (divisor)". The difference between a modulo operation and remainder is in the definition of what happens when you have a negative dividend or divisor. And that is also where you come up with different flavors of modulo operation. What happens depends entirely on the programming language. Some languages say that the modulus always has the same sign as the dividend. Others say it always has the same sign as the divisor.

Implementation
Since we're implementing our custom modulo operation in Haskell, it's probably best to stick with what Haskell does with negative divisors/dividends in regular Integral modulo operation. Lets see what Haskell does with various combinations of argument sign:


 ghci>6 `mod` 5
  1
 ghci>6 `mod` (-5)
  -4
 ghci>(-6) `mod` (-5)
  -1
 ghci>(-6) `mod` 5
  4
  

So now we've got our standard. It turns out, Haskell uses floored division to do perform modulo operations. Floored division works like this :

quotient = floor (dividend/divisor)
remainder = dividend - divisor*quotient


When we expand that, we get a simple equation to use for our custom modulo:

remainder = dividend - divisor * ( floor (dividend/divisor) )

In Haskell:


 (%) :: (RealFrac a) => a -> a -> a  
 dividend % 0 = error "Division by Zero!"   
 dividend % divisor = dividend - divisor * (fromIntegral $ floor (dividend/divisor))  


There are a few things to note about this code:
  • In the % operator's type definition, the % is enclosed in parentheses. This is necessary for the compiler to make sense of a random symbol (or symbols) at the beginning of a line.
  • The operator's actual implementation looks weird... we're placing arguments BEFORE the function name ?! Many new Haskellers (including me, until I started researching this) don't know that you can define functions implicitly as infix functions. If you wanted to use a named function instead of a symbol/operator, you can define it as infix by putting it inside backticks (`):
    • x `myInfixFunc` y = do something or other
So lets do a sanity check and make sure we get good results:

 ghci>6 % 5  
  1.0  
 ghci>6 % (-5)  
  -4.0  
 ghci>(-6) % (-5)  
  -1.0  
 ghci>(-6) % 5  
  4.0
  

Well that looks good! Now lets try it on some actual fractional values:


 ghci>6 % 2.2  
  1.5999999999999996
  

If you want to make sure that this is right, you can try Googling "6 % 2.2". Because of rounding differences, you'll get "1.6", rather than "1.5(lots of 9's)". Awesome! We just made Haskell perform a modulo operation on fractional numbers!

No comments:

Post a Comment