多项式求和
。用for循环可以方便求得多项式y的值。使用两次循环展开,两路并行。可以提高性能。
程序代码
float ploynew(float a[], float x, int degree);void main()
{float a[5]={1.0, 2.0, 1.5, 2.5,1.5}; //系数数组static float x=2.6;float y; //存放多项式的和值int degree=4; //多项式的次数y=ploynew(a, x, degree); while(1);
}
float ploynew(float a[], float x, int degree){int i;int limit=degree-1;float result1=a[0],result2=0;float xpwr1=x,xpwr2=x*x;for(i=1; i<=limit; i+=2){result1 += a[i]*xpwr1; xpwr1 *= x*x;result2 += a[i+1]*xpwr2;xpwr2 *= x*x; }for(;i<=degree;i++)result1 += a[i]*xpwr1;return result1+result2;}
y=128.83。