xexp.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/calc.h>
  5. /**********************************************************************
  6. exp(x) computes e raised to power x
  7. exp(x,y) computes x raised to power y
  8. if floating point exception occurs during the evaluation of exp(x)
  9. or exp(x,y) the result is NULL
  10. **********************************************************************/
  11. int f_exp(int argc, const int *argt, void **args)
  12. {
  13. DCELL *res = args[0];
  14. DCELL *arg1 = args[1];
  15. DCELL *arg2;
  16. int i;
  17. if (argc < 1)
  18. return E_ARG_LO;
  19. if (argc > 2)
  20. return E_ARG_HI;
  21. if (argt[0] != DCELL_TYPE)
  22. return E_RES_TYPE;
  23. if (argt[1] != DCELL_TYPE)
  24. return E_ARG_TYPE;
  25. arg2 = (argc > 1) ? args[2] : NULL;
  26. for (i = 0; i < columns; i++)
  27. if (IS_NULL_D(&arg1[i]))
  28. SET_NULL_D(&res[i]);
  29. else if (argc > 1 && IS_NULL_D(&arg2[i]))
  30. SET_NULL_D(&res[i]);
  31. else if (argc > 1 && arg1[i] < 0 && arg2[i] != ceil(arg2[i]))
  32. SET_NULL_D(&res[i]);
  33. else {
  34. floating_point_exception = 0;
  35. res[i] = (argc > 1)
  36. ? pow(arg1[i], arg2[i])
  37. : exp(arg1[i]);
  38. if (floating_point_exception)
  39. SET_NULL_D(&res[i]);
  40. }
  41. return 0;
  42. }