Re: [Pyobjc-dev] NSDecimalNumber to the next level
Brought to you by:
ronaldoussoren
From: Pierce T.W. I. <pi...@tw...> - 2004-03-10 15:18:57
|
On Mar 9, 2004, at 11:55 PM, Ronald Oussoren wrote: > > On 10-mrt-04, at 7:41, Ronald Oussoren wrote: >> >>> Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(1.0) >> 0 >> >> The last one is a bug, it should raise TypeError just like: > > The problem seems to be in NSDecimalNumber not PyObjC: > > >>> Foundation.NSDecimalNumber.zero().decimalNumberByAdding_("a") > 0 Oh, don't get me started on NSDecimalNumber's failings. Here are some: [[NSDecimalNumber zero] decimalNumberByAdding: nil]; <--- bus error [[NSDecimalNumber zero] decimalNumberByAdding: [NSNumber numberWithInt:1]]; <-- bus error Basically, NSDN dies if you do math with anything but another NSDN, which is irritating because you'd really like NSDN to be a full member of the NSNumber class cluster. However, I don't get to change NSDN. I can change PyObjC, so I'd like to change it so that it knows how to build an NSDN when it needs one. I'm not overly worried about the loss of precision, because if you're doing stuff in Python, then you should know that you're doing it with Python arithmetic, which is based on double. For me (I realize this isn't everyone) I use Python to write mini-tools based on our object model frameworks. So I want convenience more then accuracy. So of course, the ideal would be that if you do math in Python, it implicitly converts it. If you don't do the math in python, it doesn't. Note that this wouldn't involve any loss of precision for instance: Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(1) Because the bridge would build the 1 as an NSDecimalNumber. This would: z=Foundation.NSDecimalNumber.zero() z= z+1 But that seems OK to me because z is now a python variable, and python variables are doubles. If you don't want the loss of precision when you move to Python, you should use some sort of Python class that supports larger precision and do the math in that. Anyways, that's my $.02. (Does "That's my two cents translate to .nl?) So anyways, is it possible for the bridge to look at the required class for a parameter, and do different conversions? That is, if I'm calling decimalNumberByAdding_(x) can the bridge tell that it needs an NSDecimalNumber * and so it needs to do conversions where necessary? Pierce |