PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/fth/math.fth

https://github.com/cataska/pforth
Forth | 89 lines | 82 code | 7 blank | 0 comment | 1 complexity | 77599bbbfd6ae5e8241774a74b89af80 MD5 | raw file
  1. \ @(#) math.fth 98/01/26 1.2
  2. \ Extended Math routines
  3. \ FM/MOD SM/REM
  4. \
  5. \ Author: Phil Burk
  6. \ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
  7. \
  8. \ The pForth software code is dedicated to the public domain,
  9. \ and any third party may reproduce, distribute and modify
  10. \ the pForth software code or any derivative works thereof
  11. \ without any compensation or license. The pForth software
  12. \ code is provided on an "as is" basis without any warranty
  13. \ of any kind, including, without limitation, the implied
  14. \ warranties of merchantability and fitness for a particular
  15. \ purpose and their equivalents under the laws of any jurisdiction.
  16. anew task-math.fth
  17. decimal
  18. : FM/MOD { dl dh nn | dlp dhp nnp rem quo -- rem quo , floored }
  19. dl dh dabs -> dhp -> dlp
  20. nn abs -> nnp
  21. dlp dhp nnp um/mod -> quo -> rem
  22. dh 0<
  23. IF \ negative dividend
  24. nn 0<
  25. IF \ negative divisor
  26. rem negate -> rem
  27. ELSE \ positive divisor
  28. rem 0=
  29. IF
  30. quo negate -> quo
  31. ELSE
  32. quo 1+ negate -> quo
  33. nnp rem - -> rem
  34. THEN
  35. THEN
  36. ELSE \ positive dividend
  37. nn 0<
  38. IF \ negative divisor
  39. rem 0=
  40. IF
  41. quo negate -> quo
  42. ELSE
  43. nnp rem - negate -> rem
  44. quo 1+ negate -> quo
  45. THEN
  46. THEN
  47. THEN
  48. rem quo
  49. ;
  50. : SM/REM { dl dh nn | dlp dhp nnp rem quo -- rem quo , symmetric }
  51. dl dh dabs -> dhp -> dlp
  52. nn abs -> nnp
  53. dlp dhp nnp um/mod -> quo -> rem
  54. dh 0<
  55. IF \ negative dividend
  56. rem negate -> rem
  57. nn 0>
  58. IF \ positive divisor
  59. quo negate -> quo
  60. THEN
  61. ELSE \ positive dividend
  62. nn 0<
  63. IF \ negative divisor
  64. quo negate -> quo
  65. THEN
  66. THEN
  67. rem quo
  68. ;
  69. : /MOD ( a b -- rem quo )
  70. >r s>d r> sm/rem
  71. ;
  72. : MOD ( a b -- rem )
  73. /mod drop
  74. ;
  75. : */MOD ( a b c -- rem a*b/c , use double precision intermediate value )
  76. >r m*
  77. r> sm/rem
  78. ;
  79. : */ ( a b c -- a*b/c , use double precision intermediate value )
  80. */mod
  81. nip
  82. ;