GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
xpow.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 pow(a,b)
9  a raised to the power b
10 ****************************************************************/
11 
12 static int ipow(int x, int y)
13 {
14  int res = 1;
15 
16  while (y) {
17  if (y & 1)
18  res *= x;
19  y >>= 1;
20  x *= x;
21  }
22  return res;
23 }
24 
25 int f_pow(int argc, const int *argt, void **args)
26 {
27  int i;
28 
29  if (argc < 2)
30  return E_ARG_LO;
31  if (argc > 2)
32  return E_ARG_HI;
33 
34  if (argt[1] != argt[0] || argt[2] != argt[0])
35  return E_ARG_TYPE;
36 
37  switch (argt[0]) {
38  case CELL_TYPE: {
39  CELL *res = args[0];
40  CELL *arg1 = args[1];
41  CELL *arg2 = args[2];
42 
43  for (i = 0; i < columns; i++) {
44  if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]) || arg2[i] < 0)
45  SET_NULL_C(&res[i]);
46  else
47  res[i] = ipow(arg1[i], arg2[i]);
48  }
49  return 0;
50  }
51  case FCELL_TYPE: {
52  FCELL *res = args[0];
53  FCELL *arg1 = args[1];
54  FCELL *arg2 = args[2];
55 
56  for (i = 0; i < columns; i++) {
57  if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
58  SET_NULL_F(&res[i]);
59  else if (arg1[i] < 0 && arg2[i] != ceil(arg2[i]))
60  SET_NULL_F(&res[i]);
61  else {
63  res[i] = pow(arg1[i], arg2[i]);
65  SET_NULL_F(&res[i]);
66  }
67  }
68  return 0;
69  }
70  case DCELL_TYPE: {
71  DCELL *res = args[0];
72  DCELL *arg1 = args[1];
73  DCELL *arg2 = args[2];
74 
75  for (i = 0; i < columns; i++) {
76  if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
77  SET_NULL_D(&res[i]);
78  else if (arg1[i] < 0 && arg2[i] != ceil(arg2[i]))
79  SET_NULL_D(&res[i]);
80  else {
82  res[i] = pow(arg1[i], arg2[i]);
84  SET_NULL_D(&res[i]);
85  }
86  }
87  return 0;
88  }
89  default:
90  return E_INV_TYPE;
91  }
92 }
@ 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
#define x
int f_pow(int argc, const int *argt, void **args)
Definition: xpow.c:25