# Modulus in QGIS Raster Calculator

I have the same question like the one aked here: How to get the “absolute value” of a raster cell in QGIS?

I came up with the same solution for the problem some time ago. However I showed this solution to a software developer some days ago, and he was very amused. Is there a build in function in QGIS to acquire the modulus of a real number?

Raster calculator does not have a built-in modulus function, but you can try:

`````` ( "ras1@1" > 0 ) * "ras1@1" +  ( "ras1@1" < 0 ) * ( -1 ) * "ras1@1"
``````
• If the cell value is negative, `( "ras1@1" > 0 )` returns `0`, while `( "ras1@1" < 0 )` returns `1`. The output cell value is `( -1 ) * "ras1@1"`.
• If the cell value is positive, `( "ras1@1" > 0 )` returns `1`, while `( "ras1@1" < 0 )` returns `0`. The output cell value is `"ras1@1"`.
• If the cell value is `0`, both `( "ras1@1" > 0 )` and `( "ras1@1" < 0 )` return `0`. The output cell value is `0`, too.

The solution is to use the geoalgorithm r.mapcalc in the GRASS section of he Processing Toolbox. The number of supported functions is by far bigger than the Raster Calculator:

``````function                description                                     type
---------------------------------------------------------------------------
abs(x)                  return absolute value of x                      *
acos(x)                 inverse cosine of x (result is in degrees)      F
asin(x)                 inverse sine of x (result is in degrees)        F
atan(x)                 inverse tangent of x (result is in degrees)     F
atan(x,y)               inverse tangent of y/x (result is in degrees)   F
cos(x)                  cosine of x (x is in degrees)                   F
double(x)               convert x to double-precision floating point    F
eval([x,y,...,]z)       evaluate values of listed expr, pass results to z
exp(x)                  exponential function of x                       F
exp(x,y)                x to the power y                                F
float(x)                convert x to single-precision floating point    F
graph(x,x1,y1[x2,y2..]) convert the x to a y based on points in a graph F
graph2(x,x1[,x2,..],y1[,y2..])
alternative form of graph()                     F
if                      decision options:                               *
if(x)                   1 if x not zero, 0 otherwise
if(x,a)                 a if x not zero, 0 otherwise
if(x,a,b)               a if x not zero, b otherwise
if(x,a,b,c)             a if x > 0, b if x is zero, c if x < 0
int(x)                  convert x to integer [ truncates ]              I
isnull(x)               check if x = NULL
log(x)                  natural log of x                                F
log(x,b)                log of x base b                                 F
max(x,y[,z...])         largest value of those listed                   *
median(x,y[,z...])      median value of those listed                    *
min(x,y[,z...])         smallest value of those listed                  *
mode(x,y[,z...])        mode value of those listed                      *
nmax(x,y[,z...])        largest value of those listed, excluding NULLs  *
nmedian(x,y[,z...])     median value of those listed, excluding NULLs   *
nmin(x,y[,z...])        smallest value of those listed, excluding NULLs *
nmode(x,y[,z...])       mode value of those listed, excluding NULLs     *
not(x)                  1 if x is zero, 0 otherwise
pow(x,y)                x to the power y                                *
rand(a,b)               random value x : a <= x < b                     *
round(x)                round x to nearest integer                      I
round(x,y)              round x to nearest multiple of y
round(x,y,z)            round x to nearest y*i+z for some integer i
sin(x)                  sine of x (x is in degrees)                     F
sqrt(x)                 square root of x                                F
tan(x)                  tangent of x (x is in degrees)                  F
xor(x,y)                exclusive-or (XOR) of x and y                   I

Internal variables:
row()                  current row of moving window                    I
col()                  current col of moving window                    I
nrows()                number of rows in computation region            I
ncols()                number of columns in computation region         I
x()                    current x-coordinate of moving window           F
y()                    current y-coordinate of moving window           F
ewres()                current east-west resolution                    F
nsres()                current north-south resolution                  F
area()                 area of current cell in square meters           F
null()                 NULL value
`````` 