473,769 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

numeric emulation and __pos__

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
Jul 7 '08 #1
8 1464
On Jul 7, 6:12*pm, Ethan Furman <et...@stonelea f.uswrote:
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__.
Jul 8 '08 #2
On Jul 8, 12:12*am, Ethan Furman <et...@stonelea f.uswrote:
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('123 456789123456789 123456789123456 789')
Decimal("1.2345 678912345678912 34567891E+35")

Mark
Jul 8 '08 #3
Mark Dickinson wrote:
On Jul 8, 12:12 am, Ethan Furman <et...@stonelea f.uswrote:
>>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('1 234567891234567 891234567891234 56789')

Decimal("1.2345 678912345678912 34567891E+35")

Mark
--
Ethan

Jul 8 '08 #4


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__.__n eg__(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

Jul 8 '08 #5
Terry Reedy wrote:
>

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__.__n eg__(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
Jul 8 '08 #6
On Jul 7, 4:12*pm, Ethan Furman <et...@stonelea f.uswrote:
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.2345 6")
>>getcontext(). prec = 5
+t
Decimal("1.2346 ")
>
Thanks for the feedback.
--
Ethan
casevh
Jul 9 '08 #7
On Jul 8, 12:34*pm, Ethan Furman <et...@stonelea f.uswrote:
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).
Jul 9 '08 #8
Ethan Furman <et***@stonelea f.uswrote:
>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.gr eenend.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
Jul 9 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1678
by: Chris | last post by:
Is there any way to make the class Z behave the same way as class Y? Chris class Y: value = 42 def __hasattr__(self, name): if name == '__int__': return True def __getattr__(self, name):
3
3909
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't aware of. One of them is the unary plus operator, that calls the __pos__ method. It's something that can be highly useful in my own experiments with the use of classes as a tool for generic declarative descriptions of objects -- UI forms,...
1
1418
by: John Dell'Aquila | last post by:
I want to trap numeric operators (+ * % etc.) so that my new style classes can handle them generically, similar to __getattr__ in an old style class. I've never done any metaprogramming before but I'm thinking of using the below metaclass to accomplish this. It reads a class variable, implements_operators, to determine which methods handle various operators, then inserts closures to call the generic methods with disambiguating...
0
1220
by: Pam Sheldon | last post by:
I have an app that uses data files. How can get the files over to the emulator so I can debug the app in the emulation mode? Microsoft does not say this is a limitation of the emulator. Pam
3
2061
by: Gilles Cadorel | last post by:
I'd like to add in a HTML page a button, that open a Unix Emulation on a new Windows, when I clik on it. I'm using WRQ Reflection to connect to Unix hosts. The problem is that I don't want the emulation be opened in Internet Explorer, but the real programm (r1win.exe in this case) to be opened on a new Windows. Can anybody help me ? Gilles
3
4348
by: ShadowMan | last post by:
Hi all I've seen a lot of posts about frames emulation. I would like to emulate a: header, left-side menu, content, footer layout. I need to be cross browser compatible...but I'm get crazy!!! My code is above: it displays right on Firefox 1.0 but it doesn't on IE6. What should I change?! thanx --
0
1257
by: Jose Michael Meo R. Barrido | last post by:
I'm looking for a .net framework component to read write to Serial(Com port) that supports ANSI Emulation.
0
1002
by: Maddog | last post by:
How do I enable Brief emulation as the editor in Visual Studio.Net? I can do it in Visual Studio 6.0.
33
2088
by: bearophileHUGS | last post by:
I have just re-read the list of changes in Python 2.6, it's huge, there are tons of changes and improvements, I'm really impressed: http://docs.python.org/dev/whatsnew/2.6.html I'll need many days to learn all those changes! I can see it fixes several of the missing things/problems I have found in Python in the past, like the lack of information regarding the floating point it uses, etc. I have seen that many (smart) updates are from...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.