Reputation: 1587
In Fortran, I output the result of tanh(1)
and I get the value 0.7615941763
:
open(unit=2, file='test_digits.txt', ACTION="write")
write(2, '(1000F14.10)')( real(tanh(1.0)))
However, I now try to do the same in MatLAB and the output is 0.761594155955765
. There is a difference at the 8th digit.
What is the reason for this precision-difference? Can I fix it somehow?
Upvotes: 2
Views: 234
Reputation: 18098
Matlab is using double precision by default, you are using single precision floats! They are limited to 7-8 digits... If you use double precision as well, you will get the same precision:
program test
write(*,*) 'single precision:', tanh(1.0)
write(*,*) 'double precision:', tanh(1.0d0)
end program
Output:
single precision: 0.761594176
double precision: 0.76159415595576485
Upvotes: 6