xmod.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/calc.h>
  5. /****************************************************************
  6. mod(a,b) = a % b
  7. ****************************************************************/
  8. int f_mod(int argc, const int *argt, void **args)
  9. {
  10. int i;
  11. if (argc < 2)
  12. return E_ARG_LO;
  13. if (argc > 2)
  14. return E_ARG_HI;
  15. if (argt[1] != argt[0] || argt[2] != argt[0])
  16. return E_ARG_TYPE;
  17. switch (argt[0]) {
  18. case CELL_TYPE:
  19. {
  20. CELL *res = args[0];
  21. CELL *arg1 = args[1];
  22. CELL *arg2 = args[2];
  23. for (i = 0; i < columns; i++) {
  24. if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]))
  25. SET_NULL_C(&res[i]);
  26. else
  27. res[i] = arg1[i] % arg2[i];
  28. }
  29. return 0;
  30. }
  31. case FCELL_TYPE:
  32. {
  33. FCELL *res = args[0];
  34. FCELL *arg1 = args[1];
  35. FCELL *arg2 = args[2];
  36. for (i = 0; i < columns; i++) {
  37. if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
  38. SET_NULL_F(&res[i]);
  39. else {
  40. floating_point_exception = 0;
  41. res[i] = (FCELL) fmod(arg1[i], arg2[i]);
  42. if (floating_point_exception)
  43. SET_NULL_F(&res[i]);
  44. }
  45. }
  46. return 0;
  47. }
  48. case DCELL_TYPE:
  49. {
  50. DCELL *res = args[0];
  51. DCELL *arg1 = args[1];
  52. DCELL *arg2 = args[2];
  53. for (i = 0; i < columns; i++) {
  54. if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
  55. SET_NULL_D(&res[i]);
  56. else {
  57. floating_point_exception = 0;
  58. res[i] = (DCELL) fmod(arg1[i], arg2[i]);
  59. if (floating_point_exception)
  60. SET_NULL_D(&res[i]);
  61. }
  62. }
  63. return 0;
  64. }
  65. default:
  66. return E_INV_TYPE;
  67. }
  68. }