① 用c语言编写一个函数,判断两个实数是否相等,两数误差在0.0001内认为相等
#include <stdio.h>
#include <stdbool.h>
bool equivalence(double x, double y)
{
double z = x - y;
if(-0.0001<z && z<0.0001)
{
return true;
}
else
{
return false;
}
}
int main(int argc,char *argv[])
{
double a,b;
scanf("%lf %lf",&a,&b);
if(equivalence(a,b))
{
printf("相等 ");
}
else
{
printf("不相等 ");
}
return 0;
}
运行结果
西安盛图软件科技
② C语言浮点数误差的解决
浮点数表示法的确存在精度丢失的问题,是由于最低有效位的限制造成的。而且一般其实际表示的数值会比规定的要小。比如2.6的浮点数表示为40266666H,而该数实际的值是2.5999999,这样计算的结果是0.0999999比0.1小,不能输出0.1,产生精度丢失,而如果将其表示更改为40266667H,其表示的实际值是2.6000001,而这个数可以输出0.1的结果的。所以如果要使计算能够得出正确结果,可以尝试在输入的时候将有效位数扩展,以确保得出正确结果。
比如要输出2.6的结果,可以设sx=2.6000001,这样可以使浮点数表示结果为大于2.6的尽可能小的值。如果要输出2.1的结果,可以设sx=2.1000001。
int k=0;
float sx=2.6000001;
③ 求教ierfc函数的C/C++函数实现
求教ierfc函数的C/C++函数实现
悬赏分:30 - 离问题结束还有 14 天 15 小时
现在有一个Q函数
Q函数可以很容易的通过erfc函数求得
erfc(x)=(2/根号π)*∫exp(-t²/2)dt 其中 积分上限是正无穷 下限是x
现在问题是要求Q的反函数,有人说可以通过erfc的反函数求得(即ierfc)
可是我不会 也没有查到 请知道ierfc函数的或者能够解决我问题的大虾赐教 不甚感激
回答:
下面这个是在MATLAB环境下的解决方案,希望能给你点启发,
起到抛砖引玉的作用
使用二分法计算erfc函数的反函数M程序
function c=finverse_erfc(z);
% count inverse function of erfc[complementary error function]
% Author email of the program:% [email protected]
a=600;
b=0;
if z>1 | z<0;
error([[' z takes a error value!'],...
['; Please check it''s value!']]);
end
c=(a+b)/2;
while abs(erfc(c)-z)>1e-6;
c=(a+b)/2;
if erfc(c)-z<0;
a=c;
else
b=c;
end
end
% c是你要的值
④ 在C语言中erfc函数怎么表示,也就是x的误差补偿函数,急求高人指点。。。
float用printf("%f"来表示,double用printf("%ld"来表示。
把英文帮助找出来了,自己看看吧。
函数原型:
#include <math.h>
double erfc(double x);
float erfcf(float x);
long double erfcl(long double x);
Link with -lm. 编译时需要链接 -lm
DESCRIPTION 说明
The erfc() function returns the complementary error function of x, that is, 1.0 - erf(x).
RETURN VALUE 返回值
On success, these functions return the complementary error function of x, a value in the range [0,2].
If x is a NaN, a NaN is returned.
If x is +0 or -0, 1 is returned.
If x is positive infinity, +0 is returned.
If x is negative infinity, +2 is returned.
If the function result underflows and proces an unrepresentable value, the return value is 0.0.
If the function result underflows but proces a representable (i.e., subnormal) value, that value is returned, and a range error occurs.
⑤ erfc函数在c语言中用什么头文件
#include<math.h>
⑥ c语言求平均数,最后有误差;麻烦帮我看一下,谢谢!
定义为float型,保留6位小数,最后的数字出现误差是正常的,原因是float型精度不足。解决方法:ave不用float型,改为double型。
⑦ C语言中怎么用erfc函数我的是vc6,函数库里面没有这个函数。求代码〒_〒
void calroot(double a, double b, double c)
{
double dert;
double value;
if (a == 0)
{
printf("元二程!\n");
return;
}
dert = b *b - 4 * a * c;
⑧ C语言浮点运算感觉很奇怪同样的数,算出来结果却不一样,这是怎么回事
计算机存储和运算都是以二进制处理的,而表达式是十进制的,那么存储或运算时是要转换成二进制,计算完成后输出还要再转换成十进制。
那么你应该明白二进制每一位权重都是2^N,此处N为位号,位号分布如下:
...3,2,1,0(小数点) -1,-2,-3...
权重分布如下:
...2^3,2^2,2^1,2^0 (.小数点)2^-1,2^-2,2^-3
例如一个二进制的1.111(B)=1.875(D)
整数部分:1的位号为0,因此1*2^0=1*1=1
小数点向右第一个1:位号为-1,因此1*2^-1=1*1/2=0.5
小数点向右第二个1:位号为-2,因此1*2^-2=1*1/4=0.25
小数点向右第三个1:位号为-3,因此1*2^-3=1*1/4=0.125
合起来1+0.5+0.25+0.125=1.875,其他位依此类推。
无需想得太多,你可以明显看到,二进制小数表达的数都是不同级别减半后的累加。与十进制的某些小数没有一一对应,显然转换必然会发生误差。
另一方面,存储时CPU会对十进制小数会进行编码,float的尾数长度为23位,阶码8位,符号占1位,共32位。double是64位,无论如何精度都是有限的,因此也会存在误差,1.1*100时编译器会将表达式先转换二进制并运算运算,运算后再编码存储到变量中或临时变量中,而运算是由CPU直接处理的,因此你可以看到有个0.000002的误差数,而printf是个函数,对误差进行了修正。
PS:简单了解下浮点数的编码方式网页链接
⑨ c语言计算误差,求2的59次方与标准差2
我这里有2个方法计算的2的59次方,结果一致,希望能帮到你:
#include<stdio.h>
#include<string.h>
int
main(){
long
long
a
=
1;
a
=
a
<<
59;
printf("a
=
%lld\n",
a);
double
d
=
1;
int
i
=
59;
while(i--)
d
=
d*2;
printf("d
=
%f\n",
d);
return
0;
}
⑩ 求误差函数erf C语言程序代码!!!急!!!!
C99标准开始就有误差函数的内部实现,所以只要包含<math.h>头文件即可。
调用格式为:
float erff( float arg ); /* 单精度误差函数 */
double erf( double arg ); /* 双精度误差函数 */
long double erfl( long double arg ); /* 长双精度误差函数 */