【问题标题】:Precision of Fortran vs that of MatlabFortran 与 Matlab 的精度
【发布时间】:2015-04-25 21:35:08
【问题描述】:

在 Fortran 中,我输出 tanh(1) 的结果,我得到值 0.7615941763

open(unit=2, file='test_digits.txt', ACTION="write")
write(2, '(1000F14.10)')( real(tanh(1.0)))

但是,我现在尝试在 MatLAB 中执行相同的操作,输出为 0.761594155955765。第8位有区别。

这种精度差异的原因是什么?我能以某种方式解决它吗?

【问题讨论】:

    标签: matlab floating-point fortran precision


    【解决方案1】:

    Matlab 默认使用双精度,你使用的是单精度浮点数!它们仅限于 7-8 位...如果您也使用双精度,您将获得相同的精度:

    program test
      write(*,*) 'single precision:', tanh(1.0)
      write(*,*) 'double precision:', tanh(1.0d0)
    end program
    

    输出:

     single precision:  0.761594176    
     double precision:  0.76159415595576485  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2010-12-04
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多