作者sjgau (sjgau)
看板Fortran
标题Re: [问题] 可以判断整数吗?
时间Sun Dec 26 10:24:06 2010
以下的 C语言程式设计,
double 就是 fortran 的 real*8,
经过 25次的 复杂运算,产生的
绝对误差如下
谨 提供参考
#if 0
dx= 5.684342e-014
请按任意键继续 . . .
#endif
// ----------------------------------------------
#include <stdio.h>
#include <math.h>
#include <process.h>
// ----------------------------------------------
template <class T>
T sqr(T x)
{
return(x*x);
}// end of sqr()
// ----------------------------------------------
void main()
{
int no, i;
double x, dx;
no= 25;
x= 1.0;
for (i=1;i<=no;i++) {
x= (tan(atan(exp(log(sqr(sqrt(x))))))) + 1.0;
}
dx= fabs(x - (no + 1));
printf("\n dx= %.6le\n", dx);
system("pause");
}// end of main()
※ 引述《sjgau (sjgau)》之铭言:
: 重点是
: real*4
: rel_diff(a, b) < 1.0e-5
: real*8
: rel_diff(a, b) < 1.0e-14
: 那个 e-5, e-14 的数字,是怎麽来的?
: 答案是:经验!
: ※ 引述《sjgau (sjgau)》之铭言:
: : 我会!
: : real*4, or real*8 都可以。
: : real*4 x, y, z;
: : x= 16.0
: : y= sqrt(x)
: : iy= int(y + 0.5)
: : ! int() 还要 查ㄧ下,目的是 把 y 四舍五入,
: : ! 取最接近的 整数
: : if (rel_diff(iy, y) < 1.0e-5) then
: : ! y 是整数
: : else
: : ! y 不是整数
: : end if
: : ! rel_diff(a, b) 是自己写的 副程式, 计算 a, b 的相对误差
: : ! 相对误差等於 = (abs_diff(a, b))/max(abs(a), abs(b))
: : ! abs_diff(a, b) 是自己写的副程式,求 a, b 两个数字的 绝对误差
: : ! 绝对误差= abs(a - b)
--
e-mail:
[email protected]
我的课程介绍网页:
http://www.csie.ntu.edu.tw/train/teacher_display.php?num=18
AutoCAD 台湾地区菁英讲师获选
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.30.82
1F:→ blc:能用fortran作一次同样的事吗?毕竟讨论的是fortran。 12/26 13:29