PDA

View Full Version : Math Not working




AGWright
Aug 27, 2012, 06:27 AM
Hi

if I use this (Hardcoding MySum)

NSString *mySum = @"(10+10)+25*7";
NSNumber *result = [mySum numberByEvaluatingString];
MyAnswerS.text = [NSString stringWithFormat:@"%@",result];

the text box correctly displays 195 :)

while if I build up a string to be the same @"(10+10)+25*7"
I get the answer of 70 :(

NSString *mySum = [NSString stringWithFormat:@"%@",MyAnswer];
NSNumber *result = [mySum numberByEvaluatingString];
MyAnswerS.text = [NSString stringWithFormat:@"%@",result];

debugging MyAnswer shows @"(10+10)+25*7"

any Ideas

Best Regards



jnoxx
Aug 27, 2012, 07:11 AM
Why do you use strings instead of NSNumbers/Ints/Floats?

balamw
Aug 27, 2012, 07:14 AM
Why do you use strings instead of NSNumbers/Ints/Floats?

A bit of context would definitely be helpful, but I suspect the OP is trying to make an app where the user can enter an arbitrary formula and have it evaluated.

B

AGWright
Aug 27, 2012, 07:42 AM
A bit of context would definitely be helpful, but I suspect the OP is trying to make an app where the user can enter an arbitrary formula and have it evaluated.

B

Yep sorry the user get a series of numbers and have to make it add up to a target
I.e target = 195
Sum = (10+10) + 25 * 7

balamw
Aug 27, 2012, 07:51 AM
Break the problem down. Does it work consistently for a simpler expression, e.g. 2+2?

B

AGWright
Aug 27, 2012, 08:19 AM
Break the problem down. Does it work consistently for a simpler expression, e.g. 2+2?

B

yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9

balamw
Aug 27, 2012, 09:38 AM
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9

What does it do for 9*10? How about (10*9)?

B

dejo
Aug 27, 2012, 09:40 AM
What are MyAnswer and MyAnswerS?

chown33
Aug 27, 2012, 09:42 AM
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9

Break it down more. Run more tests, like 9*12 or 32*9 or 200*9, or put a space before 12*9.

And when you do these tests, post the code for them, as well as the results. Your first post failed to show the code that builds the string. Seeing the string-builder code might be a valuable clue. In addition, someone else could actually compile and test the code themselves, rather than you being the only one who can do it.

Also please tell us where you got the code that adds the numberByEvaluatingString capability. AFAICT it's not a standard NSString method. Sometimes people get older versions of third-party libraries, and see bugs that have been fixed in newer versions.

AGWright
Aug 27, 2012, 09:56 AM
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9

found out that replacing any spaces worked (why it was putting spaces in who knows)

stringByReplacingOccurrencesOfString:@" " withString:@""];

now working :) thank to all who answered :)

balamw
Aug 27, 2012, 10:02 AM
debugging MyAnswer shows @"(10+10)+25*7"


So were there embedded spaces in that? Where? Can you copy and paste the actual faulty input.

B

Duncan C
Aug 27, 2012, 10:14 AM
Hi

if I use this (Hardcoding MySum)

NSString *mySum = @"(10+10)+25*7";
NSNumber *result = [mySum numberByEvaluatingString];
MyAnswerS.text = [NSString stringWithFormat:@"%@",result];

the text box correctly displays 195 :)

while if I build up a string to be the same @"(10+10)+25*7"
I get the answer of 70 :(

NSString *mySum = [NSString stringWithFormat:@"%@",MyAnswer];
NSNumber *result = [mySum numberByEvaluatingString];
MyAnswerS.text = [NSString stringWithFormat:@"%@",result];

debugging MyAnswer shows @"(10+10)+25*7"

any Ideas

Best Regards


Where does teh numberByEvaluatingString method come from? I don't see it in any of Apple's frameworks. Searching the net, I found something called DDMathParser. Is that it?

For future reference, its a REALLY good idea to list any third party libraries that you are using, along with a link to info on those libraries. Otherwise, we have no idea what you are using.

You should also provide a brief summary of those libraries, what functions they provide, and how you think they are supposed to work.

e.g.:

The function numberByEvaluatingString comes from a library called DDMathParser that I found on github <link>. It lets me take formulas as strings and evaluate them as numeric expressions.

AGWright
Aug 27, 2012, 10:40 AM
sorry the link is https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/NSString+DDMathParsing.m

very useful :)