Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / dpow.cpp
blob026e189e8528e4a47dfbd94d75141041af61d10d
1 // power function for double precision floating point
3 #include "stdafx.h"
4 #include "defs.h"
6 void
7 dpow(void)
9 double a, b, base, expo, result, theta;
11 expo = pop_double();
12 base = pop_double();
14 // divide by zero?
16 if (base == 0.0 && expo < 0.0)
17 stop("divide by zero");
19 // nonnegative base or integer power?
21 if (base >= 0.0 || fmod(expo, 1.0) == 0.0) {
22 result = pow(base, expo);
23 push_double(result);
24 return;
27 result = pow(fabs(base), expo);
29 theta = M_PI * expo;
31 // this ensures the real part is 0.0 instead of a tiny fraction
33 if (fmod(expo, 0.5) == 0.0) {
34 a = 0.0;
35 b = sin(theta);
36 } else {
37 a = cos(theta);
38 b = sin(theta);
41 push_double(a * result);
42 push_double(b * result);
43 push(imaginaryunit);
44 multiply();
45 add();