Python speed vs csharp
Mike
mike at nospam.com
Sat Aug 2 20:48:29 CEST 2003
On Fri, 01 Aug 2003 16:14:47 -0400, David M. Cooke wrote:
> At some point, Mike <mike at nospam.com> wrote:
> [...]
>> # Rational approximation for erfc(x) (Abramowitz & Stegun, Sec. 7.1.26)
>> # Fifth order approximation. |error| <= 1.5e-7 for all x
>> #
>> def erfc( x ):
>> p = 0.3275911
>> a1 = 0.254829592
>> a2 = -0.284496736
>> a3 = 1.421413741
>> a4 = -1.453152027
>> a5 = 1.061405429
>>
>> t = 1.0 / (1.0 + p*float(x))
>> erfcx = ( (a1 + (a2 + (a3 +
>> (a4 + a5*t)*t)*t)*t)*t ) * math.exp(-(x**2))
>> return erfcx
>
> Since no else has mentioned this, and it's not in the comments: the
> above code is no good for x < 0 (erfc(-3)=84337, for instance). You'll
> need a check for x < 0, and if so, use the identity
> erfc(x) = 2 - erfc(-x). That'll slow you down a tad again :)
That's correct (and noted in A&S). Since the argument I use is time
relative to the start of the simulation, it's always positive. Since I'm
not using it as a general purpose erfc calculator, I didn't bother with any
checks (although, now that you've mentioned it, it would be rather stupid
not to add a note to the comments).
-- Mike --
More information about the Python-list
mailing list