MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Floating-Point Computing

  • To: mathgroup at smc.vnet.net
  • Subject: [mg93958] Re: Floating-Point Computing
  • From: "Nasser Abbasi" <nma at 12000.org>
  • Date: Sat, 29 Nov 2008 04:33:42 -0500 (EST)
  • References: <ggofnk$rsh$1@smc.vnet.net>
  • Reply-to: "Nasser Abbasi" <nma at 12000.org>

"Antonio" <aneves at gmail.com> wrote in message 
news:ggofnk$rsh$1 at smc.vnet.net...
> Dear List,
>
> I came across this Sun article on floating point:
> http://developers.sun.com/solaris/articles/fp_errors.html
>
> which calculates the following code:
>
> main()
> {
> register i;
> double f=0.0;
> double sqrt();
>
> for(i=0;i<=1000000000;i++)
> {
>    f+=sqrt( (double) i);
> }
> printf("Finished %20.14f\n",f);
> }
>
> whose correct answer using Euler-Maclaurin formula is:
> f=21081851083600.37596259382529338
>
> Tried to implement in mathematica, just for fun, so that later I could
> play with precision, but the kernel has no more memory and shuts down.
>
> N[Total[Sqrt[Range[1000000000]]]]
>
> Any ideas?
>
> ANtonio
>

The above are 2 different implementations.

In C, there you did not do pre allocation of all the numbers being square 
rooted.

It is done in a loop, one at a time. In Mathematica, you are asking to 
pre-allocate a list whose length is 1000000000, then sqrt each entry in the 
list, then add the result.

I think you are comparing apples and oranges in terms of implementation.

This is what I get on my PC using gcc 3.4.4 (using the above C code)
$ ./a.exe
Finished 21081851083598.38281250000000

And this is what I get using Mathematica version 7, code rewritten to use a 
for loop as in C
In[22]:= f = 0.;
Timing[For[i = 0, i <= 1000000000, i++, f = f + Sqrt[N[i]]]; f]

Out[23]= {3249.547, 2.1081851083598383*^13}

ps. I noticed if I do Sqrt[ N[i] ]  vs Sqrt[ i ], it runs faster.

Nasser



  • Prev by Date: Re: Re: Re: v.7.0 issues
  • Next by Date: Strange behaviour of Fourier[] in Mathematica 7
  • Previous by thread: Re: Floating-Point Computing
  • Next by thread: Extracting free parameters from a Solve result