Modifying the value of a float-like object

Eric.Le.Bigot at spectro.jussieu.fr Eric.Le.Bigot at spectro.jussieu.fr
Tue Apr 14 15:48:19 EDT 2009


Thanks Dave for your thoughtful remarks, which you sent right when I
was writing a response to the previous posts.

I was wondering about a kind "mutable float"; so you're right, it's
not fully a float, because it's mutable.  I'd like to have an object
that behaves like a float in numerical calculations.  I understand
that mutability would require to handle such objects with care, as in
your example, but Python programmers are used to this with any mutable
object.  All my calculations use "constants with an uncertainty" (or
regular floats).

There are many such calculations in my code, and I'd like it to be
very clean.  The first idea I mentioned (using "x()") is essentially
what you propose with using "x[0]" in calculations.

So, it looks like it's not possible to have float-like objects
(calculation/formula-wise) that are mutable?!  and it also looks like
the price to pay for the mutability is to have to write "heavier"
versions of any formula that uses these "special floats" (that carry
an uncertainty): "x[0]+y[0]*sin(...)", "x()+y()", "float(x)+float
(y)",...  which, by the way, essentially prevents any float-like
object from being used as a float in formulas that you don't write
yourself.

This does not look good.  Python is failing me!!! :/  I heard that
this would be easy to do in Ruby (not confirmed, though)...

More ideas and thoughts would still be most welcome!

On Apr 14, 8:45 pm, Dave Angel <da... at ieee.org> wrote:
> Eric.Le.Bi... at spectro.jussieu.fr wrote:
> > It looks like what is needed here are a kind of "mutable float".  Is
> > there a simple way of creating such a type?  I don't mind changing the
> > value through x.value =.23 instead of x = 1.23... :)
>
> > On Apr 14, 3:03 pm, Eric.Le.Bi... at spectro.jussieu.fr wrote:
>
> >> Hello,
>
> >> Is there a way to easily build an object that behaves exactly like a
> >> float, but whose value can be changed?  The goal is to maintain a list
> >> [x, y,…] of these float-like objects, and to modify their value on the
> >> fly (with something like x.value =.14) so that any expression like "x
> >> +y" uses the new value.
>
> >> I thought of two solutions, both of which I can't make to work:
>
> >> 1) Use a class that inherits from float.  This takes care of the
> >> "behave like float" part.  But is it possible to change the value of
> >> the float associated with an instance?  That is, is it possible to
> >> do:  "x =yFloat(1.23); x.change_value(3.14)" so that x's float value
> >> becomes 3.14?
>
> >> 2) The other possibility I thought of was: use a class that defines a
> >> 'value' member (x.value).  This takes care of the "value can be
> >> changed" part.  But is it possible/easy to make it fully behave like a
> >> float (including when passed to functions like math.sin)?
>
> >> Alternatively, I'd be happy with a way of handling numerical
> >> uncertainties in Python calculations (such as in "calculate the value
> >> and uncertainty of a*sin(b) knowing that a=0 +/- 0.1 and b=1.00 +/-
> >> 0.01").
>
> >> Any idea would be much appreciated!
>
> The answer to your original question is no.  If the value can be changed, then it doesn't behave like a float.  And that's not just a pedantic answer, it's a serious consideration.
>
> You have to decide what characteristics of a float you need to mimic, and which ones you don't care about, and which ones you want to change.  Only after having a pretty good handle on those answers can you pick a "best" implementation.
>
> Let's call this new type a nfloat, and let's assume you have a function that returns one.  That might be a constructor, but it may not, so we're keeping our options open.
>   myval = newfunction(42.0)
>
> What do you want to happen when you execute   b = myval ?   Presumably
> you want them to be "equal" but in what sense?  Suppose you then change
> one of them with your suggested attribute/method.
>      myval.value = newfunction("aaa")
>
> is b the same as it was (like a float would be), or is b also changed?
>
> Do you need lots of functions to work on one of these nfloats, or could
> you use them as follows:
>      sin( b.value )
>
> Instead of using .value to change the underlying float, how about if you
> use [0] ?  Just use a list of size 1.




More information about the Python-list mailing list