String representations of numbers and approximate equality

Dave Angel davea at davea.name
Wed Sep 24 15:56:00 EDT 2014


Chris Angelico <rosuav at gmail.com> Wrote in message:
> 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:
> 

Your definition is not nearly as concrete as you think. Is the
 first number considered to be exact, and we'll only check the
 second? Will the factor always be an int, and thus
 exact?

When the desired second number is exactly halfway between two
 values, is rounding down as acceptable as up?
  (Eg, exact answer 142.75, acceptable 142.7 and 142.8)


Once you're sure of the goal, it's a question of whether the
 decimal package can handle it. I would be inclined to do it in
 two stages. Drop all the periods and convert each to int (long).
 Multiply and Add 5**n, where n is related to the length of the
 target. Convert to string,  padded with lots of zeroes. 
 

See if the exact string begins with the target. If not, they're
 not close enough.

If the first part passes, you just have to check the position of
 the decimal point.

Or you could check the implementation of round. If you pick the
 right value for round,  you could do an exact comparison.
 

Alternatively,  if you're just looking to catch typos,  it could
 be simpler. Or if you put constraints on the numbers
 involved.

-- 
DaveA




More information about the Python-list mailing list