xdiv.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. div(a,b) = a / b
  8. ****************************************************************/
  9. int f_div(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. arg2[i] == 0)
  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. arg2[i] == 0.0f)
  41. SET_NULL_F(&res[i]);
  42. else {
  43. floating_point_exception = 0;
  44. res[i] = arg1[i] / arg2[i];
  45. if (floating_point_exception)
  46. SET_NULL_F(&res[i]);
  47. }
  48. }
  49. return 0;
  50. }
  51. case DCELL_TYPE:
  52. {
  53. DCELL *res = args[0];
  54. DCELL *arg1 = args[1];
  55. DCELL *arg2 = args[2];
  56. for (i = 0; i < columns; i++) {
  57. if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]) ||
  58. arg2[i] == 0.0)
  59. SET_NULL_D(&res[i]);
  60. else {
  61. floating_point_exception = 0;
  62. res[i] = arg1[i] / arg2[i];
  63. if (floating_point_exception)
  64. SET_NULL_D(&res[i]);
  65. }
  66. }
  67. return 0;
  68. }
  69. default:
  70. return E_INV_TYPE;
  71. }
  72. }