sin.cmn 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Program that computes the sine function with fixed point.
  2. PI: 3216 .
  3. # prints and pops fixed point number (1024 ~= 1.0) from -9.99 to 9.99
  4. printFixedPoint:
  5. $0 0 << ?
  6. "-" ->
  7. -1 *
  8. .
  9. $0 1024 / "0" + ->
  10. "." ->
  11. @'
  12. 1024 % 10 *
  13. $0 1024 / "0" + ->
  14. $0 0 = ?
  15. !@
  16. .
  17. .
  18. "0" ->
  19. ^
  20. .
  21. # Bhaskara I's sine approximation
  22. sin:
  23. 1 # stack: x sign
  24. $1 0 << ?
  25. ^
  26. -1 * # x *= -1
  27. -1 # sign = -1
  28. .
  29. >< # stack: sign x
  30. 2 PI * % # x %= 2 * PI
  31. $0 PI > ? # x > PI ?
  32. PI - # x -= PI
  33. >< -1 * >< # sign *= -1
  34. .
  35. PI $1 - * # x *= PI - x
  36. $0 16 * # 16 * x
  37. $1 4 * # 4 * x
  38. 5 PI * PI * # 5 * PI * PI
  39. >< - # 5 * PI * PI - 4 * x
  40. 1024 / # (5 * PI * PI - 4 * x) / 1024
  41. / >< ^ * # (16 * x) / ((5 * PI * PI - 4 * x) / 1024)
  42. .
  43. #-----------------
  44. PI -1 * # from
  45. @@
  46. 100 +
  47. $0 $0 sin
  48. $0 2000 + 64 / 50
  49. @'
  50. $1 $1 = ? "#" ; " " . ->
  51. --
  52. .
  53. ^ ^
  54. ><
  55. 0 "sin(" -->
  56. printFixedPoint
  57. 0 ") = " -->
  58. printFixedPoint
  59. 10 ->
  60. $0 PI 2 * >>= ?
  61. !@
  62. .
  63. .