Re: Precision issue?

From: Paul Van Delst (paul.vandelst_at_noaa.gov)
Date: 07/19/04


Date: Mon, 19 Jul 2004 13:44:07 -0400

beliavsky@aol.com wrote:
> Paul Van Delst <paul.vandelst@noaa.gov> wrote:
>
>
>>Others have replied why the answer isn't what you think it should be. The
>
> way the code
>
>>you
>>posted is accumulating is simply a bad way of adding numbers because it
>
> doesn't take
>
>>the
>>imprecision of floating point calculations into account.
>>
>>I realise your example was probably oversimplified for posting to clf, but
>
> can you do
>
>>something like:
>>
>> y = real(i)*yy
>>
>>and avoid the summation altogether?
>>
>>If you are adding a set of numbers, e.g.
>>
>> y = 0.0
>> do j = 1, 1000000
>> y = y + x(j)
>> end do
>>
>>you may want to look into some sort of compensated summation algorithm.
>
> Kahan's method
>
>>is
>>a commonly known one, but there are plenty of others (most of them with
>
> their own caveats.)
>
> In Fortran 90/95 one can write y = SUM(x). I wonder how sophisticated the
> implementation of SUM is in Fortran compilers. Do they just use the loop
> above, perhaps with loop unrolling?

If a loop is generated, unrolling it appropriately would be seemly I would think.
>
> To reduce roundoff error, one should sum the numbers in order of increasing
> size if they are all positive.

Yeah, the sorting of data to improve some summation algorithms was pretty much what I
meant by "caveat". The sorts of data I typically sum (1000000's of points of spectral
atmospheric transmittances) are always 0<x<1 so sorting doesn't provide a big enough
improvement to balance the actual sorting (in both time and code complexity). Have I
mentioned previously that I do pretty simple stuff? :o)

> Sometimes one has information about the magnitudes
> and ordering of data. I wonder if it would make sense to add optional arguments
> to the SUM function and other intrinsic functions, so that one could write
>
> y = SUM(x,order="descending",sign="positive")
>
> and have the compiler use the optimal summation algorithm.

Hmm. I don't know about that. I sort of like having to think about that sort of stuff
myself. Not because I'm a masochist, but it makes me appreciate and cogitate about
numerical precision/problems for subsequent problems.

cheers,

paulv



Relevant Pages

  • Re: sorting
    ... I have matlab to generate a table of "numbers" it's a 600x6 matrix. ... Say I have columns A to F I wanted to sort the numbers by column E ... This is not sorted by F after sorting by E. ... F is the sum of the numbers from A to D multiplied to a variable. ...
    (comp.soft-sys.matlab)
  • Re: Sort-of a sorting problem
    ... Sort these into a list with proper outline format: ... I need to find a way to sum each lower level into a successive level (ie ... >summarixe all 1.1.2.1.x iinto 1.1.2.1) up to the top level. ... standard aggegate functions will do this in each level's ...
    (microsoft.public.access.formscoding)
  • Re: Use index and match function to get data from pivot table
    ... If you can sort the names, you should be able to use 'Subtotals' found on the Data menu. ... There are 1000 students in file. ... "Sum of % of bad performance ...
    (microsoft.public.excel.programming)
  • Re: trying to sum a group of records but "sum" missing from Group, Sort, and Total dropdown/wizard
    ... I could then see that "sum" was unghosted and available to me. ... I could then also create groups using the group & sort wizards and get ... Format changes number type to string type. ... Totals tab I can Group my records by the "productiondate" field then ...
    (microsoft.public.access.reports)
  • Re: Sort-of a sorting problem
    ... I setup 5 integer fields. ... This makes it easy for sort and grouping ... Each of the lower level tasks is an aggregate sum already involving several tables, subcalculation for each record, and summation of that sum. ... Separate fields could be used for each heirarchy level, but this will be extremely tedious. ...
    (microsoft.public.access.formscoding)