1 /*****
2  * mod.h
3  * Andy Hammerlindl 2005/03/16
4  *
5  * Definition of implementation-independent mod function.
6  *****/
7 
8 #ifndef MOD_H
9 #define MOD_H
10 
11 #include <cmath>
12 
13 #include "common.h"
14 
15 using std::fmod;
16 
Mod(Int x,Int y)17 inline Int Mod(Int x, Int y) {return x % y;}
Mod(double x,double y)18 inline double Mod(double x, double y) {return fmod(x,y);}
19 
20 template<typename T>
portableMod(T x,T y)21 inline T portableMod(T x,T y)
22 {
23 // Implementation-independent definition of mod; ensure that result has
24 // same sign as divisor
25   T val=Mod(x,y);
26   if((y > 0 && val < 0) || (y < 0 && val > 0)) val += y;
27   return val;
28 }
29 
imod(Int x,Int y)30 inline Int imod(Int x, Int y)
31 {
32   return portableMod<Int>(x,y);
33 }
34 
imod(Int i,size_t n)35 inline Int imod(Int i, size_t n) {
36   i %= (Int) n;
37   if(i < 0) i += (Int) n;
38   return i;
39 }
40 
41 #endif
42