Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78315

Re: String representations of numbers and approximate equality

References <CAPTjJmpOxK5xosvk-=0nBdpsXoEwoSj5qEyY5pf1yFS2PbEoww@mail.gmail.com>
Date 2014-09-24 16:41 -0700
Subject Re: String representations of numbers and approximate equality
From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.14334.1411602084.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Sep 24, 2014 at 9:54 AM, Chris Angelico <rosuav@gmail.com> wrote:
> 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.

tolerance = decimal.Decimal('0.1') # I hesitate to call it epsilon

if abs(decimal1 - decimal2) <= tolerance:
   print('They are close')
else:
   print('They are different')

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: String representations of numbers and approximate equality Dan Stromberg <drsalists@gmail.com> - 2014-09-24 16:41 -0700
  Re: String representations of numbers and approximate equality wxjmfauth@gmail.com - 2014-09-24 23:20 -0700

csiph-web