GRASS 8 Programmer's Manual
8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
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
{
62
floating_point_exception
= 0;
63
res[i] =
pow
(
arg1
[i],
arg2
[i]);
64
if
(
floating_point_exception
)
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
{
81
floating_point_exception
= 0;
82
res[i] =
pow
(
arg1
[i],
arg2
[i]);
83
if
(
floating_point_exception
)
84
SET_NULL_D
(&res[i]);
85
}
86
}
87
return
0;
88
}
89
default
:
90
return
E_INV_TYPE
;
91
}
92
}
calc.h
E_INV_TYPE
@ E_INV_TYPE
Definition
calc.h:15
E_ARG_TYPE
@ E_ARG_TYPE
Definition
calc.h:13
E_ARG_HI
@ E_ARG_HI
Definition
calc.h:12
E_ARG_LO
@ E_ARG_LO
Definition
calc.h:11
IS_NULL_C
#define IS_NULL_C(x)
Definition
calc.h:26
floating_point_exception
volatile int floating_point_exception
Definition
calc.c:8
SET_NULL_D
#define SET_NULL_D(x)
Definition
calc.h:32
columns
int columns
Definition
calc.c:11
SET_NULL_C
#define SET_NULL_C(x)
Definition
calc.h:30
IS_NULL_F
#define IS_NULL_F(x)
Definition
calc.h:27
IS_NULL_D
#define IS_NULL_D(x)
Definition
calc.h:28
SET_NULL_F
#define SET_NULL_F(x)
Definition
calc.h:31
AMI_STREAM
Definition
ami_stream.h:153
f_pow
func_t f_pow
gis.h
FCELL
float FCELL
Definition
gis.h:636
DCELL
double DCELL
Definition
gis.h:635
CELL
int CELL
Definition
gis.h:634
raster.h
FCELL_TYPE
#define FCELL_TYPE
Definition
raster.h:12
DCELL_TYPE
#define DCELL_TYPE
Definition
raster.h:13
CELL_TYPE
#define CELL_TYPE
Definition
raster.h:11
x
#define x
lib
calc
xpow.c
Generated on Sun Apr 5 2026 06:59:54 for GRASS 8 Programmer's Manual by
1.9.8