xexp.c 1.2 KB

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