xlog.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include "globals.h"
  5. #include "expression.h"
  6. #include "func_proto.h"
  7. /**********************************************************************
  8. log(x)
  9. log(x,b)
  10. first form computes the natural log of x = ln(x)
  11. second form computes log of x base b = ln(x)/ln(b)
  12. if x is non-positive, or floating point exception occurs while
  13. computing ln(x), the result is NULL
  14. if b is non-positive, or 1.0, or floating point exception occurs while
  15. computing ln(b), the result is NULL
  16. **********************************************************************/
  17. int f_log(int argc, const int *argt, void **args)
  18. {
  19. DCELL *res = args[0];
  20. DCELL *arg1 = args[1];
  21. DCELL *arg2 = (argc >= 2) ? args[2] : (DCELL *) 0;
  22. int i;
  23. if (argc < 1)
  24. return E_ARG_LO;
  25. if (argc > 2)
  26. return E_ARG_HI;
  27. if (argt[0] != DCELL_TYPE)
  28. return E_RES_TYPE;
  29. if (argt[1] != DCELL_TYPE)
  30. return E_ARG_TYPE;
  31. if (argc > 1 && argt[2] != DCELL_TYPE)
  32. return E_ARG_TYPE;
  33. for (i = 0; i < columns; i++)
  34. if (IS_NULL_D(&arg1[i]) || (arg1[i] <= 0.0))
  35. SET_NULL_D(&res[i]);
  36. else if (argc > 1 && (IS_NULL_D(&arg2[i]) || (arg2[i] <= 0.0)))
  37. SET_NULL_D(&res[i]);
  38. else {
  39. floating_point_exception = 0;
  40. res[i] = (argc > 1)
  41. ? log(arg1[i]) / log(arg2[i])
  42. : log(arg1[i]);
  43. if (floating_point_exception)
  44. SET_NULL_D(&res[i]);
  45. }
  46. return 0;
  47. }