Re: [Pyobjc-dev] NSDecimalNumber to the next level
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2004-03-10 06:50:19
|
On 9-mrt-04, at 19:29, Pierce T.Wetter III wrote: >>> I want to take NSDecimalNumber to the next level. >>> >>> History: >>> >>> 1.0. >>> >>> NSDecimalNumber is wrapped as identical to CFNumber, which turns >>> out not work. >>> >>> 1.1 >>> >>> NSDecimalNumbers are passed through untransformed, you can use: >>> >>> dv=obj.doubleValue() to convert them to Python doubles, then: >>> >>> NSDecimalNumber.numberWithDouble(dv) to convert them back. This >>> had to happen because the type information was getting lost, and the >>> values passed back to Objective-C were plain CFNumber classes, which >>> didn't implement NSDecimalNumber.decimalNumberByAdding_(dv), etc. >> >> The type info should not get lost, NSDecimalNumber and NSDecimal (the >> C type backing the NSDecimalNumber class) should work correctly as >> numbers, including save coercions. I explicitly disabled implicit >> coercion from NSDecimal{Number,} to and from float because you might >> loose precision that way. >> >> If this doesn't work you found a bug that should be fixed. > > Call me crazy, but I'd like to get this code to work: > > import objc > import Foundation > print > Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(Foundation.NSD > ecimalNumber.zero()) > print Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(1.0) > That's what I would like to be able to do. >>> import Foundation >>> Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(Foundation.NSDe cimalNumber.zero()) 0 >>> type(_) <objective-c class NSDecimalNumber at 0xa0a061f4> >>> Foundation.NSDecimalNumber.zero().decimalNumberByAdding_(1.0) 0 The last one is a bug, it should raise TypeError just like: >>> Foundation.NSDecimal(0) + 1.0 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsupported operand type(s) for +: 'Foundation.NSDecimal' and 'float' From what I've seen from NSDecimal is simular to the type described in PEP 327 and the last time I saw discussion about this they seemed to favor explicit conversion from float for various reasons. Ronald -- X|support bv http://www.xsupport.nl/ T: +31 610271479 F: +31 204416173 |