Bil*_*ean 2 floating-point precision matlab fortran
在Fortran中,我输出结果,tanh(1)并得到值0.7615941763:
open(unit=2, file='test_digits.txt', ACTION="write")
write(2, '(1000F14.10)')( real(tanh(1.0)))
Run Code Online (Sandbox Code Playgroud)
但是,我现在尝试在MatLAB中做同样的事情,输出是0.761594155955765.第8位有差异.
这种精确度差异的原因是什么?我能以某种方式修复它吗?
Matlab默认使用双精度,你使用单精度浮点数!它们限制在7-8位......如果你也使用双精度,你将获得相同的精度:
program test
write(*,*) 'single precision:', tanh(1.0)
write(*,*) 'double precision:', tanh(1.0d0)
end program
Run Code Online (Sandbox Code Playgroud)
输出:
single precision: 0.761594176
double precision: 0.76159415595576485
Run Code Online (Sandbox Code Playgroud)