results of division

Dan Bishop danb_83 at yahoo.com
Thu Dec 9 16:23:47 EST 2004


Brad Tilley wrote:
> Hello,
>
> What is the proper way to limit the results of division to only a few

> spaces after the decimal? I don't need rocket-science like precision.

> Here's an example:

If your only complaint is that it's ugly to display 17 digits, then use
the % operator to display however many digits you want.

print "%.3f" % x        # prints 3 digits after the decimal point

If you're referring to internal calculations, then you seem to have a
misunderstanding of the way floating-point math works.  Intel hardware
does all arithmetic with 64 significant bits (which get rounded to 53
when stored as "double precision"), and there's no way to tell it
"Don't bother calculating all 64 bits; I only need 11."  It would take
extra code to get rid of the extra bits, and why bother slowing down
your program to make the math *less* accurate?




More information about the Python-list mailing list