如何在C中获得更高的精度? [英] How to get more precision in C ?

查看:113
本文介绍了如何在C中获得更高的精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的数值计算程序中获得至少8位数的精度。我尝试过使用双打,但我一直得到

结果只有小数点后6位。例如。


#include< stdio.h>

#define M_PI 3.14159265358979323846 / * M_PI未在math.h中定义

根据我的编译器* /


int main(无效)

{

双倍;

s = M_PI;

printf("%f",s);

返回0;


}

I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.

#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;

}

推荐答案

>我希望在我的数字
>I would like to have precision upto atleast 8 digits in my numerical
$ b $中有至少8位数的精度b>计算程序。我尝试过使用双打但是我一直只得到结果直到小数点后6位。例如。
>computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.



您只能在小数点后打印* 6位数。在实际计算中你可能会获得更高的精确度。


尝试:printf("%300.200f",s);

虽然这主要用于调试。双赢不要
几乎有这么多数字的精确度。

You only *print* 6 digits after the decimal. You may be getting greater
precision in the actual computation.

Try: printf("%300.200f", s);
although this is primarily useful for debugging. A double won''t
have nearly this many digits of precision.


> #include< stdio.h>
#define M_PI 3.14159265358979323846 / * M_PI未在math.h中定义
根据我的编译器* /
int main(void)
{
double s ;

s = M_PI;

printf("%f",s);

返回0;

}
>#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;

}


在文章< bd ************************* ********* @ q27g2000prf。 googlegroups.com>,

pereges< Br ***** @ gmail.comwrote:
In article <bd**********************************@q27g2000prf. googlegroups.com>,
pereges <Br*****@gmail.comwrote:

>我想要在我的数值计算程序中,精度达到至少8位数。我尝试过使用双打但是我一直只得到结果直到小数点后6位。例如。
>I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.


> #include< stdio.h>
#define M_PI 3.14159265358979323846 / * M_PI未在math.h中定义/>根据我的编译器* /
>#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/


> int main(void)
{
double s;

s = M_PI;

printf("%f",s);
返回0;
}
>int main(void)
{
double s;
s = M_PI;
printf("%f", s);
return 0;
}



使用%f格式指定精度,例如%.60f。 %f的默认值

为6.


另外,为了安全起见,请确保输出以换行符结尾。例如,


printf("%。9f \ n",s);


-

问:为什么鸡穿过莫比乌斯带?


答:有多种原因。

Specify a precision with your %f format, such as %.60f . The default
for %f is 6.

Also, to be safe, ensure your output ends in a newline. For example,

printf("%.9f\n", s);

--
Q: Why did the chicken cross the Mobius strip?

A: There were manifold reasons.


4月10日,8:53 * pm,pereges< Brol ... @ gmail.comwrote:
On Apr 10, 8:53*pm, pereges <Brol...@gmail.comwrote:

我希望精确到达我的数字中的至少8位数/>
计算程序。我尝试过使用双打,但我一直得到

结果只有小数点后6位。例如。


#include< stdio.h>

#define M_PI 3.14159265358979323846 / * M_PI未在math.h中定义

根据我的编译器* /


int main(无效)

{

* * * * double s;

* * * * s = M_PI;

* * * * printf("%f",s);


返回0;


}
I would like to have precision upto atleast 8 digits in my numerical
computation program. I have tried using doubles but I keep getting
results only till 6 places after decimal. eg.

#include <stdio.h>
#define M_PI 3.14159265358979323846 /* M_PI is not defined in math.h
according to my compiler*/

int main(void)
{
* * * * double s;
* * * * s = M_PI;
* * * * printf("%f", s);

return 0;

}



试试这样:


#包括< stdio.h>

#include< float.h>

static const double pi_approximation =

3.1415926535897932384626433832795;


int main(无效){

printf(我的近似值为%。* g\ n,DBL_DIG + 1,

pi_approximation);

返回0;

}

Try it this way:

#include <stdio.h>
#include <float.h>
static const double pi_approximation =
3.1415926535897932384626433832795;

int main(void) {
printf("My pi approximation is %.*g\n", DBL_DIG + 1,
pi_approximation);
return 0;
}


这篇关于如何在C中获得更高的精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆