fork download
  1. include mini-oof.fs
  2. : 2fdup ( f1 f2 -- f1 f2 f1 f2 ) fover fover ;
  3. : -frot ( f1 f2 f3 -- f3 f1 f2 ) frot frot ;
  4. : 2f/ ( f1 f2 f3 f4 -- f1/f3 f2/f4 )
  5. frot fswap f/ -frot f/ fswap ;
  6.  
  7. object class
  8. method init
  9. method conjugate
  10. method modulus
  11. method complex>string
  12. float% field real
  13. float% field imag
  14. end-class complex
  15.  
  16. :noname ( r i -- ) dup imag f! real f! ; complex defines init
  17. :noname ( -- c )
  18. dup real f@ imag f@ -1e f*
  19. complex new dup init ; complex defines conjugate
  20. :noname ( -- f )
  21. dup
  22. real f@ fdup f*
  23. imag f@ fdup f*
  24. f+ fsqrt ; complex defines modulus
  25.  
  26. : complex+ ( c c -- c )
  27. 2dup
  28. real f@ real f@ f+
  29. imag f@ imag f@ f+
  30. complex new dup init ;
  31. : complex- ( c c -- c )
  32. 2dup
  33. real f@ real f@ fswap f-
  34. imag f@ imag f@ fswap f-
  35. complex new dup init ;
  36. : complex* ( c c -- c )
  37. 2dup
  38. 2dup real f@ real f@ f* imag f@ imag f@ f* f-
  39. 2dup real f@ imag f@ f* imag f@ real f@ f* f+
  40. complex new dup init ;
  41. : complex/ ( c c -- c )
  42. tuck
  43. conjugate complex*
  44. dup real f@ imag f@
  45. modulus fdup f* fdup 2f/
  46. complex new dup init ;
  47.  
  48. complex new constant t1
  49. complex new constant t2
  50. 3e 5e t1 init
  51. 8e -1e t2 init
  52.  
  53. : complex. ( c -- )
  54. dup real f@ f. imag f@ f. ;
  55.  
  56. ." + " t1 t2 complex+ complex. cr
  57. ." - " t1 t2 complex- complex. cr
  58. ." * " t1 t2 complex* complex. cr
  59. ." / " t1 t2 complex/ complex. cr
  60. ." modulo " t1 modulus f. cr
  61. bye
Success #stdin #stdout 0.02s 7464KB
stdin
Standard input is empty
stdout
+ 11. 4. 
- -5. 6. 
* 29. 37. 
/ 0.292307692307692 0.661538461538462 
modulo 5.8309518948453