Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Approximately equal numeric fields #2

Open
dan-blanchard opened this issue Dec 1, 2014 · 3 comments
Open

Approximately equal numeric fields #2

dan-blanchard opened this issue Dec 1, 2014 · 3 comments
Labels

Comments

@dan-blanchard
Copy link

It'd be really nice if there was some way to say "Consider numeric values equivalent if they're equal to a certain number of decimal places."

Here's a simple function for doing so from Stack Overflow:

def nearly_equal(a, b, sig_fig=5):
    return (a == b or 
            int(a * 10**sig_fig) == int(b * 10**sig_fig))
@larsyencken
Copy link
Owner

That's an excellent suggestion.

Out of curiosity, what's your use case? What types of things are you comparing?

@dan-blanchard
Copy link
Author

I'm comparing the output of a system that generates many columns of floats. Because of floating point precision nonsense, when I run it on different machines the outputs differ after the 9th decimal and I don't care about those differences. I'm trying to find more substantial differences that would indicate an error.

@ivansabik
Copy link

This issue is really old is really old but I find myself also using that a lot. My specific case is I use a lot of times Excel for editing the new file I want to compare against to (for instance I remove columns so that it matches the other one). Our friend Excel likes adding .00 (or removing those not sure exactly now) and since string/text comparison is being done currently it accounts for it as modified. Still working on adding other feature I use a lot (xlsx export) but once I'm done with that might take a look at this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants