Re: yet another question on Mathematica numerical, how to force it to use single precision?




"Nasser Abbasi" <nma@xxxxxxxxx> wrote in message
news:l2lUk.8540$YU2.2535@xxxxxxxxxxxxxxxxxxxxxxx
How can I make Mathemtica give me the same digits to this little sum below
as I get from Fortran or Matlab?
I tried using just N[], which is should use machine precision. I want to
tell it to do the computation in "single precision", i.e. 32 bit as
compared to double precision.

Could some one remind me how to do this?

This is the Mathematica code

---------------------
In[95]:= s = Sum[N[1/k^2], {k, 1, 20000}]
Precision[s]
Accuracy[s]

Out[95]= 1.644884068098209
Out[96]= MachinePrecision
Out[97]= 15.738454476026273
----------------------

This is what I get from Fortran

PROGRAM main
IMPLICIT NONE
REAL :: s
INTEGER :: n,MAX

s = 0.0;
MAX = 20000;

DO n = 1,MAX
s = s + (1./n**2);
END DO

WRITE(*,*), 'sum', s
END PROGRAM main

$ f77 sum_problem.f
$ ./a.exe
sum 1.64472532

------------------------------

This is what I ge from Matlab (by forcing matlab to use single precision

format long g;
s=single(0);
MAX = 20000;
for i=1:MAX
s=s+(1/i^2);
end
s

s =
1.644725

So, Fortran and Matlab generate the same first 6 digits beyond the decimal
point. I want Mathematica to generate those same digits.

Any ideas?



I just tried something.

I think the result of Mathematica is simply the same result as double
precision. So, my question is really, how to make Mathematica use single
precision instead of double precision? When I change Fortran to double it
now gives same digits as Mathematica with N[] or N[,16]

PROGRAM main
IMPLICIT NONE
DOUBLE PRECISION :: s <---- change to double
INTEGER :: n,MAX

s = 0.0;
MAX = 20000;
DO n = 1,MAX
s = s + (1./n**2);
END DO

WRITE(*,1) s
1 format('sum = ', F16.14)
END PROGRAM main

$ ./a.exe
sum = 1.64488406809821

compare to Mathematica with just N[]

Out[95]= 1.644884068098209

But when I use N[,7] in Mathematica hoping to get some number close to what
I get with single precision which is 1.644725, but I still do not

In[122]:= s = Sum[N[1/k^2, 7], {k, 1, 20000}]
1.6448840680982050618`7.000000000000145

Is it possible to do computation in single precision in Mathematica?

Nasser


.



Relevant Pages

  • Re: help about ARPACK solver
    ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ... I still dont know what kind of criteria Matlab adopts to form ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... Fortran code isn't working right yet. ... try and save the values to a file and load them into Matlab to do the ... Matlab uses 'double precision' variables throughout by default. ...
    (sci.math.num-analysis)
  • Re: How to increase Working Precision?
    ... arbitrary precision in applied situations is quite ... not 10 problems, each 10 digits. ... the machine-arithmetic subroutine library of Mathematica, ... iterations I've suggested than the 1-2 or 3 arithmetic ...
    (sci.math.symbolic)
  • Re: yet another question on Mathematica numerical, how to force it to use single precision?
    ... which is should use machine precision. ... I want to tell it to do the computation in "single precision", i.e. 32 bit as compared to double precision. ... This is what I get from Fortran ... This is what I ge from Matlab (by forcing matlab to use single precision ...
    (sci.math.symbolic)
  • Re: 0^0 oh no!
    ... you'd use SetPrecision to tell the system to ... in Mathematica one would typically use SetPrecision if coding ... For languages that work in fixed precision (I think ... think typically one would, at the finish of an iterative algorithm, ...
    (sci.math.symbolic)