[IronPython] Math accuracy?
Keith J. Farmer
kfarmer at thuban.org
Wed Aug 24 12:51:23 CEST 2005
Here's a *VERY* naïve solution (ie, a 4am solution) for complex ^ positiveInt. It could be improved by generating the binomial expansion directly for n and avoiding a bunch of extra Complex64 instantiations.
IronPython 0.9.1 on .NET 2.0.50215.44
Copyright (c) Microsoft Corporation. All rights reserved.
>>> a = 1j
>>> a ** 2
(-1+0j)
>>> a ** 1
1j
>>> a ** 0
(1+0j)
>>> a ** 3
-1j
>>> a ** 4
(1+0j)
-----
Keith J. Farmer
kfarmer at thuban.org
public Complex64 Power(Complex64 y)
{
double a = real;
double b = imag;
double c = y.real;
double d = y.imag;
if (Math.Truncate(c) == c && d == 0 && c >= 0)
{
switch ((int) c)
{
case 0:
return new Complex64(1, 0);
case 1:
return this;
case 2:
{
double newReal = a * a - b * b;
double newImaginary = 2 * a * b;
return new Complex64(newReal, newImaginary);
}
default:
{
double newReal = a * a - b * b;
double newImaginary = 2 * a * b;
return (new Complex64(newReal, newImaginary)) * this.Power(new Complex64(c-2, 0));
}
}
}
double powers = a * a + b * b;
double arg = Math.Atan2(b, a);
double mul = Math.Pow(powers, c / 2) * Math.Pow(Math.E, -d * arg);
double common = c * arg + .5 * d * Math.Log(powers, Math.E);
return new Complex64(mul * Math.Cos(common), mul * Math.Sin(common));
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20050824/50d6371d/attachment.html>
More information about the Ironpython-users
mailing list