xsqrt.c 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. sqrt(x)
  9. if floating point exception occurs during the evaluation of sqrt(x)
  10. the result is NULL
  11. **********************************************************************/
  12. int f_sqrt(int argc, const int *argt, void **args)
  13. {
  14. DCELL *res = args[0];
  15. DCELL *arg1 = args[1];
  16. int i;
  17. if (argc < 1)
  18. return E_ARG_LO;
  19. if (argc > 1)
  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. for (i = 0; i < columns; i++)
  26. if (IS_NULL_D(&arg1[i]) || (arg1[i] < 0.0))
  27. SET_NULL_D(&res[i]);
  28. else {
  29. floating_point_exception = 0;
  30. res[i] = sqrt(arg1[i]);
  31. if (floating_point_exception)
  32. SET_NULL_D(&res[i]);
  33. }
  34. return 0;
  35. }