xround.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <limits.h>
  2. #include <math.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include "globals.h"
  6. #include "expression.h"
  7. #include "func_proto.h"
  8. /**********************************************************************
  9. round(x, step, start)
  10. rounds x to nearest value in the sequence
  11. y[i] = i * step + start
  12. **********************************************************************/
  13. /* i_round(x) rounds x to nearest value, handles negative correctly */
  14. static double i_round(double x, double step, double start)
  15. {
  16. x -= start;
  17. x /= step;
  18. x = floor(x + 0.5);
  19. x *= step;
  20. x += start;
  21. return x;
  22. }
  23. /**********************************************************************/
  24. int f_round(int argc, const int *argt, void **args)
  25. {
  26. const DCELL *arg1 = args[1];
  27. int i;
  28. if (argc < 1)
  29. return E_ARG_LO;
  30. if (argc > 3)
  31. return E_ARG_HI;
  32. if (argc == 1 && argt[0] != CELL_TYPE)
  33. return E_RES_TYPE;
  34. if (argt[1] != DCELL_TYPE)
  35. return E_ARG_TYPE;
  36. if (argc > 1 && argt[2] != DCELL_TYPE)
  37. return E_ARG_TYPE;
  38. if (argc > 2 && argt[3] != DCELL_TYPE)
  39. return E_ARG_TYPE;
  40. if (argc == 1) {
  41. CELL *res = args[0];
  42. for (i = 0; i < columns; i++) {
  43. if (IS_NULL_D(&arg1[i]))
  44. SET_NULL_C(&res[i]);
  45. else {
  46. DCELL x = i_round(arg1[i], 1.0, 0.0);
  47. if (x > 2147483647.0 || x < -2147483647.0)
  48. SET_NULL_C(&res[i]);
  49. else
  50. res[i] = (CELL) x;
  51. }
  52. }
  53. return 0;
  54. }
  55. else if (argc == 2) {
  56. const DCELL *arg2 = args[2];
  57. switch (argt[0]) {
  58. case CELL_TYPE:
  59. {
  60. CELL *res = args[0];
  61. for (i = 0; i < columns; i++) {
  62. if (IS_NULL_D(&arg1[i]))
  63. SET_NULL_C(&res[i]);
  64. else if (IS_NULL_D(&arg2[i]))
  65. SET_NULL_C(&res[i]);
  66. else {
  67. DCELL x = i_round(arg1[i], arg2[i], 0.0);
  68. if (x > 2147483647.0 || x < -2147483647.0)
  69. SET_NULL_C(&res[i]);
  70. else
  71. res[i] = (CELL) x;
  72. }
  73. }
  74. return 0;
  75. }
  76. case FCELL_TYPE:
  77. {
  78. FCELL *res = args[0];
  79. for (i = 0; i < columns; i++)
  80. if (IS_NULL_D(&arg1[i]))
  81. SET_NULL_F(&res[i]);
  82. else if (IS_NULL_D(&arg2[i]))
  83. SET_NULL_F(&res[i]);
  84. else
  85. res[i] = (FCELL) i_round(arg1[i], arg2[i], 0.0);
  86. return 0;
  87. }
  88. case DCELL_TYPE:
  89. {
  90. DCELL *res = args[0];
  91. for (i = 0; i < columns; i++)
  92. if (IS_NULL_D(&arg1[i]))
  93. SET_NULL_D(&res[i]);
  94. else if (IS_NULL_D(&arg2[i]))
  95. SET_NULL_D(&res[i]);
  96. else
  97. res[i] = (DCELL) i_round(arg1[i], arg2[i], 0.0);
  98. return 0;
  99. }
  100. default:
  101. return E_INV_TYPE;
  102. }
  103. }
  104. else if (argc == 3) {
  105. const DCELL *arg2 = args[2];
  106. const DCELL *arg3 = args[3];
  107. switch (argt[0]) {
  108. case CELL_TYPE:
  109. {
  110. CELL *res = args[0];
  111. for (i = 0; i < columns; i++) {
  112. if (IS_NULL_D(&arg1[i]))
  113. SET_NULL_C(&res[i]);
  114. else if (IS_NULL_D(&arg2[i]))
  115. SET_NULL_C(&res[i]);
  116. else if (IS_NULL_D(&arg3[i]))
  117. SET_NULL_C(&res[i]);
  118. else {
  119. DCELL x = i_round(arg1[i], arg2[i], arg3[i]);
  120. if (x > 2147483647.0 || x < -2147483647.0)
  121. SET_NULL_C(&res[i]);
  122. else
  123. res[i] = (CELL) x;
  124. }
  125. }
  126. return 0;
  127. }
  128. case FCELL_TYPE:
  129. {
  130. FCELL *res = args[0];
  131. for (i = 0; i < columns; i++)
  132. if (IS_NULL_D(&arg1[i]))
  133. SET_NULL_F(&res[i]);
  134. else if (IS_NULL_D(&arg2[i]))
  135. SET_NULL_F(&res[i]);
  136. else if (IS_NULL_D(&arg3[i]))
  137. SET_NULL_F(&res[i]);
  138. else
  139. res[i] = (FCELL) i_round(arg1[i], arg2[i], arg3[i]);
  140. return 0;
  141. }
  142. case DCELL_TYPE:
  143. {
  144. DCELL *res = args[0];
  145. for (i = 0; i < columns; i++)
  146. if (IS_NULL_D(&arg1[i]))
  147. SET_NULL_D(&res[i]);
  148. else if (IS_NULL_D(&arg2[i]))
  149. SET_NULL_D(&res[i]);
  150. else if (IS_NULL_D(&arg3[i]))
  151. SET_NULL_D(&res[i]);
  152. else
  153. res[i] = (DCELL) i_round(arg1[i], arg2[i], arg3[i]);
  154. return 0;
  155. }
  156. default:
  157. return E_INV_TYPE;
  158. }
  159. }
  160. else
  161. return E_WTF;
  162. }
  163. int c_round(int argc, int *argt)
  164. {
  165. if (argc < 1)
  166. return E_ARG_LO;
  167. if (argc > 3)
  168. return E_ARG_HI;
  169. argt[0] = CELL_TYPE;
  170. if (argc > 1 && argt[0] < argt[2])
  171. argt[0] = argt[2];
  172. if (argc > 2 && argt[0] < argt[3])
  173. argt[0] = argt[3];
  174. argt[1] = DCELL_TYPE;
  175. if (argc > 1)
  176. argt[2] = DCELL_TYPE;
  177. if (argc > 2)
  178. argt[3] = DCELL_TYPE;
  179. return 0;
  180. }