xdiv.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/calc.h>
  4. /****************************************************************
  5. div(a,b) = a / b
  6. ****************************************************************/
  7. int f_div(int argc, const int *argt, void **args)
  8. {
  9. int i;
  10. if (argc < 2)
  11. return E_ARG_LO;
  12. if (argc > 2)
  13. return E_ARG_HI;
  14. if (argt[1] != argt[0] || argt[2] != argt[0])
  15. return E_ARG_TYPE;
  16. switch (argt[0]) {
  17. case CELL_TYPE:
  18. {
  19. CELL *res = args[0];
  20. CELL *arg1 = args[1];
  21. CELL *arg2 = args[2];
  22. for (i = 0; i < columns; i++) {
  23. if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]) ||
  24. arg2[i] == 0)
  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. arg2[i] == 0.0f)
  39. SET_NULL_F(&res[i]);
  40. else {
  41. floating_point_exception = 0;
  42. res[i] = arg1[i] / arg2[i];
  43. if (floating_point_exception)
  44. SET_NULL_F(&res[i]);
  45. }
  46. }
  47. return 0;
  48. }
  49. case DCELL_TYPE:
  50. {
  51. DCELL *res = args[0];
  52. DCELL *arg1 = args[1];
  53. DCELL *arg2 = args[2];
  54. for (i = 0; i < columns; i++) {
  55. if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]) ||
  56. arg2[i] == 0.0)
  57. SET_NULL_D(&res[i]);
  58. else {
  59. floating_point_exception = 0;
  60. res[i] = 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. }