xlog.c 1.4 KB

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