Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 10:08 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 8
Cori is on a distinguished road
Smile Is there a function that translates a string into math equation?

Hi, I'm new to Objective-C and I'm looking for a way to do a quick arithmetic operation given a specific equation, in string.

For example:
NSString *eq1 = @"10+20-30*40/50";
NSString *eq2 = @"(10+20)-30*40/50";

double total = MATH_FUNCTION(eq1) + MATH_FUNCTION(eq2);

Is there a MATH_FUNCTION that can translate a string into a math equation? And also apply the arithmetic rules of MDAS when processing the operands inside the string?

Thanks for reading and answering!! :-)
Cori is offline   Reply With Quote
Old 09-07-2011, 11:45 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 68
mokargas is on a distinguished road
Default

Quote:
Originally Posted by Cori View Post
Hi, I'm new to Objective-C and I'm looking for a way to do a quick arithmetic operation given a specific equation, in string.

For example:
NSString *eq1 = @"10+20-30*40/50";
NSString *eq2 = @"(10+20)-30*40/50";

double total = MATH_FUNCTION(eq1) + MATH_FUNCTION(eq2);

Is there a MATH_FUNCTION that can translate a string into a math equation? And also apply the arithmetic rules of MDAS when processing the operands inside the string?

Thanks for reading and answering!! :-)
iphone - NSString to equation - Stack Overflow
mokargas is offline   Reply With Quote
Old 09-09-2011, 06:36 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 8
Cori is on a distinguished road
Smile

Thanks for helping, Mokargas! I like you!!

I was excited to find the NSPredicate solution but unfortunately, it yields wrong calculations when it's division and the dividend and divisor are both integers, for example:

NSString *str7 = @"((10+(25/22))*10)"; // shd be 111.36364, not 110
NSString *str8 = @"((10+(25/22))*10.111)"; // shd be 112.59977, not 111.221
NSString *str9 = @"((10+(25/22.0))*10.111)"; // the fix to get 112.59977

NSString *equation = @"1+5*6";
// dummy predicate that contains our expression
NSPredicate *pred = [NSPredicate predicateWithFormat:
[equation stringByAppendingString:@" == 42"]];
NSExpression *exp = [pred leftExpression];
NSNumber *result = [exp expressionValueWithObject:nil context:nil];
NSLog(@"%@", result); // logs "31"

Apparently the float result is always converted to integer. So for workaround, I replaced the string's occurrences of @"/" with @"*1.0/" though I'm not sure that is a reliable solution.

Few days ago I came across a short doc on Tcl Math Library, that mentioned a linear algebra pkg, like below:
This manual page is for Mac OS X version 10.6.6


math(n) Tcl Math Library
NAME
math - Tcl Math Library
SYNOPSIS
package require Tcl 8.2
package require math ?1.2.4?
//... text edited out…
• math::geometry - 2D geometrical computations
• math::interpolate - Various interpolation methods
• math::linearalgebra - Linear algebra package
• math::optimize - Optimization methods
//... text edited out…
The thing is I have no idea if I can use this Tcl math lib for an iPhone app, nor do I know how to find this lib. I'm also wondering it has a good docu about that linear algebra.
Cori is offline   Reply With Quote
Reply

Bookmarks

Tags
math function, string to math equation

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 384
8 members and 376 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:55 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0