GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-8dced26735
xabs.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 abs(x)
9 
10  absolute value. if x is negative returns -x
11 **********************************************************************/
12 
13 int f_abs(int argc, const int *argt, void **args)
14 {
15  int i;
16 
17  if (argc < 1)
18  return E_ARG_LO;
19  if (argc > 1)
20  return E_ARG_HI;
21 
22  if (argt[0] != argt[1])
23  return E_RES_TYPE;
24 
25  switch (argt[1]) {
26  case CELL_TYPE: {
27  CELL *res = args[0];
28  CELL *arg1 = args[1];
29 
30  for (i = 0; i < columns; i++)
31  if (IS_NULL_C(&arg1[i]))
32  SET_NULL_C(&res[i]);
33  else
34  res[i] = arg1[i] < 0 ? -arg1[i] : arg1[i];
35  return 0;
36  }
37  case FCELL_TYPE: {
38  FCELL *res = args[0];
39  FCELL *arg1 = args[1];
40 
41  for (i = 0; i < columns; i++)
42  if (IS_NULL_F(&arg1[i]))
43  SET_NULL_F(&res[i]);
44  else
45  res[i] = (FCELL)fabs(arg1[i]);
46  return 0;
47  }
48  case DCELL_TYPE: {
49  DCELL *res = args[0];
50  DCELL *arg1 = args[1];
51 
52  for (i = 0; i < columns; i++)
53  if (IS_NULL_D(&arg1[i]))
54  SET_NULL_D(&res[i]);
55  else
56  res[i] = fabs(arg1[i]);
57  return 0;
58  }
59  default:
60  return E_INV_TYPE;
61  }
62 }
@ E_INV_TYPE
Definition: calc.h:15
@ E_RES_TYPE
Definition: calc.h:14
@ E_ARG_HI
Definition: calc.h:12
@ E_ARG_LO
Definition: calc.h:11
#define IS_NULL_C(x)
Definition: calc.h:26
#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_abs(int argc, const int *argt, void **args)
Definition: xabs.c:13