advanced number recognition in strings?

Dan Sommers me at privacy.net
Tue May 9 06:31:38 EDT 2006


On Mon, 8 May 2006 17:16:20 +0000 (UTC),
roy at panix.com (Roy Smith) wrote:

> s.schmeier at gmail.com <s.schmeier at gmail.com> wrote:
>> we want extract numbers from strings and wonder if there is already a
>> module around for doing this. An example in our case would look like
>> this:
>> 
>> 0.032 +/- 0.5 x 10(-4)
>> 
>> it would even be helpful to have a routine which does not recognise the
>> +/- , but at least the 10(-4).

[ ... ]

> That being said, e-notation does not represent uncertainty, but then
> again, neither does an IEEE float.  To keep the uncertainty, you need
> to create a new numeric class which stores the mantissa, the
> uncertainty, and the exponent.  Perhaps you could get away with
> storing the mantissa and exponent as a standard float, but you'd still
> need to store the uncertainty.  Then you'd (presumably) need to define
> a whole set of math operators which did something useful with these
> numbers.  For example, what should:

For a physics course I took a couple of years ago, I developed a
number-like class that kept track of uncertainties through various
calculations.  I used a float for both the value and the uncertainty,
and wrote my own __str__ method to make the output look nice.  I didn't
have to parse anything at run-time, though.  I half-implemented the code
to track units as well (IOW, I implemented just enough to meet my own
requirements).

I can post the code somewhere if anyone is interested.

> x = 0.032 +/- 0.5 x 10(-4)
> y = 0.032 +/- 1.0 x 10(-4)
> print x == y

> print out?

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist



More information about the Python-list mailing list