numeric emulation and __pos__

E

Ethan Furman

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

samwyse

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.

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

Mark Dickinson

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

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:
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("1.234567891234567891234567891E+35")

Mark
 
E

Ethan Furman

Mark said:
No reason to support any of these for a nonintegral
nonbinary type, as far as I can see.


Very little: it implements the unary + operator, which
for most numeric types is a no-op:


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:


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:
Decimal("16.328")
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).
 
T

Terry Reedy

Ethan said:
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
 
E

Ethan Furman

Terry said:
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.
 
C

casevh

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:
Decimal("1.2346")


Thanks for the feedback.

casevh
 
S

samwyse

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

Sion Arrowsmith

Ethan Furman said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top