Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78315 > unrolled thread
| Started by | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| First post | 2014-09-24 16:41 -0700 |
| Last post | 2014-09-24 23:20 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2014-09-24 16:41 -0700 |
| Subject | Re: String representations of numbers and approximate equality |
| Message-ID | <mailman.14334.1411602084.18130.python-list@python.org> |
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')
[toc] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-09-24 23:20 -0700 |
| Message-ID | <82a86410-5b0f-4742-be00-69e0e8f94e52@googlegroups.com> |
| In reply to | #78315 |
Le jeudi 25 septembre 2014 01:41:42 UTC+2, Dan Stromberg a écrit :
> 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')
Basically yes, but...
In the present case, what counts is the confidence interval
of a calculation, not the the precision of the numbers
used for the calculation.
>>> def z(x, y):
... x, y = float(x), float(y)
... err = 0.05 * y
... fac = 30
... if y - err <= x * fac < y + err:
... return 'good enough'
... else:
... return 'bad'
...
>>> z('0.1', '10')
'bad'
>>> z('0.1', '3')
'good enough'
>>> z('4.73', '142')
'good enough'
>>> z('4.729999', '142')
'good enough'
>>> z('4.7300001', '142')
'good enough'
>>> z('4.73', '140')
'good enough'
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web