 |
 |
Newbie cocoa question: how to evalulate math expression
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Sep 2001
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|