Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.

High Precision Sum

seiji-torigoe
Beginner
404 Views
1.Will you know why the total is different?
2.Is there a sum function of high precision?

// result
Sum = 1099999.99999728520000000000
Sum = 1100000.00003658240000000000
Sum = 1100000.00000000000000000000

// code
#include
#include
#include "ipp.h"

void main(void)
{
int len, i;
Ipp64f *pSrc, *pWrk;
IppStatus Status;
Ipp64f S, R, T;

len = 1000000+1;

pSrc = ippsMalloc_64f(len);
Status = ippsSet_64f(0.1, pSrc, len);
*pSrc = 1000000.0;

Status = ippsSum_64f(pSrc, len, &S);
printf("Sum = %.20f ", S);

S = 0.0;
pWrk = pSrc;
for ( i = 0 ; i len ; i++ ) {
S = S + *pWrk;
pWrk++;
}
printf("Sum = %.20f ", S);

S = 0.0;
R = 0.0;
pWrk = pSrc;
for ( i = 0 ; i len ; i++ ) {
R = R + *pWrk;
pWrk++;
T = S;
S = S + R;
T = S - T;
R = R - T;
}
printf("Sum = %.20f ", S);

ippsFree(pSrc);

printf("push any key "); getch();
}
0 Kudos
2 Replies
Vladimir_Dudnik
Employee
404 Views
Hi,
there is answer from our expert:

So 0.3E-10 accuracy is not enough?

Let us do several simple calculations:

You use for sum the rounded number (0.1 cant be presented as combination of powers of 2, in hex it is 0x3FB999999999999A, that means that the last bit has been already rounded). Therefore for every add instruction we lose for the last bit 0.5 of accuracy (2.2204460492503131e-016 it is the weight of the last bit (I mean 64f data type which has 53bit mantissa), so for 0.5 bit we have ~1E-16). For your length = ~1E6 the accuracy loss is 1E6 * 1E-16 = ~1E-10, which is completely correspond to the result you have obtained.

If you need more precise sum you should use BigNumberArithmetic library with synthetic data types.

Regards,
Vladimir
0 Kudos
seiji-torigoe
Beginner
404 Views
Thanks.
It is all right.
I was expecting the high accuracy of my code end.
It is regrettable.
0 Kudos
Reply