xpow.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. pow(a,b)
  9. a raised to the power b
  10. ****************************************************************/
  11. static int ipow(int x, int y)
  12. {
  13. int res = 1;
  14. while (y) {
  15. if (y & 1)
  16. res *= x;
  17. y >>= 1;
  18. x *= x;
  19. }
  20. return res;
  21. }
  22. int f_pow(int argc, const int *argt, void **args)
  23. {
  24. int i;
  25. if (argc < 2)
  26. return E_ARG_LO;
  27. if (argc > 2)
  28. return E_ARG_HI;
  29. if (argt[1] != argt[0] || argt[2] != argt[0])
  30. return E_ARG_TYPE;
  31. switch (argt[0]) {
  32. case CELL_TYPE:
  33. {
  34. CELL *res = args[0];
  35. CELL *arg1 = args[1];
  36. CELL *arg2 = args[2];
  37. for (i = 0; i < columns; i++) {
  38. if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]) || arg2[i] < 0)
  39. SET_NULL_C(&res[i]);
  40. else
  41. res[i] = ipow(arg1[i], arg2[i]);
  42. }
  43. return 0;
  44. }
  45. case FCELL_TYPE:
  46. {
  47. FCELL *res = args[0];
  48. FCELL *arg1 = args[1];
  49. FCELL *arg2 = args[2];
  50. for (i = 0; i < columns; i++) {
  51. if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
  52. SET_NULL_F(&res[i]);
  53. else if (arg1[i] < 0 && arg2[i] != ceil(arg2[i]))
  54. SET_NULL_F(&res[i]);
  55. else {
  56. floating_point_exception = 0;
  57. res[i] = pow(arg1[i], arg2[i]);
  58. if (floating_point_exception)
  59. SET_NULL_F(&res[i]);
  60. }
  61. }
  62. return 0;
  63. }
  64. case DCELL_TYPE:
  65. {
  66. DCELL *res = args[0];
  67. DCELL *arg1 = args[1];
  68. DCELL *arg2 = args[2];
  69. for (i = 0; i < columns; i++) {
  70. if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
  71. SET_NULL_D(&res[i]);
  72. else if (arg1[i] < 0 && arg2[i] != ceil(arg2[i]))
  73. SET_NULL_D(&res[i]);
  74. else {
  75. floating_point_exception = 0;
  76. res[i] = pow(arg1[i], arg2[i]);
  77. if (floating_point_exception)
  78. SET_NULL_D(&res[i]);
  79. }
  80. }
  81. return 0;
  82. }
  83. default:
  84. return E_INV_TYPE;
  85. }
  86. }