Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

numeric emulation and __pos__

26 views
Skip to first unread message

Ethan Furman

unread,
Jul 7, 2008, 7:12:27 PM7/7/08
to Python
Greetings, List!

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

samwyse

unread,
Jul 8, 2008, 9:32:51 AM7/8/08
to

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__.

Mark Dickinson

unread,
Jul 8, 2008, 9:55:26 AM7/8/08
to
On Jul 8, 12:12 am, Ethan Furman <et...@stoneleaf.us> wrote:
> 1) Any reason to support the less common operators?
>         i.e. <<, >>, &, ^, |

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

Ethan Furman

unread,
Jul 8, 2008, 1:34:04 PM7/8/08
to Python
Mark Dickinson wrote:
> On Jul 8, 12:12 am, Ethan Furman <et...@stoneleaf.us> wrote:
>
>>1) Any reason to support the less common operators?
>> i.e. <<, >>, &, ^, |
>
> 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

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

Terry Reedy

unread,
Jul 8, 2008, 5:17:52 PM7/8/08
to pytho...@python.org

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

Ethan Furman

unread,
Jul 8, 2008, 6:54:37 PM7/8/08
to pytho...@python.org

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

casevh

unread,
Jul 8, 2008, 11:41:41 PM7/8/08
to
On Jul 7, 4:12 pm, Ethan Furman <et...@stoneleaf.us> wrote:
> Greetings, List!
>
> 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. <<, >>, &, ^, |
Assuming you are working with decimal numbers, the &, ^, | may not be
of any use for your application. The shift operators may be useful but
there are two possible ways to define their behavior:

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

samwyse

unread,
Jul 9, 2008, 11:03:39 AM7/9/08
to
On Jul 8, 12:34 pm, Ethan Furman <et...@stoneleaf.us> 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).

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).

Sion Arrowsmith

unread,
Jul 9, 2008, 11:44:48 AM7/9/08
to
Ethan Furman <et...@stoneleaf.us> wrote:
>Anybody have an example of when the unary + actually does something?

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

0 new messages