作者sjgau (sjgau)
看板Fortran
标题Re: [问题] 可以判断整数吗?
时间Sun Dec 26 12:00:03 2010
spacing(x) 所提供的,是理论值,
但是,经过程式的反覆运算之後,
会累积误差,得到的是 经验值。
经验,还是很重要的。
你直接套用 理论值,会死得 很难看。
8.226 SPACING — Smallest distance between two numbers of a given type
Description:
Determines the distance between the argument X and the nearest adjacent
number of the same type.
Standard:
Fortran 95 and later
Class:
Elemental function
Syntax:
RESULT = SPACING(X)
Arguments:
X Shall be of type REAL.
Return value:
The result is of the same type as the input argument X.
Example:
PROGRAM test_spacing
INTEGER, PARAMETER :: SGL = SELECTED_REAL_KIND(p=6, r=37)
INTEGER, PARAMETER :: DBL = SELECTED_REAL_KIND(p=13, r=200)
WRITE(*,*) spacing(1.0_SGL) ! "1.1920929E-07" on
i686
WRITE(*,*) spacing(1.0_DBL) ! "2.220446049250313E-016" on
i686
END PROGRAM
※ 引述《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:推 pillbox1989:大推本篇 细心教学文! 12/26 13:32