I'm working on a numeric data type for measured values that will keep
track of and limit results to the number of significant digits
originally defined for the values in question.
I am doing this primarily because I enjoy playing with numbers, and also
to get some experience with unit testing.
At this point I have the __init__ portion finished, and am starting on
the various operator functions.
Questions for the group:
1) Any reason to support the less common operators?
i.e. <<, >>, &, ^, |
2) What, exactly, does .__pos__() do? An example would help, too.
Thanks for the feedback.
--
Ethan
1) Those make much less sense for non-integers. I'd say skip them.
2) It's an overridable no-op that implements the unary plus
operator. Unary plus returns its value unchanged, as does __pos__.
No reason to support any of these for a nonintegral
nonbinary type, as far as I can see.
> 2) What, exactly, does .__pos__() do? An example would help, too.
>
Very little: it implements the unary + operator, which
for most numeric types is a no-op:
>>> +5.0
5.0
So you could safely implement it as:
def __pos__(self):
return self
By the way, have you met the Decimal type? It also
keeps track of significant zeros. For example:
>>> from decimal import Decimal
>>> Decimal('1.95') + Decimal('2.05')
Decimal("4.00")
(Note that the output includes the extra zeros...)
Interestingly, in Decimal the __pos__ operation isn't a no-op:
it rounds a Decimal to the current context, and it also
(mistakenly, in my opinion) changes -0.0 to 0.0:
>>> +Decimal('-0.0')
Decimal("0.0")
>>> +Decimal('123456789123456789123456789123456789')
Decimal("1.234567891234567891234567891E+35")
Mark
Anybody have an example of when the unary + actually does something?
Besides the below Decimal example. I'm curious under what circumstances
it would be useful for more than just completeness (although
completeness for it's own sake is important, IMO).
> So you could safely implement it as:
>
> def __pos__(self):
> return self
>
> By the way, have you met the Decimal type? It also
> keeps track of significant zeros. For example:
>
>>>>from decimal import Decimal
>>>>Decimal('1.95') + Decimal('2.05')
>
> Decimal("4.00")
Significant zeroes is not quite the same as significant digits (although
Decimal may support that too, I haven't studied it). The difference
being, for example, the following:
>>> from decimal import Decimal
>>> first = Decimal("3.14")
>>> second = Decimal("5.2")
>>> first * second
Decimal("16.328")
>>> from measure import Measure
>>> third = Measure("3.14")
>>> fourth = Measure("5.2")
>>> third * fourth
Measure("16")
In other words, any calculated value cannot be more accurate than the
least accurate input (5.2 in the case above, which hase two significant
digits).
> (Note that the output includes the extra zeros...)
> Interestingly, in Decimal the __pos__ operation isn't a no-op:
> it rounds a Decimal to the current context, and it also
> (mistakenly, in my opinion) changes -0.0 to 0.0:
>
>
>>>>+Decimal('-0.0')
>
> Decimal("0.0")
>
>>>>+Decimal('123456789123456789123456789123456789')
>
> Decimal("1.234567891234567891234567891E+35")
>
> Mark
--
Ethan
Ethan Furman wrote:
> Anybody have an example of when the unary + actually does something?
> Besides the below Decimal example. I'm curious under what circumstances
> it would be useful for more than just completeness (although
> completeness for it's own sake is important, IMO).
All true operators and some built-in functions translate to special
method calls.
-x == x.__neg__() == type(x).__neg__(x) == x.__class__.__neg__(x)
What should happen to '+x'? Raise SyntaxError? Ignore the '+'? Or
translate it to __pos__ in analogy with '-' and __neg__? Guido made the
third choice: partly for consistency perhaps, but also for the benefit
of user-written classes. Decimal started as user-contributed decimal.py.
Does anything else use this hook? I don't know, but I do know that
there are periodic demands for *more* operators for user use. So I
expect it has been.
tjr
It definitely makes sense from that perspective. As I was reading the
docs for numeric emulation I came across the unary + and wasn't sure
what to make of it. I tried it out, and got exactly what I put in, for
everything I tried. So I wondered if there were any situations where
you would get something besides what you started with.
Thanks to everyone for your comments and help.
--
Ethan
1) Multiplication or division by powers of 2. This mimics the common
use of those operators as used with binary numbers.
2) Multiplication or division by powers of 10.
>
> 2) What, exactly, does .__pos__() do? An example would help, too.
The unary + operator is frequently added for symmetry with -, however
it is also used to force an existing number to match a new precision
setting. For example, using the decimal module:
>>> from decimal import *
>>> t=Decimal('1.23456')
>>> t
Decimal("1.23456")
>>> getcontext().prec = 5
>>> +t
Decimal("1.2346")
>
> Thanks for the feedback.
> --
> Ethan
casevh
> Anybody have an example of when the unary + actually does something?
> Besides the below Decimal example. I'm curious under what circumstances
> it would be useful for more than just completeness (although
> completeness for it's own sake is important, IMO).
Well, as in Decimal, it would be a good operator to use for
canonization. Let's say you implement complex numbers as an angle and
radius. Then, unary plus could be used to normalize the angle to +/-
Pi and the radius to a positive number (by inverting the angle).
I've seen it (jokingly) used to implement a prefix increment
operator. I'm not going to repeat the details in case somebody
decides it's serious code.
--
\S -- si...@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump