Horner's rule for polynomial evaluation: Difference between revisions

(added php)
Line 546:
 
=={{header|Objective-C}}==
{{works with|Mac OS X|10.6+}} Using blocks
<lang objc>#import <Foundation/Foundation.h>
 
typedef double (*^mfunc)(double, double, double);
 
double accumulateFunc(double s, double x, double a)
{
return s * x + a;
}
 
@interface NSArray (HornerRule)
- (double)horner: (double)x;
- (NSArray *)reversedArray;
- (double)injectDouble: (double)s xValue: (double)x with: (mfunc)op;
@end
 
Line 568 ⟶ 564:
 
 
- (double)injectDouble: (double)s xValue: (double)x with: (mfunc)op
{
double sum = s;
NSEnumerator for(NSNumber*e =el in [self) objectEnumerator];{
sum = op(sum, x, [el doubleValue]);
id el;
while( (el = [e nextObject]) != nil) {
sum = op(sum, x, [el doubleValue]);
}
return sum;
Line 581 ⟶ 575:
- (double)horner: (double)x
{
return [[self reversedArray] injectDouble: 0.0 xValue: x with: ^(mfuncdouble s, double a)accumulateFunc { return s * x + a; } ];
}
@end
Line 595 ⟶ 589:
[NSNumber numberWithDouble: -4.0],
[NSNumber numberWithDouble: 6.0], nil];
printf("%lff\n", [coeff horner: 3.0]);
 
[pool release];
Anonymous user