xmod.c 1.6 KB

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