but I don't want to print the trailing ".0"

Gareth McCaughan Gareth.McCaughan at pobox.com
Thu May 1 20:16:23 EDT 2003


Dan Jacobson wrote:

> What is the "in crowd" method to enhance s.py so I don't have to use
> sed to get what I want?
> $ cat s.py
> steps=(1500,1250,1000,625,500)
> for i in steps:
>     print i/100.0 #how do I enhance this line, please? what %f thingy?

print "%g" % (i/100.0)

> By the way, any dumbness here?:
> for arg in sys.argv[1:]: #no --help yet, never mind.
>     freq=string.atof(arg)
>     remainder=int(round(freq*1000))%9
>     if not remainder: ...
> 
> pydoc string says string.atof is obsolete it seems, oh, I see, I am
> supposed to use float(), ok, I wish it said that there on pydoc string.
> How about int(round( ? Looks goofy.

It's right, whether or not it looks goofy.
If you mean that round() ought to return an integer,
then you're probably unaware of the fact that it can
round to a number of decimal places other than 0. :-)

> BTW, I believe I'm doing this to avoid this wackyness: 
> $ python -c 'a= 3.3*100000;print a,a % 500'
> 330000.0 0.0 #OK
> $ python -c 'a=33.3*100000;print a,a % 500'
> 3330000.0 500.0 #Yuck

It's not clear whether you've worked out what's
going on here, so I'll tell you :-). The numbers
33/10 and 333/10 aren't exactly representable as
IEEE double-precision floating-point numbers,
so when you ask Python for 3.3 or 33.3 it gives
you the nearest approximation it can.

Those numbers are:

  - for  3.3: 7430939385161318 * 2**-51
  - for 33.3: 4686558362232422 * 2**-47

When you multiply these by 100000 and round to the
nearest representable IEEE double-precision float,
you get:

  - for  3.3*100000: 5669356830720000 * 2**-34
  - for 33.3*100000: 7151120547839999 * 2**-31

It just so happens that the first of these is exactly
equal to 330000, whereas the second is just slightly
less than 3330000; 2**-31 less, in fact. So when you
reduce them modulo 500, the first gives exactly 0 and
the second gives 500-2**-31. Which is close enough to
500 that when you print it it just says "500.0".

> BTW,
> $ python -Qwarnall someotherprog.py
> someotherprog.py:34: DeprecationWarning: classic float division
>   print arg + ":", step/100.0
> OK, then can it or somebody tell me how it prefers me to change it?
> If I have the .0 trailing aren't I safe for decades to come?

The bit of Python that issues the warning is buried
quite deep; by the time it has control, the fact that
the denominator came from a floating-point literal
has been forgotten.

But -Qwarnall is not intended for human use; it's for
use with the "fixdiv.py" script. You run your program
using -Qwarnall, dumping the warnings to a file, and
then fixdiv.py grovels over that file and works out
which instances of division might need to change. Not
producing spurious warnings simply isn't any part of
what -Qwarnall is about.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
.sig under construc




More information about the Python-list mailing list