REPOST: Re: set decimal place

Chris Barker chrishbarker at attbi.com
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
right!

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at attbi.net                ---           ---           ---
                                     ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-milwwi.newsops.execpc.com!newsfeeds.sol.net!newspump.sol.net!nntp1.roc.gblx.net!nntp.gblx.net!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!enews.sgi.com!news-xfer.nuri.net!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Chris Barker <chrishbarker at attbi.com>
Newsgroups: comp.lang.python
Subject: cmsg cancel <3C2CC34A.ED177E8D at attbi.com>
Control: cancel <3C2CC34A.ED177E8D at attbi.com>
Date: Mon, 31 Dec 2001 01:51:28 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.3C2CC34A.ED177E8D at attbi.com>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009775131 27193 211.57.49.2 (31 Dec 2001 05:05:31 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:05:31 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list