xmod.c 1.7 KB

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