Using MAPM with Cocoa/ObjectiveC
Using MAPM with Cocoa/ObjectiveC
- Subject: Using MAPM with Cocoa/ObjectiveC
- From: Martin Stanley <email@hidden>
- Date: Thu, 2 Dec 2010 23:36:47 -0500
I am in need of an arbitrary precision library for use in an iPhone app. I downloaded MAPM (http://www.tc.umn.edu/~ringx004/mapm-main.html) and compiled it successfully, but am now running into some very strange errors. Things like commenting out a line of debugging code cause an "EXC_BAD_ACCESS" error 2 lines earlier than the commented code! (This reminds me of the bad/good old days when we had good reason to suspect the compiler in situations like this!)
The code is posted below. As it is written, the code returns the expected result. However, if the second NSLog statement is commented and the first un-commneted the code crashes with "EXC_BAD_ACCESS" and the debugger shows the PC at the line: NSString *str = [NSString stringWithFormat:@"%s", out3];
It feels like some kind of memory corruption or perhaps thread issue. MAPM is not thread-safe, but as far as I know I am not using multiple threads. All of the code that calls the library is contained in a single method. However, this method is in an extension to NSDecimalNumber that I have created. Could this be the source of the problem?
Does anyone have a suggestion on what to try next?
Has anyone used MAPM from a Cocoa app?
Thanks in advance,
Martin
------------
@implementation NSDecimalNumber (TVExtensions)
- (NSDecimalNumber *)decimalNumberByRaisingToDecimalPower:(NSDecimalNumber *)decimalPower {
M_APM mapm1;
M_APM mapm2;
M_APM mapm3;
mapm1 = m_apm_init();
mapm2 = m_apm_init();
mapm3 = m_apm_init();
// set values from C-string
m_apm_set_string(mapm1, (char *)[[self description] UTF8String]); // cast to avoid compiler complaints about const
m_apm_set_string(mapm2, (char *)[[decimalPower description] UTF8String]);
// compute result
m_apm_pow(mapm3, DECIMAL_PLACES, mapm1, mapm2);
// convert to NSDecimalNumber for return value
int digits3 = m_apm_significant_digits(mapm3);
char out3[digits3 + 2]; // add 1 for the sign and 1 for the decimal point
m_apm_to_string(out3, DECIMAL_PLACES, mapm3);
NSString *str = [NSString stringWithFormat:@"%s", out3];
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:str];
// NSLog(@"num is: %@", num);
NSLog(@"num is: %@ (%@)", num, [num class]);
m_apm_free(mapm1);
m_apm_free(mapm2);
m_apm_free(mapm3);
return num;
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden