Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Newbie cocoa question: how to evalulate math expression

Newbie cocoa question: how to evalulate math expression
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2001
Status: Offline
Reply With Quote
Oct 31, 2003, 06:02 PM
 
Hello,
I began tinkering with x-code last night. I am trying to do some math from some fields and display a result in another field. I got it to display one field plus another, but making a more complex math expression yields only operand errors

I tried without success:

[ddisplay setFloatValue:[avalue floatValue] * [bvalue floatValue] + [avalue floatvalue] * [cvalue floatValue] * [cvalue floatValue]];

Anyone help would be appreciated!
     
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Oct 31, 2003, 06:37 PM
 
Looks like an operator precedence issue. How about adding some parenthesis?
Code:
[ddisplay setFloatValue: ( [avalue floatValue] * [bvalue floatValue] + [avalue floatvalue] * [cvalue floatValue] * [cvalue floatValue] ) ];
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Oct 31, 2003, 06:56 PM
 
Originally posted by MajorMatt:

[ddisplay setFloatValue:[avalue floatValue] * [bvalue floatValue] + [avalue floatvalue] * [cvalue floatValue] * [cvalue floatValue]];
Notice in the bold code, you didn't capitalize your floatValue call. I'm guessing that you also got an error about being unable to find the method and another about returning id.

Hope this helps,
Matt Fahrenbacher
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 31, 2003, 07:18 PM
 
A nitpick, but no reason you shouldn't know this:

a*(b+c) is faster to compute than a*b + a*c
(1 mul, 1 add vs 2 mul, 1 add)

so in your case, factor out the [avalue floatValue] -- you won't notice a speed difference, but if anything, it's shorter to write
     
Fresh-Faced Recruit
Join Date: Sep 2001
Status: Offline
Reply With Quote
Oct 31, 2003, 08:05 PM
 
Thanks, guys.


I made a build, it works but not as standalone as there is this 15 meg build folder?

I notice the compile option is grayed out and I suspect that is what I need.
     
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Nov 2, 2003, 12:04 PM
 
Originally posted by 00101001:
A nitpick, but no reason you shouldn't know this:

a*(b+c) is faster to compute than a*b + a*c
(1 mul, 1 add vs 2 mul, 1 add)

so in your case, factor out the [avalue floatValue] -- you won't notice a speed difference, but if anything, it's shorter to write
To extend this, any time you have a polynomial expression, it's nest to factor it like this, both for speed and numerical stability. I.e., rather than:
y = 3*x*x*x*x + 5*x*x*x + 2*x*x + 6*x + 7
instead write
y = (((3*x + 5)*x + 2)*x + 6)*x + 7

This is known as Horner's method, I believe. I once sped a (math-intensive fractal) program up by about 10x using this technique.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 07:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2