Correctly summing numbers

Hello,

I’d like to sum the following numbers:

2.0, 1e100, 2.0, -1e100

I do the following:

q)a:2.0e+0 1e100 2.0e+0 -1e100

q)sum(a)

0f

However the result is incorrect. Does Q provide a means to perform arithmetic correctly? if so what is the correct way of summing lists of numbers.

Gil.

the number 2 - 1e+100 is -9999999999999…999999998

now q uses double arithmetic, so this number has to be
rounded to something that can be contained in a double.?
ie. -1e+100

q)2 - 1e+100
-1e+100
q)2 - 1e+7
-9999998f

more here:

?http://en.wikipedia.org/wiki/Double-precision_floating-point_format

ta, jack

On Friday, August 2, 2013 8:15:49 AM UTC+10, effbiae wrote:

the number 2 - 1e+100 is -9999999999999…999999998

now q uses double arithmetic, so this number has to be
rounded to something that can be contained in a double. 
ie. -1e+100

q)2 - 1e+100
-1e+100
q)2 - 1e+7
-9999998f

more here:

 http://en.wikipedia.org/wiki/Double-precision_floating-point_format

Thanks for the information Jack. 

I had been hoping that there was a function in q that would be capable of attempting to perform an exact sum or something close to an exact sum, perhaps using Kahan summation, sorting of values or adding them into a priority queue, partial fractions over multiple sums, pairwise summation and so on.

Gil.

 

a simple sorting solution:

q)sum a idesc abs a:2 1e100 2 -1e100
4f