GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
xlog.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 log(x)
9 log(x,b)
10 
11  first form computes the natural log of x = ln(x)
12  second form computes log of x base b = ln(x)/ln(b)
13 
14  if x is non-positive, or floating point exception occurs while
15  computing ln(x), the result is NULL
16 
17  if b is non-positive, or 1.0, or floating point exception occurs while
18  computing ln(b), the result is NULL
19 **********************************************************************/
20 
21 int f_log(int argc, const int *argt, void **args)
22 {
23  DCELL *res = args[0];
24  DCELL *arg1 = args[1];
25  DCELL *arg2 = (argc >= 2) ? args[2] : (DCELL *)0;
26  int i;
27 
28  if (argc < 1)
29  return E_ARG_LO;
30 
31  if (argc > 2)
32  return E_ARG_HI;
33 
34  if (argt[0] != DCELL_TYPE)
35  return E_RES_TYPE;
36 
37  if (argt[1] != DCELL_TYPE)
38  return E_ARG_TYPE;
39 
40  if (argc > 1 && argt[2] != DCELL_TYPE)
41  return E_ARG_TYPE;
42 
43  for (i = 0; i < columns; i++)
44  if (IS_NULL_D(&arg1[i]) || (arg1[i] <= 0.0))
45  SET_NULL_D(&res[i]);
46  else if (argc > 1 && (IS_NULL_D(&arg2[i]) || (arg2[i] <= 0.0)))
47  SET_NULL_D(&res[i]);
48  else {
50  res[i] = (argc > 1) ? log(arg1[i]) / log(arg2[i]) : log(arg1[i]);
52  SET_NULL_D(&res[i]);
53  }
54 
55  return 0;
56 }
@ E_RES_TYPE
Definition: calc.h:14
@ E_ARG_TYPE
Definition: calc.h:13
@ E_ARG_HI
Definition: calc.h:12
@ E_ARG_LO
Definition: calc.h:11
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 IS_NULL_D(x)
Definition: calc.h:28
double DCELL
Definition: gis.h:629
#define DCELL_TYPE
Definition: raster.h:13
int f_log(int argc, const int *argt, void **args)
Definition: xlog.c:21