循环-08. 二分法求多项式单根 问题解决

来源:互联网 发布:现在最流行的网络歌曲 编辑:程序博客网 时间:2024/06/10 13:30

因为精度问题总是不能全部通过
最终发现需要setiosflags( ios::fixed )来控制

下面是全部通过代码

#include <iostream>#include <math.h>#include<iomanip> using namespace std;double a3, a2, a1, a0;double fx(double x);int main() {    double x1, x2, mid,ans;    cin >> a3 >> a2 >> a1 >> a0;    cin >> x1 >> x2;    do {        mid = (x2 + x1) / 2;        ans = mid;        if (fx(x1) * fx(x2) == 0) {            if (fx(x1) == 0) {                ans = x1;                break;            }            else {                ans = x2;                break;            }        }        else if (fx(x1) * fx(x2)<0) {            if (fx(mid) == 0) {                ans=mid;                break;            }            else {                if (fx(x1) * fx(mid) > 0) {                    x1 = mid;                    c1 = fabsf(x2 - x1);                    continue;                }                else {                    x2 = mid;                    c1 = (x2 - x1);                    continue;                }            }        }    } while (x2-x1> 0.0001);    cout << setiosflags(ios::fixed) << setprecision(2)<<ans << endl;    return 0;}double fx(double x) {    return a3*pow(x, 3) + a2*pow(x, 2) + a1*x + a0;}

一下转载
http://blog.sina.com.cn/s/blog_5356e47901019vqx.html
setiosflags( ios::fixed ),其头文件为:include.
注:在遇到要计算浮点数且希望能控制其输出、精度、小数点后的位数等时,那么这个时候,用setiosflags( ios::fixed )来控制是再好不过了!且看下面程序:

#include<iostream>#include<cmath>using namespace std;int main() {cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;}

输出结果为:sqrt(2000) = 44.7214. 那么也就是说编译器的默认精度为小数点后4位。那么如果我想让其小数点后精度为1位、2位、3位或20位,该怎么办呢?来,这么试试:
例一:

#include<iostream>#include<iomanip>#include<cmath>using namespace std;int main() {cout << setprecision( 1 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为4e+001

例二:

int main() {cout << setprecision( 1 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为45

例三:

int main() {cout << setprecision( 3 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为44.7

大家可以明显看到,编译器输出的结果纯粹是无稽之谈!根本不是按照人们的意志去做的!遇到这种比艳门照还尴尬的情况该怎么办呢?不要怕,这时候动动脑子,救星就来啦!它就是人称一朵犁花压海棠的:setiosflags( ios::fixed ) ! 废话少说,拿实例来!
例一:

int main() {cout << setiosflags( ios::fixed ) << setprecision( 1 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为44.7

例二:

int main() {cout << setiosflags( ios::fixed ) << setprecision( 2 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为44.72

例三:

int main() {cout << setiosflags( ios::fixed ) << setprecision( 3 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果为44.721

Apparently, all of the answers are totally correct with any doubt! 结果全部正确,哪怕你来个 setiosflags( ios::fixed ) << setprecision( 1000 ), 结果照样不来半点寒糊!
此外,还要补充一点的是,某些人,当吃饱撑得在床上直打滚儿、嗷嗷叫着难受时,便喜欢多惹出点事非来,他们喜欢在setiosflags()里再加个showpoint,我不知道这样做好是不好,可我感觉它确实有点多余!因为加不加showpoint几乎是没什么区别,如果你也吃多了,肚子涨,胃痛胃酸不消化,非要挑骨头捡刺儿,那也能找出点不一样,就是:当setprecision()的精度为0的时候,你有showpoint,那结果你就会多个点儿,没showpoint,就没有。实例:

int main() {cout << setiosflags( ios::fixed ) << setprecision( 0 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} // 结果 45int main() {cout << setiosflags( ios::fixed|ios::showpoint ) << setprecision( 0 );cout << "sqrt(2000) = " << sqrt( 2000 ) << endl;return 0;} //结果 45.

看到这个点儿没?所以说嘛,showpoint,可有可无!如果想简化程序,那干脆就不加!
最后,再次特别提醒大家,想使用setiosflags或者setprecision时,一定得加头文件,在setiosflags()里面填东西的时候,前面一定得加上域符 ios::。 好啦,这次就是这样,下回再说!

0 0
原创粉丝点击