Re: extracting the mantissa for a NSDecimal
So to avoid the endian quagmire, I should create an NSDecimalNumber from my NSDecimal and add that to my dictionary. NSDecimal pi = ... // 3.1415927... NSDecimalNumber *piNSD = [NSDecimalNumber decimalNumberWithDecimal:pi]; I plan to write a pair of apps that saves NSDecimal values to a file and retrieves them. I plan to test these apps on modern and legacy hardware to verify the endian question. But for now, that is a side-track for me. Has anyone already done such a test? ++ Tom Tom Bernard tombern...@bersearch.com on 11/20/09 1:02 PM, Greg Guerin wrote: Message: 6 Date: Fri, 20 Nov 2009 10:53:35 -0700 From: Greg Guerin Subject: Re: extracting the mantissa for a NSDecimal Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Tom Bernard wrote: NSData *anNSDecimalAsData = [NSData dataWithBytes:anNSDecimal length:sizeof(NSDecimal)]; gives you an NSData object suitable for an NSDictionary without having to muck around with NSDecimal's private fields. The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. on 11/20/09 1:02 PM, Jens Alfke wrote: Message: 8 Date: Fri, 20 Nov 2009 10:49:27 -0800 From: Jens Alfke Subject: Re: extracting the mantissa for a NSDecimal To: Greg Guerin Content-Type: text/plain; charset=WINDOWS-1252; format=flowed; delsp=yes On Nov 20, 2009, at 9:53 AM, Greg Guerin wrote: The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. s/may be/is/ s/could be/will be/ NSDecimal is a struct containing multi-byte integers. It is definitely endian-sensitive. on 11/20/09 1:02 PM, Gary L. Wade wrote: Message: 11 Date: Fri, 20 Nov 2009 11:33:30 -0800 From: Gary L. Wade garyw...@desisoftsystems.com Subject: Re: extracting the mantissa for a NSDecimal To: Jens Alfke, Greg Guerin Content-Type: text/plain;charset=US-ASCII On 11/20/2009 10:49 AM, Jens Alfke j...@mooseyard.com wrote: On Nov 20, 2009, at 9:53 AM, Greg Guerin wrote: The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. s/may be/is/ s/could be/will be/ NSDecimal is a struct containing multi-byte integers. It is definitely endian-sensitive. I haven't been following this closely, but NSDecimalNumber both takes an NSDecimal and conforms to the NSCoding Protocol on 11/20/09 10:14 AM, Roland King wrote: Message: 5 Date: Fri, 20 Nov 2009 20:32:05 +0800 From: Roland King Subject: Re: extracting the mantissa for a NSDecimal To: Tom Bernard Content-Type: text/plain; charset=us-ascii I don't know if he ever did get an answer however your method of putting it in an NSData is a much better idea and the original question sort of missed the point. Yes there is a constructor for NSDecimalNumber which allows you to give a mantissa and an exponent, but that doesn't mean the number is stored in a way which lets you get those out again. For instance mantissa 123 with exponent 3 is the same number as 123000 with exponent 0. It's true that it would be possible for NSDecimalNumber to have an instance method which returned one possible combination of mantissa, exponent and sign which represented the number, but it doesn't, and you really shouldn't need it. it's a little like using [ stringWithFormat:@%...@%@ string1, string2, nil ] to make a string and then expecting there's a method you can call later which gets you string1 and string2 back, but of course by then you just have the resulting string, you can't go backwards. There is -(NSDecimal)decimal method which returns a structure with mantissa/exponent etc however the fields in that are explicitly declared to be private, so you really shouldn't use it. You really need to treat it as a black box if you wish to save and restore it. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: extracting the mantissa for a NSDecimal
Hello Olivier, I am working with NSDecimals and ran across your 7 year old post in Cocoa-dev. Did you ever get an answer to your mantissa question? The archives do not show a response to the thread. Btw, did you consider NSData's +dataWithBytes:length: method? NSData *anNSDecimalAsData = [NSData dataWithBytes:anNSDecimal length:sizeof(NSDecimal)]; gives you an NSData object suitable for an NSDictionary without having to muck around with NSDecimal's private fields. ++ Tom Tom Bernard tombern...@bersearch.com --__--__-- Message: 11 Date: Tue, 29 Oct 2002 12:37:35 -0600 Subject: extracting the mantissa for a NSDecimal From: Olivier ... To: cocoa apple ... i'm trying to save an NSDecimal in a dictionary. I study the NSDecimal structure and decided that all i really need to save are: _exponent, _isNegative and _mantissa field i'm trying to get the mantissa field, i want to store it as a NSNumber in my dictionary. the length field of the NSDecimal tells how many short of the mantissa are relevant so i'm building my data this way: NSMutableData *theNumberData = [NSMutableData dataWithLength:16]; for (i = 0; i valueOfObject._length; i++) { [theNumberData replaceBytesInRange: NSMakeRange(16 - (i+1) * 2, 2) withBytes: valueOfObject._mantissa[i]]; } so that the relevant field are put in the data object starting from the end of the object (most significant bytes first) then start the problem, converting it to an NSNumber i tried: const char *theNumberPtr = [theNumberData bytes]; [NSNumber numberWithUnsignedLongLong: *theNumberPtr] [NSNumber numberWithUnsignedLongLong: theNumberPtr[0]] [NSNumber numberWithUnsignedLongLong: theNumberPtr] without any success. Any idea of how to get that mantissa in a NSNumber? Olivier --__--__-- ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: extracting the mantissa for a NSDecimal
I don't know if he ever did get an answer however your method of putting it in an NSData is a much better idea and the original question sort of missed the point. Yes there is a constructor for NSDecimalNumber which allows you to give a mantissa and an exponent, but that doesn't mean the number is stored in a way which lets you get those out again. For instance mantissa 123 with exponent 3 is the same number as 123000 with exponent 0. It's true that it would be possible for NSDecimalNumber to have an instance method which returned one possible combination of mantissa, exponent and sign which represented the number, but it doesn't, and you really shouldn't need it. it's a little like using [ stringWithFormat:@%...@%@ string1, string2, nil ] to make a string and then expecting there's a method you can call later which gets you string1 and string2 back, but of course by then you just have the resulting string, you can't go backwards. There is -(NSDecimal)decimal method which returns a structure with mantissa/exponent etc however the fields in that are explicitly declared to be private, so you really shouldn't use it. You really need to treat it as a black box if you wish to save and restore it. On 20-Nov-2009, at 7:43 PM, Tom Bernard wrote: Hello Olivier, I am working with NSDecimals and ran across your 7 year old post in Cocoa-dev. Did you ever get an answer to your mantissa question? The archives do not show a response to the thread. Btw, did you consider NSData's +dataWithBytes:length: method? NSData *anNSDecimalAsData = [NSData dataWithBytes:anNSDecimal length:sizeof(NSDecimal)]; gives you an NSData object suitable for an NSDictionary without having to muck around with NSDecimal's private fields. ++ Tom Tom Bernard tombern...@bersearch.com --__--__-- Message: 11 Date: Tue, 29 Oct 2002 12:37:35 -0600 Subject: extracting the mantissa for a NSDecimal From: Olivier ... To: cocoa apple ... i'm trying to save an NSDecimal in a dictionary. I study the NSDecimal structure and decided that all i really need to save are: _exponent, _isNegative and _mantissa field i'm trying to get the mantissa field, i want to store it as a NSNumber in my dictionary. the length field of the NSDecimal tells how many short of the mantissa are relevant so i'm building my data this way: NSMutableData *theNumberData = [NSMutableData dataWithLength:16]; for (i = 0; i valueOfObject._length; i++) { [theNumberData replaceBytesInRange: NSMakeRange(16 - (i+1) * 2, 2) withBytes: valueOfObject._mantissa[i]]; } so that the relevant field are put in the data object starting from the end of the object (most significant bytes first) then start the problem, converting it to an NSNumber i tried: const char *theNumberPtr = [theNumberData bytes]; [NSNumber numberWithUnsignedLongLong: *theNumberPtr] [NSNumber numberWithUnsignedLongLong: theNumberPtr[0]] [NSNumber numberWithUnsignedLongLong: theNumberPtr] without any success. Any idea of how to get that mantissa in a NSNumber? Olivier --__--__-- ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/rols%40rols.org This email sent to r...@rols.org ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: extracting the mantissa for a NSDecimal
Tom Bernard wrote: NSData *anNSDecimalAsData = [NSData dataWithBytes:anNSDecimal length:sizeof(NSDecimal)]; gives you an NSData object suitable for an NSDictionary without having to muck around with NSDecimal's private fields. The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. -- GG ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: extracting the mantissa for a NSDecimal
On Nov 20, 2009, at 9:53 AM, Greg Guerin wrote: The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. s/may be/is/ s/could be/will be/ NSDecimal is a struct containing multi-byte integers. It is definitely endian-sensitive. —Jens___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: extracting the mantissa for a NSDecimal
On 11/20/2009 10:49 AM, Jens Alfke j...@mooseyard.com wrote: On Nov 20, 2009, at 9:53 AM, Greg Guerin wrote: The representation stored in the NSData may be endian-sensitive. If the NSDictionary contents might cross architectures, that could be problematic. s/may be/is/ s/could be/will be/ NSDecimal is a struct containing multi-byte integers. It is definitely endian-sensitive. I haven't been following this closely, but NSDecimalNumber both takes an NSDecimal and conforms to the NSCoding Protocol. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com