mod.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "stdafx.h"
  2. #include "defs.h"
  3. void mod(void);
  4. void
  5. eval_mod(void)
  6. {
  7. push(cadr(p1));
  8. eval();
  9. push(caddr(p1));
  10. eval();
  11. mod();
  12. }
  13. void
  14. mod(void)
  15. {
  16. int n;
  17. save();
  18. p2 = pop();
  19. p1 = pop();
  20. if (iszero(p2))
  21. stop("mod function: divide by zero");
  22. if (!isnum(p1) || !isnum(p2)) {
  23. push_symbol(MOD);
  24. push(p1);
  25. push(p2);
  26. list(3);
  27. restore();
  28. return;
  29. }
  30. if (isdouble(p1)) {
  31. push(p1);
  32. n = pop_integer();
  33. if (n == (int) 0x80000000)
  34. stop("mod function: cannot convert float value to integer");
  35. push_integer(n);
  36. p1 = pop();
  37. }
  38. if (isdouble(p2)) {
  39. push(p2);
  40. n = pop_integer();
  41. if (n == (int) 0x80000000)
  42. stop("mod function: cannot convert float value to integer");
  43. push_integer(n);
  44. p2 = pop();
  45. }
  46. if (!isinteger(p1) || !isinteger(p2))
  47. stop("mod function: integer arguments expected");
  48. p3 = alloc();
  49. p3->k = NUM;
  50. p3->u.q.a = mmod(p1->u.q.a, p2->u.q.a);
  51. p3->u.q.b = mint(1);
  52. push(p3);
  53. restore();
  54. }
  55. #if SELFTEST
  56. static char *s[] = {
  57. "mod(2.0,3.0)",
  58. "2",
  59. "mod(-2.0,3.0)",
  60. "-2",
  61. "mod(2.0,-3.0)",
  62. "2",
  63. "mod(-2.0,-3.0)",
  64. "-2",
  65. "mod(2,3)",
  66. "2",
  67. "mod(-2,3)",
  68. "-2",
  69. "mod(2,-3)",
  70. "2",
  71. "mod(-2,-3)",
  72. "-2",
  73. "mod(a,b)",
  74. "mod(a,b)",
  75. "mod(2.0,0.0)",
  76. "Stop: mod function: divide by zero",
  77. "mod(2,0)",
  78. "Stop: mod function: divide by zero",
  79. "mod(1.2,2)",
  80. "Stop: mod function: cannot convert float value to integer",
  81. "mod(1/2,3)",
  82. "Stop: mod function: integer arguments expected",
  83. "mod(15,8.0)",
  84. "7",
  85. };
  86. void
  87. test_mod(void)
  88. {
  89. test(__FILE__, s, sizeof s / sizeof (char *));
  90. }
  91. #endif