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