by default, if a number has decimal .
after it, then Mathematica will do computation using machine Precision, which on my PC (intel hardware) running windows 7 64 bit is double Precision.
I'd like to get the computation also but using single and quad precision, to match the following small Fortran program output.
Is there a recommend way to do this? Here is the fortran program and its output
PROGRAM foo
IMPLICIT NONE
INTEGER:: i
INTEGER, PARAMETER :: &
sp = kind(1.0), &
dp = selected_real_kind(2*precision(1.0_sp)), &
qp = selected_real_kind(2*precision(1.0_dp))
REAL(kind=sp) :: sum1,x1
REAL(kind=dp) :: sum2,x2
REAL(kind=qp) :: sum3,x3
sum1=0.0_sp; sum2=0.0_dp; sum3=0.0_qp
x1 = 0.00001_sp
x2 = 0.00001_dp
x3 = 0.00001_qp
DO i=1,10**5
sum1 = sum1 + x1
sum2 = sum2 + x2
sum3 = sum3 + x3
END DO
PRINT *,sum1; PRINT *,sum2; PRINT *,sum3;
END
Compiled using gfortran foo.f90
gives
ex1>./a.out
1.00099015 ---> SINGLE
0.99999999999808376 --> DOUBLE
0.999999999999999999999999999998395508 ----> QUAD
The first line is single, the second is double (which Mathematica matches exactly) and the third is quad.
Here is the Mathematica basic code which gives output that matches the double Precision:
x1=0.00001;
sum1=0.0;
Do[sum1=sum1+x1,{i,1,10^5}]
InputForm[sum1]
Which gives
0.9999999999980838
I tried doing
x1=0.00001`32;
sum1=0.0`32;
To see if will something close to the quad result, but it had not effect on final output. I still get 0.9999999999980838
How to change the above Mathematica code to make it give the single and quad Precision as shown by Fortran?
0.0`32
is for some reason MachinePrecision. You can actually initializesum1=0;
and get your desired precision in the calculation. (It does not match the fortran results though) $\endgroup$sum=0
instead ofsum=0.0
makes it do arbitray precision somewhere internally? $\endgroup$