Julia's computation is more accurate because we use pairwise summation 
instead of naive summation. This gives the same results as MATLAB:

x = 0.0; for i = 1:100; x += pi/100; end; x - pi

We don't use summation in Range construction, though. The reason the range 
ends up different is that pi/(pi/100) is very slightly less than 100, and 
MATLAB fudges it because the difference is less than 3 ulp.

Simon

On Thursday, April 24, 2014 2:05:51 PM UTC-4, Hans W Borchers wrote:
>
> There is a strange difference in results between Matlab and Julia that 
> could be the reason why  [0:pi/100:pi]  ends up differently:
>
>     matlab> x = ones(100, 1) * (pi/100);
>     matlab> sum(x) - pi
>     ans =
>       -4.4409e-15
>
>
>     julia> x = ones(100)*(pi/100);
>     julia> sum(x) - pi
>     1.3322676295501878e-15
>
>
> So it appears that summing pi/100 100-times is smaller than pi in Matlab 
> and larger than pi in Julia.
>
>
> On Thursday, April 24, 2014 5:47:34 PM UTC+2, Stefan Karpinski wrote:
>>
>> On Thu, Apr 24, 2014 at 11:43 AM, andrew cooke <[email protected]> wrote:
>>
>>>
>>> sorry, rational is a stupid suggestion for irrational numbers...
>>>
>>> what i am trying to say is that there is no perfect solution to this 
>>> problem (that i know of).  if matlab works better then it is either luck or 
>>> because they are actually fudging things.  fudging things is appealing but 
>>> usually bites you somewhere down the line.
>>>
>>
>> This is exactly how I feel about it. I still think it may be possible to 
>> improve our FloatRange behavior even further, but I'd like to avoid 
>> "fudging it".
>>
>

Reply via email to