Re: Compiled function changes somehow.
- To: mathgroup at smc.vnet.net
- Subject: [mg78602] Re: Compiled function changes somehow.
- From: dh <dh at metrohm.ch>
- Date: Thu, 5 Jul 2007 04:02:57 -0400 (EDT)
- References: <f6fqn6$8mo$1@smc.vnet.net>
Hi Nacho, try: Max[Abs[standardresult-compiledresult]] and you will see that the differences are very small, that is in the last few bits. The non compiled version first makes a rational number and then converts it to a machine number. The compiled form on the other hand, calculates the quotient of 2 machine numbers. As machine arithmetic is not 100% accurate you can not expect bothe results to be identical up the the last bit. hope this helps, Daniel Nacho wrote: > Hello. > > I have been playing with Compile to accelerate some simple code and I > have found some differences that I cannot explain. > > The standard/compiled code is as follows: > > standard[max_] := > Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten; > > compiled = > Compile[{{max, _Integer}}, > Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten]; > > So I can do the same calculations with both codes: > > In[19]:= standardresult = standard[1000]; // Timing > > Out[19]= {2.969, Null} > > In[20]:= compiledresult = compiled[1000]; // Timing > > Out[20]= {0.422, Null} > > > The second is much faster, as expected. But are the results the same? > Apparently, yes: > > In[21]:= standardresult == compiledresult > > Out[21]= True > > In[22]:= standardresult === compiledresult > > Out[22]= True > > > But when I use Union in the lists to count the number of different > elements, I have this surprise: > > Length[Union[standardresult]] > Length[Union[compiledresult]] > > Out[23]= 608383 > Out[24]= 634815 > > So they are not exactly the same... I think that the correct answer > comes from the uncompiled version. It is the same if I remove the //N > so it can be compared symbolically. > > Is this behaviour expected? Am I missing something? > > Both seem to be Machine Precision, but obviously there are some little > differences. This happens with V5.2 and V6.0. > > Any hint? > > Thank you. > >