I find that Fortran gives different result in a simple calculation, compared with C++, Python, or Julia.
The following code in Fortran
program array_slice
implicit none
double precision :: sigma = 0.30
double precision :: rho = 0.75
write(*,*) dexp(dble(3.) * sigma / dsqrt(dble(1.) - rho*rho))
end program array_slice
saved as test.f90
, compiled using ifort test.f90 -o test
, returns
3.89881303534984
However, the following code in C++
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
double sigma = 0.3;
double rho = 0.75;
std::cout << std::setprecision(15) << std::exp(3. * sigma / std::sqrt(1. - rho*rho)) << std::endl;
return 0;
}
saved as testcpp.cpp
, compiled using g++ testcpp.cpp -o testcpp
, returns
3.89881282454784
Similarly, the following code in Python 3.8.5
import numpy as np
sigma = 0.3
rho = 0.75
print(np.exp(3 * sigma / np.sqrt(1 - rho**2)))
returns
3.8988128245478393
Still similarly, the following code in Julia 1.6.1
σ = 0.3
ρ = 0.75
println(exp(3. * σ / sqrt(1 - ρ^2)))
returns
3.8988128245478393
All of the above test are done in 64-bit Win10 WSL.
I guess Fortran uses single-precision, instead of double-precision, but I don't know why since I've already defined sigma
and rho
as double precision
and used dexp
and dsqrt
instead of exp
and sqrt
.