Underscores in Python numbers
Roy Smith
roy at panix.com
Sun Nov 20 15:52:29 EST 2005
cookedm+news at physics.mcmaster.ca (David M. Cooke) wrote:
> One example I can think of is a large number of float constants used
> for some math routine. In that case they usually be a full 16 or 17
> digits. It'd be handy in that case to split into smaller groups to
> make it easier to match with tables where these constants may come
> from. Ex:
>
> def sinxx(x):
> "computes sin x/x for 0 <= x <= pi/2 to 2e-9"
> a2 = -0.16666 66664
> a4 = 0.00833 33315
> a6 = -0.00019 84090
> a8 = 0.00000 27526
> a10= -0.00000 00239
> x2 = x**2
> return 1. + x2*(a2 + x2*(a4 + x2*(a6 + x2*(a8 + x2*a10))))
>
> (or least that's what I like to write). Now, if I were going to higher
> precision, I'd have more digits of course.
You have described, if memory serves, a Taylor series, and those
coefficients are 1/3!, 1/5!, 1/7!, etc. What I would do, rather than
embedding the numeric constants in the code, is embed the formula and have
the machine compute the actual values at import time. At the very least,
have them machine generated and saved. You certainly don't want to be
typing in long strings of digits like that.
More information about the Python-list
mailing list