精華區beta Fortran 關於我們 聯絡資訊
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: sjgau4311@gmail.com 我的課程介紹網頁: http://www.csie.ntu.edu.tw/train/teacher_display.php?num=18 AutoCAD 台灣地區菁英講師獲選 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.82
pillbox1989:大推本篇 細心教學文! 12/26 13:32