floor.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "stdafx.h"
  2. #include "defs.h"
  3. void
  4. eval_floor(void)
  5. {
  6. push(cadr(p1));
  7. eval();
  8. yfloor();
  9. }
  10. void
  11. yfloor(void)
  12. {
  13. save();
  14. yyfloor();
  15. restore();
  16. }
  17. void
  18. yyfloor(void)
  19. {
  20. double d;
  21. p1 = pop();
  22. if (!isnum(p1)) {
  23. push_symbol(FLOOR);
  24. push(p1);
  25. list(2);
  26. return;
  27. }
  28. if (isdouble(p1)) {
  29. d = floor(p1->u.d);
  30. push_double(d);
  31. return;
  32. }
  33. if (isinteger(p1)) {
  34. push(p1);
  35. return;
  36. }
  37. p3 = alloc();
  38. p3->k = NUM;
  39. p3->u.q.a = mdiv(p1->u.q.a, p1->u.q.b);
  40. p3->u.q.b = mint(1);
  41. push(p3);
  42. if (isnegativenumber(p1)) {
  43. push_integer(-1);
  44. add();
  45. }
  46. }
  47. #if SELFTEST
  48. static char *s[] = {
  49. "floor(a)",
  50. "floor(a)",
  51. "floor(a+b)",
  52. "floor(a+b)",
  53. "floor(5/2)",
  54. "2",
  55. "floor(4/2)",
  56. "2",
  57. "floor(3/2)",
  58. "1",
  59. "floor(2/2)",
  60. "1",
  61. "floor(1/2)",
  62. "0",
  63. "floor(0/2)",
  64. "0",
  65. "floor(-1/2)",
  66. "-1",
  67. "floor(-2/2)",
  68. "-1",
  69. "floor(-3/2)",
  70. "-2",
  71. "floor(-4/2)",
  72. "-2",
  73. "floor(-5/2)",
  74. "-3",
  75. "floor(5/2.0)",
  76. "2",
  77. "floor(4/2.0)",
  78. "2",
  79. "floor(3/2.0)",
  80. "1",
  81. "floor(2/2.0)",
  82. "1",
  83. "floor(1/2.0)",
  84. "0",
  85. "floor(0.0)",
  86. "0",
  87. "floor(-1/2.0)",
  88. "-1",
  89. "floor(-2/2.0)",
  90. "-1",
  91. "floor(-3/2.0)",
  92. "-2",
  93. "floor(-4/2.0)",
  94. "-2",
  95. "floor(-5/2.0)",
  96. "-3",
  97. };
  98. void
  99. test_floor(void)
  100. {
  101. test(__FILE__, s, sizeof s / sizeof (char *));
  102. }
  103. #endif