函数的!苦逼自学党! 求帮忙!谢谢!

跳舞的傻子 2014-06-23 03:21:56
这是书上的 用弦截法求方程 x ∧3 - 5x ∧2 + 16x - 80 = 0的根


然后就是
# include <stdio.h>
# include <math.h>

float f(float x)// 用于求f(x) = x 3 - 5x 2 + 16x - 80的值!
{
float y;
y = ((x - 5.0) * x + 16.0) * x - 80.0;//
return (y);
}

float xpoint (float x1, float x2)//用于求 弦与x轴的交点
{
float y;
y = (x1*f(x2)) - x2*f(x1) / (f(x2)-f(x1));
return (y);
}

float root (float x1, float x2)// 求近似值
{
int i;
float x, y, y1;
y1 = f(x1);

do
{
x = xpoint (x1, x2);
y = f(x);
if (y*y1 > 0)
{
y1 = y;
x1 = x;
}
else
x2 = x;
}
while (fabs (y) >= 1E-6);//?????????????这里的 fabs (y)>= 1E-6 是什么意思??
//???????fabs (y) 是系统函数???还是什么??

return (x);
}

int main(void)
{
float x1, x2,f1, f2, x;
do
{
printf ("input x1, x2:");
scanf ("%f %f", &x1, &x2);
f1 = f(x1);
f2 = f(x2);
}
while (f1*f2 >= 0);
x = root(x1, x2);
printf ("A root of equation is %8.4f\n", x);//A root of equation is 一根方程

return 0;
}



书上 是 输入2,6
输出 5.00000


可我是输出
input x1, x2:2 6
A root of equation is -1.#IND
Press any key to continue


无语了 书上是用TC跑的

我是用vc6.0 跑的

中间就改了一下

但大体意思不变!

但为毛 就错了??
还有求大神 帮忙解释一下
while (fabs (y) >= 1E-6);//?????????????这里的 fabs (y)>= 1E-6 是什么意思??
//???????fabs (y) 是系统函数???还是什么??

谢谢!~
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-23
  • 打赏
  • 举报
回复
mk:@MSITStore:C:\MSDN98\98VS\2052\vclang.chm::/html/_pluslang_c.2b2b_.floating.2d.point_constants.htm C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. Syntax floating-constant : fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt fractional-constant : digit-sequenceopt . digit-sequence digit-sequence . exponent-part : e signopt digit-sequence E signopt digit-sequence sign : one of + – digit-sequence : digit digit-sequence digit floating-suffix :one of f l F L Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example: 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example: 18.46e0 // 18.46 18.46e1 // 184.6 If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0. Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively. Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like void func( double ); and void func( long double );
赵4老师 2014-06-23
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt_fabs.htm fabs Calculates the absolute value of the floating-point argument. double fabs( double x ); Function Required Header Compatibility fabs <math.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value fabs returns the absolute value of its argument. There is no error return. Parameter x Floating-point value Example /* ABS.C: This program computes and displays * the absolute values of several numbers. */ #include <stdio.h> #include <math.h> #include <stdlib.h> void main( void ) { int ix = -4, iy; long lx = -41567L, ly; double dx = -3.141593, dy; iy = abs( ix ); printf( "The absolute value of %d is %d\n", ix, iy); ly = labs( lx ); printf( "The absolute value of %ld is %ld\n", lx, ly); dy = fabs( dx ); printf( "The absolute value of %f is %f\n", dx, dy ); } Output The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -3.141593 is 3.141593 Floating-Point Support Routines See Also abs, _cabs, labs
brookmill 2014-06-23
  • 打赏
  • 举报
回复
楼主得学会自己找资料、自己调试代码、自己查错误。当然刚开始可能有点难,代码写多了慢慢就会了。
brookmill 2014-06-23
  • 打赏
  • 举报
回复
这里的公式和方程我都不懂,但是我把这一行的括号挪了个位置,就输出5.0000了,不知道这样对不对 y = (x1*f(x2)) - x2*f(x1) / (f(x2)-f(x1)); 改成 y = (x1*f(x2) - x2*f(x1)) / (f(x2)-f(x1));
brookmill 2014-06-23
  • 打赏
  • 举报
回复
vc6.0和TC都是老掉牙的东西了,尽量远离。 fabs是math.h里的函数,求绝对值的。这种问题楼主要学会自己查手册或者google,总这么提问不是办法。 while (fabs (y) >= 1E-6); 实际上相当于 while(y!=0),但是浮点数的二进制表示有误差,不能直接与0比较,所以只要它的绝对值大于0.000001就认为不是0 参考:http://bbs.csdn.net/topics/80345307 “中间就改了一下”很可能就会造成问题,只能想办法调试,或者和书上的代码对比分析

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧