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


Groups > comp.lang.python > #8402 > unrolled thread

Significant figures calculation

Started byHarold <dadapapa@googlemail.com>
First post2011-06-24 13:05 -0700
Last post2011-06-24 18:50 -0700
Articles 2 on this page of 22 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Significant figures calculation Harold <dadapapa@googlemail.com> - 2011-06-24 13:05 -0700
    Re: Significant figures calculation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-24 20:46 +0000
      Re: Significant figures calculation Jerry Hill <malaclypse2@gmail.com> - 2011-06-24 16:58 -0400
        Re: Significant figures calculation steve+comp.lang.python@pearwood.info - 2011-06-25 10:31 +1000
        Re: Significant figures calculation Chris Torek <nospam@torek.net> - 2011-06-25 19:04 +0000
          Re: Significant figures calculation Harold <dadapapa@googlemail.com> - 2011-06-26 08:35 -0700
            Re: Significant figures calculation Dave Angel <davea@ieee.org> - 2011-06-27 08:04 -0400
          Re: Significant figures calculation Harold <dadapapa@googlemail.com> - 2011-06-27 06:40 -0700
            Re: Significant figures calculation Ethan Furman <ethan@stoneleaf.us> - 2011-06-27 13:53 -0700
              Re: Significant figures calculation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-28 12:56 +1000
                Re: Significant figures calculation Chris Angelico <rosuav@gmail.com> - 2011-06-28 13:16 +1000
                  Re: Significant figures calculation Erik Max Francis <max@alcyone.com> - 2011-06-27 20:45 -0700
                    Re: Significant figures calculation Mel <mwilson@the-wire.com> - 2011-06-28 07:47 -0400
                      Re: Significant figures calculation Chris Angelico <rosuav@gmail.com> - 2011-06-28 21:57 +1000
                      Re: Significant figures calculation Erik Max Francis <max@alcyone.com> - 2011-06-28 11:54 -0700
                        Re: Significant figures calculation Mel <mwilson@the-wire.com> - 2011-06-28 16:47 -0400
                          Re: Significant figures calculation Erik Max Francis <max@alcyone.com> - 2011-06-28 14:01 -0700
                  Re: Significant figures calculation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-28 18:35 +1000
                    Re: Significant figures calculation Erik Max Francis <max@alcyone.com> - 2011-06-28 11:59 -0700
                Re: Significant figures calculation Erik Max Francis <max@alcyone.com> - 2011-06-27 20:49 -0700
            Re: Significant figures calculation Ethan Furman <ethan@stoneleaf.us> - 2011-06-27 15:58 -0700
      Re: Significant figures calculation Harold <dadapapa@googlemail.com> - 2011-06-24 18:50 -0700

Page 2 of 2 — ← Prev page 1 [2]


#8517

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-27 15:58 -0700
Message-ID<mailman.465.1309214754.1164.python-list@python.org>
In reply to#8506
Harold Fellermann wrote:
> Hi Ethan,
> 
>>>>>> Empirical('1200.').significance
>>> 2
>>>>>> Empirical('1200.0').significance
>>> 5
>> What about when 1200 is actually 4 significant digits? Or 3?
> 
> Then you'd simply write 1.200e3 and 1.20e3, respectively.
> That's just how the rules are defined.

But your code is not following them:

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
--> from decimal import Decimal
--> class Empirical(Decimal) :
...     @property
...     def significance(self) :
...         t = self.as_tuple()
...         if t[2] < 0 :
...             return len(t[1])
...         else :
...             return len(''.join(map(str,t[1])).rstrip('0'))
...
--> Empirical('1.200E+3').significance
2  # should be four
--> Empirical('1.20E+3').significance
2  # should be three
--> Empirical('1.20E+4').significance
2  # should be three

The negatives appear to work, though:
--> Empirical('1.20E-4').significance
3
--> Empirical('1.2819E-3').significance
5
--> Empirical('1.2819E-1').significance
5
--> Empirical('1.281900E-1').significance
7

~Ethan~

[toc] | [prev] | [next] | [standalone]


#8426

FromHarold <dadapapa@googlemail.com>
Date2011-06-24 18:50 -0700
Message-ID<218ee71c-ba06-4e94-aaac-c87b44cce554@5g2000yqb.googlegroups.com>
In reply to#8406
> > I tried to modify the DecimalContext (e.g. getcontext().prec = 2) but
> > that did not lead to the correct behavior.
>
> Really? It works for me.

You are right, I did something wrong when attempting to set the
precision.
And the trick with rounding the decimal with the unary + is neat.
It's the first time for me to play with decimals, so bare with me if I
miss
the obvious.

However, this approach forces you to know the number of significant
figures
beforehand -- which is precisely what the arithmetics should do for
you.
What would indeed be nice, is Jerry's suggestion to obtain the number
of
significant bits and write a small wrapper around the number protocoll
implementation that accounts significance and have __str__/__repr__
set
the context dynamically.

I haven't seen anything obvious in the docs, though it might be
possible
to use log10 of the length of some normalized string representation.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web