xabs.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/calc.h>
  5. /**********************************************************************
  6. abs(x)
  7. absolute value. if x is negative returns -x
  8. **********************************************************************/
  9. int f_abs(int argc, const int *argt, void **args)
  10. {
  11. int i;
  12. if (argc < 1)
  13. return E_ARG_LO;
  14. if (argc > 1)
  15. return E_ARG_HI;
  16. if (argt[0] != argt[1])
  17. return E_RES_TYPE;
  18. switch (argt[1]) {
  19. case CELL_TYPE:
  20. {
  21. CELL *res = args[0];
  22. CELL *arg1 = args[1];
  23. for (i = 0; i < columns; i++)
  24. if (IS_NULL_C(&arg1[i]))
  25. SET_NULL_C(&res[i]);
  26. else
  27. res[i] = arg1[i] < 0 ? -arg1[i]
  28. : arg1[i];
  29. return 0;
  30. }
  31. case FCELL_TYPE:
  32. {
  33. FCELL *res = args[0];
  34. FCELL *arg1 = args[1];
  35. for (i = 0; i < columns; i++)
  36. if (IS_NULL_F(&arg1[i]))
  37. SET_NULL_F(&res[i]);
  38. else
  39. res[i] = (FCELL) fabs(arg1[i]);
  40. return 0;
  41. }
  42. case DCELL_TYPE:
  43. {
  44. DCELL *res = args[0];
  45. DCELL *arg1 = args[1];
  46. for (i = 0; i < columns; i++)
  47. if (IS_NULL_D(&arg1[i]))
  48. SET_NULL_D(&res[i]);
  49. else
  50. res[i] = fabs(arg1[i]);
  51. return 0;
  52. }
  53. default:
  54. return E_INV_TYPE;
  55. }
  56. }