[Python-Dev] math.areclose ...? (original) (raw)
Aahz aahz at pythoncraft.com
Mon Feb 6 21:20:31 CET 2006
- Previous message: [Python-Dev] math.areclose ...?
- Next message: [Python-Dev] math.areclose ...?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Feb 06, 2006, Chris or Leslie Smith wrote:
Aahz:
Alex:
|| def areclose(x,y,rtol=1.e-5,atol=1.e-8): || return abs(x-y)<atol+rtol*abs(y) | | Looks interesting. I don't quite understand what atol/rtol are, | though. Does it help to spell it like this? def areclose(x, y, relativeerr = 1.e-5, absoluteerr=1.e-8): diff = abs(x - y) ave = (abs(x) + abs(y))/2 return diff < absoluteerr or diff/ave < relativeerr Also, separating the two terms with 'or' rather than '+' makes the two error terms mean more what they are named. The '+' mixes the two effects and even though the result is basically the same, it makes it difficult to explain when the test will be true.
Yes, that's a big help. I was a bit concerned that this would have no utility for numbers with large magnitude. Alex, given your focus on Python readability, I'm a bit surprised you didn't write this to start with!
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"19. A language that doesn't affect the way you think about programming, is not worth knowing." --Alan Perlis
- Previous message: [Python-Dev] math.areclose ...?
- Next message: [Python-Dev] math.areclose ...?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]