String representations of numbers and approximate equality

Chris Angelico rosuav at gmail.com
Wed Sep 24 18:54:12 CEST 2014


Yes, it's another "how can I see if two numbers are approximately
equal" thread, but this time I have a concrete definition of
"approximately equal"... and they're Decimals, not floats.

I have a number of files (this is an ongoing thing) in which there are
two columns of numbers. One of them should be equal to the other times
some factor which the program knows from elsewhere. All the numbers
are represented as strings of ASCII decimal digits, possibly including
a U+002E decimal point. Something like this:

# (these are calculated on factor 30)
["0.75", "22.5"]
["0.80", "24"]
["4.73", "142"]

The definition of valid is that, within the rounding they've been
given, the values are correct. The first two are strictly correct; the
third would be 141.9 with full accuracy but 142 is deemed good enough.
But this should fail:

["0.1", "10"]

0.1 * 30 should be 3, not 10; and 10 / 30 should be 0.333, not 0.1;
therefore this is wrong.

This is a san-check for human-entered data, so sloppiness is
necessary. I just want to have a chance to catch obvious typos and
such. In theory, I think, a decimal.Decimal context should be able to
do what I want here - but I'm not particularly familiar with it.
Performance isn't critical, so stupid techniques that work on the
actual string representation would be fine too.

In general, the numbers will be fairly small. Maybe 3-4 digits before
the decimal point, and probably not more than 2-3 after. Not sure if
that makes things any easier or not.

All advice appreciated!

ChrisA



More information about the Python-list mailing list