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