| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 838 人关注过本帖, 1 人收藏
标题:C语言的精度问题怎么解决?
只看楼主 加入收藏
Sornets
Rank: 2
等 级:论坛游民
帖 子:102
专家分:61
注 册:2012-11-22
结帖率:58.82%
收藏(1)
已结贴  问题点数:20 回复次数:12 
C语言的精度问题怎么解决?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int count=0;
    double x1,x2,a,b,c,san,A,B;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    for (;;)
    {
        scanf("%lf",&a);
        count++;
        if (a==0)
        {
            return 0;
        }
        else
        {
            scanf("%lf %lf",&b,&c);
            printf("Case %d :\n",count);
            if(a<0)
            {
                a=-a;
                b=-b;
                c=-c;
            }
            if (c==0)
            {
                if (a==1)
                {
                    if (b==1)
                    {
                        printf("x^2 + x = 0\n");
                    }
                    else if (b==0)
                    {
                        printf("x^2 = 0\n");
                    }
                    else if (b==-1)
                    {
                        printf("x^2 - x = 0\n");
                    }
                    else if (b>0)
                    {
                        printf("x^2 + %lfx = 0\n",b);
                    }
                    else
                    {
                        printf("x^2 - %lfx = 0\n",-b);
                    }
                }
                else
                {
                    if (b==1)
                    {
                        printf("%lfx^2 + x = 0\n",a);
                    }
                    else if (b==0)
                    {
                        printf("%lfx^2 = 0\n",a);
                    }
                    else if (b==-1)
                    {
                        printf("%lfx^2 - x = 0\n",a);
                    }
                    else if (b>0)
                    {
                        printf("%lfx^2 + %lfx = 0\n",a,b);
                    }
                    else
                    {
                        printf("%lfx^2 - %lfx = 0\n",a,-b);
                    }
                }
            }
            else if (c>0)/*c>0*/
            {
                if (a==1)  /*c>0*/
                {
                    if (b==1)
                    {
                        printf("x^2 + x + %lf = 0\n",c);
                    }
                    else if (b==0)
                    {
                        printf("x^2 + %lf = 0\n",c);
                    }
                    else if (b==-1)
                    {
                        printf("x^2 - x + %lf = 0\n",c);
                    }
                    else if (b>0)
                    {
                        printf("x^2 + %lfx + %lf = 0\n",b,c);
                    }
                    else
                    {
                        printf("x^2 - %lfx + %lf = 0\n",-b,c);
                    }
                }
                else /*a!=1 && c>0*/
                {
                    if (b==1)
                    {
                        printf("%lfx^2 + x + %lf = 0\n",a,c);
                    }
                    else if (b==0)
                    {
                        printf("%lfx^2 + %lf = 0\n",a,c);
                    }
                    else if (b==-1)
                    {
                        printf("%lfx^2 - x + %lf = 0\n",a,c);
                    }
                    else if (b>0)
                    {
                        printf("%lfx^2 + %lfx + %lf = 0\n",a,b,c);
                    }
                    else
                    {
                        printf("%lfx^2 - %lfx + %lf = 0\n",a,-b,c);
                    }
                }
            }
            else /*c<0*/
            {
                if (a==1)  /*c<0*/
                {
                    if (b==1)
                    {
                        printf("x^2 + x - %lf = 0\n",-c);
                    }
                    else if (b==0)
                    {
                        printf("x^2 - %lf = 0\n",-c);
                    }
                    else if (b==-1)
                    {
                        printf("x^2 - x - %lf = 0\n",-c);
                    }
                    else if (b>0)
                    {
                        printf("x^2 + %lfx - %lf = 0\n",b,-c);
                    }
                    else
                    {
                        printf("x^2 - %lfx - %lf = 0\n",-b,-c);
                    }
                }
                else /*a!=1 && c<0*/
                {
                    if (b==1)
                    {
                        printf("%lfx^2 + x - %lf = 0\n",a,-c);
                    }
                    else if (b==0)
                    {
                        printf("%lfx^2 - %lf = 0\n",a,-c);
                    }
                    else if (b==-1)
                    {
                        printf("%lfx^2 - x - %lf = 0\n",a,-c);
                    }
                    else if (b>0)
                    {
                        printf("%lfx^2 + %lfx - %lf = 0\n",a,b,-c);
                    }
                    else
                    {
                        printf("%lfx^2 - %lfx - %lf = 0\n",a,-b,-c);
                    }
                }
            }
//***********************************************
            san=b*b-4*a*c;
            if (san>0)
            {
                x1=(-b-sqrt(san))/(2*a);
                x2=(-b+sqrt(san))/(2*a);
                printf("two real roots : %lf, %lf\n",x1,x2);
                printf("\n");
            }
            else if (san==0)
            {
                x1=-b/(2*a);
                printf("only one real root : %lf\n",x1);
                printf("\n");
            }
            else
            {
                san=-san;
                A=-b/(2*a);
                B=sqrt(san)/(2*a);
                if(A!=0 && B!=0)
                printf("two real roots : %lf+%lfi, %lf-%lfi\n",A,B,A,B);
                if(A==0 && B!=0)
                printf("two real roots : %lfi, -%lfi\n",B,B);
                printf("\n");
            }
        }
    }
    return 0;
}


这是求二次方程的跟,**行之前的差不多都是为了格式

Input

输入为多行,每行为一元二次方程的三个常数a,b,c,在double类型范围之内。当输入的a为0时,表示输入结束。

Output

每行输入的样例对应三行输出。

第一行输出为样例的编号。

第二行输出为所输入常数a,b,c对应的一元二次方程的标准形式,要求输出满足a>0。

第三行输出为所输入方程的根,分为三种情况:

1. 若方程满足Δ>0,即有两不等实根x1、x2,则按顺序(先小后大)输出这两个实根。

2. 若方程满足Δ=0,即有两相同实根x,则输出一个实根。

3. 若方程满足Δ<0,即有两共轭的虚根x1、x2,则输出两个虚根,虚部符号为正的(即u+vi形式)先输出,虚部符号为负的(x-yi形式)后输出。

以上输出均不输出数学上无意义或可省略的的符号,所有数值最多保留6位有效数字。每个样例之后都有一个空行分隔。

Sample Input

1 2 1
-1 2 -1
-5 2 -0.2
-3 2 0
3 0 12
2 4 4
0
Sample Output

Case 1 :
x^2 + 2x + 1 = 0
only one real root : -1

Case 2 :
x^2 - 2x + 1 = 0
only one real root : 1

Case 3 :
5x^2 - 2x + 0.2 = 0
only one real root : 0.2

Case 4 :
3x^2 - 2x = 0
two real roots : 0, 0.666667

Case 5 :
3x^2 + 12 = 0
two imaginary roots : 2i, -2i

Case 6 :
2x^2 + 4x + 4 = 0
two imaginary roots : -1+i, -1-i


这是输入方法,当输入
-5 2 -0.2
0
来计算的时候,貌似是出现了精度问题,算出来的不对,san变量是△。
求解怎么改。。老师讲过,不过我没听懂。。。还有就是我的输出都是浮点型的,要求是有几位小数就输出几位小数,要是各位会的话帮我也改掉吧。。3Q
搜索更多相关主题的帖子: include return C语言 
2012-12-21 18:30
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:3 
啥意思?输出格式控制?

%-m.nf


[fly]存在即是合理[/fly]
2012-12-21 18:33
Sornets
Rank: 2
等 级:论坛游民
帖 子:102
专家分:61
注 册:2012-11-22
收藏
得分:0 
回复 2楼 azzbcc
不是啊  就是输入的那个函数  求出来△应该是大于0的,但是调试的时候发现△=-2.2几  老师说是精度问题。求解啊。。
2012-12-21 21:30
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
你输入什么的时候△ = -2.2 ?

-5 2 -0.2 的△ = 0.。。


[fly]存在即是合理[/fly]
2012-12-21 23:12
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:3 
%lf修改为%m.nlf  比如 %8.4lf

DO IT YOURSELF !
2012-12-22 07:56
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:3 
double类型的数据判断相等就不应该用==
if(a == 1)
正确的应该这样

if(fabs(a - 1.000000) < 1e-6)

                                         
===========深入<----------------->浅出============
2012-12-22 11:18
a447340229
Rank: 2
等 级:论坛游民
帖 子:22
专家分:19
注 册:2012-12-18
收藏
得分:3 
暂时同意楼上,,,,,,

每个月总有那么几天要上论坛好好学习学习。。
2012-12-22 11:59
Sornets
Rank: 2
等 级:论坛游民
帖 子:102
专家分:61
注 册:2012-11-22
收藏
得分:0 
回复 4楼 azzbcc
图片附件: 游客没有浏览图片的权限,请 登录注册

你看。
2012-12-22 15:09
Sornets
Rank: 2
等 级:论坛游民
帖 子:102
专家分:61
注 册:2012-11-22
收藏
得分:0 
以下是引用laoyang103在2012-12-22 11:18:28的发言:

double类型的数据判断相等就不应该用==
if(a == 1)
正确的应该这样

if(fabs(a - 1.000000) < 1e-6)


哦哦 原来如此

不过我调试的发现误差太大了。。不知道是为什么
2012-12-22 15:17
Sornets
Rank: 2
等 级:论坛游民
帖 子:102
专家分:61
注 册:2012-11-22
收藏
得分:0 
2012-12-24 23:07
快速回复:C语言的精度问题怎么解决?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020995 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved