Underscores in Python numbers

David M. Cooke cookedm+news at physics.mcmaster.ca
Sun Nov 20 06:45:51 CET 2005


Peter Hansen <peter at engcorp.com> writes:

> Steven D'Aprano wrote:
>> Dealing with numeric literals with lots of digits is
>> a real (if not earth-shattering) human interface problem: it is hard for
>> people to parse long numeric strings.
>
> I'm totally unconvinced that this _is_ a real problem, if we define 
> "real" as being even enough to jiggle my mouse, let alone shattering the 
> planet.
>
> What examples does anyone have of where it is necessary to define a 
> large number of large numeric literals?  Isn't it the case that other 
> than the odd constants in various programs, defining a large number of 
> such values would be better done by creating a data file and parsing
> it?

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.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list