set decimal place

Chris Barker chrishbarker at
Fri Dec 28 14:08:58 EST 2001

Peter Hansen wrote:
> > not completely. you might want to check if two values are the same to 4
> > decimal places, in which case round should work just fine:
> >
> > >>> 10.0/3 == 10.00001/3
> > 0
> > >>> round(10.0/3,4) == round(10.00001/3,4)
> > 1
> I don't know if this is guaranteed to work in all cases.

That depends on how robust the round() code is, and I have no idea about
that. Someone who knows better than me suggested using float("%.4g"%x)
to get significant figures, because most C libraries have robust and
correct code for the %g format specifier. Also, there is the problem of
how you choose to define "correct" rounding. There is no one definition.

> Comparing floating point values directly is generally
> not a good idea, although I don't know whether this applies
> to rounded values, and if so how far you have to round them
> before it makes it safe.

Exactly, it is one easy way to check if two floating point values are
"close enough", by a criteria most of us understand.

> If it is safe, you have a valid point, but not likely what
> the OP wanted.

The OP wasn't all that clear about what they wanted, but you're probably


Christopher Barker,
ChrisHBarker at                ---           ---           ---
                                     ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------

More information about the Python-list mailing list