dpow.cpp 760 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // power function for double precision floating point
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. void
  5. dpow(void)
  6. {
  7. double a, b, base, expo, result, theta;
  8. expo = pop_double();
  9. base = pop_double();
  10. // divide by zero?
  11. if (base == 0.0 && expo < 0.0)
  12. stop("divide by zero");
  13. // nonnegative base or integer power?
  14. if (base >= 0.0 || fmod(expo, 1.0) == 0.0) {
  15. result = pow(base, expo);
  16. push_double(result);
  17. return;
  18. }
  19. result = pow(fabs(base), expo);
  20. theta = M_PI * expo;
  21. // this ensures the real part is 0.0 instead of a tiny fraction
  22. if (fmod(expo, 0.5) == 0.0) {
  23. a = 0.0;
  24. b = sin(theta);
  25. } else {
  26. a = cos(theta);
  27. b = sin(theta);
  28. }
  29. push_double(a * result);
  30. push_double(b * result);
  31. push(imaginaryunit);
  32. multiply();
  33. add();
  34. }