|
강도욱 님이 쓰신 글 :
: #include <stdio.h>
: #include <math.h>
: #include <stdlib.h>
:
: double f(double a, double b, double c, double x)
: {
: return (a*pow(x,2.0) + b*x + c);
제곱을 하는 데 굳이 pow() 함수를 쓰는 것은 속도도 느리고 읽기도 않좋네요.
그냥 간단하게
return a*x*x + b*x + c;
로 하면 되겠죠.
: }
참고로, 볼랜드/터보 C에서는 poly()란 함수를 써서 임의 차수의 다항식 계산을 할 수 있습니다.
사용법은 다음과 같습니다.
double f(double a, double b, double c, double x)
{
int coeffs[2 + 1];
coeffs[2] = a; coeffs[1] = b, coeffs[0] = c;
return poly(x, 2, coeffs);
}
: int main(void)
: {
: double a,b,c,x;
: a = 2.0;
: b = 3.0;
: c = 4.0;
: x = 2.0;
:
: printf("\n%.1f*%.1f^2 + %.1f*%.1f + %.1f = %.1f\n",a,x, b, x, c, f(a, b, c, x));
: system("pause");
: return 0;
: }
|