GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72103
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xlog.c
Go to the documentation of this file.
1 
2 #include <math.h>
3 
4 #include <grass/gis.h>
5 #include <grass/raster.h>
6 #include <grass/calc.h>
7 
8 /**********************************************************************
9 log(x)
10 log(x,b)
11 
12  first form computes the natural log of x = ln(x)
13  second form computes log of x base b = ln(x)/ln(b)
14 
15  if x is non-positive, or floating point exception occurs while
16  computing ln(x), the result is NULL
17 
18  if b is non-positive, or 1.0, or floating point exception occurs while
19  computing ln(b), the result is NULL
20 **********************************************************************/
21 
22 int f_log(int argc, const int *argt, void **args)
23 {
24  DCELL *res = args[0];
25  DCELL *arg1 = args[1];
26  DCELL *arg2 = (argc >= 2) ? args[2] : (DCELL *) 0;
27  int i;
28 
29  if (argc < 1)
30  return E_ARG_LO;
31 
32  if (argc > 2)
33  return E_ARG_HI;
34 
35  if (argt[0] != DCELL_TYPE)
36  return E_RES_TYPE;
37 
38  if (argt[1] != DCELL_TYPE)
39  return E_ARG_TYPE;
40 
41  if (argc > 1 && argt[2] != DCELL_TYPE)
42  return E_ARG_TYPE;
43 
44  for (i = 0; i < columns; i++)
45  if (IS_NULL_D(&arg1[i]) || (arg1[i] <= 0.0))
46  SET_NULL_D(&res[i]);
47  else if (argc > 1 && (IS_NULL_D(&arg2[i]) || (arg2[i] <= 0.0)))
48  SET_NULL_D(&res[i]);
49  else {
51  res[i] = (argc > 1)
52  ? log(arg1[i]) / log(arg2[i])
53  : log(arg1[i]);
55  SET_NULL_D(&res[i]);
56  }
57 
58  return 0;
59 }
double DCELL
Definition: gis.h:581
Definition: calc.h:13
int columns
Definition: calc.c:12
volatile int floating_point_exception
Definition: calc.c:9
#define DCELL_TYPE
Definition: raster.h:13
Definition: calc.h:12
#define IS_NULL_D(x)
Definition: calc.h:30
#define SET_NULL_D(x)
Definition: calc.h:34
int f_log(int argc, const int *argt, void **args)
Definition: xlog.c:22