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



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?

thanks,
Nasser


.



Relevant Pages

  • Re: maatlab vs. fortran
    ... visual simulation) where each simulation consists of random data ... In matlab this simulation ... am used to matlab but if fortran is realy much better then fortran I ... about 15 significant digits and no other choice. ...
    (sci.math.num-analysis)
  • sprintf fixed number of spaces per column?
    ... In Fortran you can do columns by using printf and allowing for only a certain number of digits, but I it seems as though Matlab is a little different. ...
    (comp.soft-sys.matlab)
  • Re: MATLAB <-> FORTRAN
    ... When you do A\b it just calls the basic solvers Matlab knows, ... in some cases Gauss Elimination. ... In this case you can find a Gauss Solver, ... I've bitten the bullet and converted to Fortran ...
    (comp.soft-sys.matlab)
  • Re: Matlab Vectorisation Speed - How is it done in c++?
    ... Beating the performance of vectorized Matlab code is very ... Matlab makes calls to optimized C and Fortran libraries ... Use optimization level 3 on numerical code and level 2 on non- ...
    (comp.soft-sys.matlab)
  • Re: Calling MATLAB from Fortran
    ... Fortran saves the data which Matlab m-file needs and then Matlab would read them as input from the file and after calculation save then in a file for Fortran. ... returns mxArray * ...
    (comp.soft-sys.matlab)