GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
xmod.c
Go to the documentation of this file.
1 
2 #include <math.h>
3 
4 #include <grass/gis.h>
5 #include <grass/raster.h>
6 #include <grass/calc.h>
7 
8 /****************************************************************
9 mod(a,b) = a % b
10 ****************************************************************/
11 
12 int f_mod(int argc, const int *argt, void **args)
13 {
14  int i;
15 
16  if (argc < 2)
17  return E_ARG_LO;
18  if (argc > 2)
19  return E_ARG_HI;
20 
21  if (argt[1] != argt[0] || argt[2] != argt[0])
22  return E_ARG_TYPE;
23 
24  switch (argt[0]) {
25  case CELL_TYPE:
26  {
27  CELL *res = args[0];
28  CELL *arg1 = args[1];
29  CELL *arg2 = args[2];
30 
31  for (i = 0; i < columns; i++) {
32  if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]))
33  SET_NULL_C(&res[i]);
34  else
35  res[i] = arg1[i] % arg2[i];
36  }
37  return 0;
38  }
39  case FCELL_TYPE:
40  {
41  FCELL *res = args[0];
42  FCELL *arg1 = args[1];
43  FCELL *arg2 = args[2];
44 
45  for (i = 0; i < columns; i++) {
46  if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
47  SET_NULL_F(&res[i]);
48  else {
50  res[i] = (FCELL) fmod(arg1[i], arg2[i]);
52  SET_NULL_F(&res[i]);
53  }
54  }
55  return 0;
56  }
57  case DCELL_TYPE:
58  {
59  DCELL *res = args[0];
60  DCELL *arg1 = args[1];
61  DCELL *arg2 = args[2];
62 
63  for (i = 0; i < columns; i++) {
64  if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
65  SET_NULL_D(&res[i]);
66  else {
68  res[i] = (DCELL) fmod(arg1[i], arg2[i]);
70  SET_NULL_D(&res[i]);
71  }
72  }
73  return 0;
74  }
75  default:
76  return E_INV_TYPE;
77  }
78 }
#define CELL_TYPE
Definition: raster.h:11
#define SET_NULL_C(x)
Definition: calc.h:32
double DCELL
Definition: gis.h:614
#define IS_NULL_F(x)
Definition: calc.h:29
#define IS_NULL_C(x)
Definition: calc.h:28
Definition: calc.h:13
int columns
Definition: calc.c:12
volatile int floating_point_exception
Definition: calc.c:9
#define DCELL_TYPE
Definition: raster.h:13
Definition: calc.h:12
int f_mod(int argc, const int *argt, void **args)
Definition: xmod.c:12
float FCELL
Definition: gis.h:615
#define IS_NULL_D(x)
Definition: calc.h:30
int CELL
Definition: gis.h:613
#define FCELL_TYPE
Definition: raster.h:12
#define SET_NULL_F(x)
Definition: calc.h:33
#define SET_NULL_D(x)
Definition: calc.h:34