Re: [Pyobjc-dev] NSDecimalNumber to the next level
Brought to you by:
ronaldoussoren
From: Pierce T.W. I. <pi...@tw...> - 2004-03-10 16:18:37
|
> > I completely forgot to mention that the usual numeric operators do > work correctly, as long as you manually convert floats to NSDecimal, > e.g. ``NSDecimalNumber.zero() + 4`` is valid code and will do what you > want. Hmmm.... That must have changed, because I submitted a change which you accepted awhile back to not implicitly convert. Checking... >>> print type(Foundation.NSDecimalNumber.zero()) <type 'float'> Arrgh, its converting again. That works because Python converted it implicitly to a float. (Which is actually kind of surprising, since I thought it had stopped doing that. See comment for (Bug #831774) in objc/objc_support.m) This code doesn't work now though: >>> print Foundation.NSDecimalNumber.numberWithDouble_(3.3).decimalNumberByAdding_ (Foundation.NSDecimalNumber.numberWithDouble_(4.4))Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'float' object has no attribute 'decimalNumberByAdding_' Again, because of the implicit float conversion, which then can't convert back. > Automaticly converting floats to NSDecimal is easy enough, it is a > couple of extra lines in Modules/Foundation/decimal.m (I'm not 100% > sure about the filename, but it should be close). Ok, I'll poke around and see what I can figure out then. I'll have to think about all the use cases anyways, since what I really want is: case 1: do math in python with NSDN without having to call doubleValue() print Foundation.NSDecimalNumber.zero()+4 case 2: yet be able to call NSDN methods? print Foundation.NSDecimalNumber.numberWithDouble_(3.3).decimalNumberByAdding_ (Foundation.NSDecimalNumber.numberWithDouble_(4.4)) case 3: and pass NSDN back to objc object.setDecimalNumberValue_(4.4) (call objc method that takes an NSDN) To all work. Hmm... You know, really, since NSDN is a superset of NSNumber, if PyObjC always passed NSDecimalNumbers back to objc instead of NSNumbers, things would mostly work. I couldn't do case 2, but I'm not sure I'd care then. Though being able to do: Foundation.NSDecimalNumber.numberWithDouble(3.3).decimalNumberByMultiply ingBy_withBehavior_(3.4, pennyRounding) Is useful. Hmmm.. Adding all the NSDecimalNumber methods to the Python float type would work then. Arrgh. Brain hurts now. Need more coffee. Pierce |