real.cpp 560 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Returns the real part of complex z
  2. z real(z)
  3. - -------
  4. a + i b a
  5. exp(i a) cos(a)
  6. */
  7. #include "stdafx.h"
  8. #include "defs.h"
  9. void
  10. eval_real(void)
  11. {
  12. push(cadr(p1));
  13. eval();
  14. real();
  15. }
  16. void
  17. real(void)
  18. {
  19. save();
  20. rect();
  21. p1 = pop();
  22. push(p1);
  23. push(p1);
  24. conjugate();
  25. add();
  26. push_integer(2);
  27. divide();
  28. restore();
  29. }
  30. #if SELFTEST
  31. static char *s[] = {
  32. "real(a+i*b)",
  33. "a",
  34. "real(1+exp(i*pi/3))",
  35. "3/2",
  36. "real(i)",
  37. "0",
  38. "real((-1)^(1/3))",
  39. "1/2",
  40. };
  41. void
  42. test_real(void)
  43. {
  44. test(__FILE__, s, sizeof s / sizeof (char *));
  45. }
  46. #endif