Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualDonTzzy

Posted 09 September 2013 - 12:47 AM

http://gafferongames.com/networking-for-game-programmers/floating-point-determinism/


#2DonTzzy

Posted 09 August 2013 - 03:17 AM

.


#1DonTzzy

Posted 07 August 2013 - 02:00 AM


I have two devices, a tablet and a cell phone, that give different results after computations.

 

http://gafferongames.com/networking-for-game-programmers/floating-point-determinism/

 

 

 

 


Running  float sum = 0; for(i=0; i<10000; i++) sum += 0.0001; yields 1.0000535 where doubles are 0.9999999999999062.

So currently there could be a potential problem even on my tablet over a span of time. I do check some results against an epsilon value of 0.001, but does this have to be done on every operation? How would one combat error accumulation?

 

 

 

( Sorry, I don't know Java.  But, I'm pretty sure you can translate this. )

 

 

#include "stdio.h"

 

template<class T>

T GetKahanSum ( const T v0[], const int numElements )

{

T sum = 0.0;

T c   = 0.0;

 

for (int i = 0; i < numElements; ++i)

{

T y = v0[i] - c;

T t = sum + y;

c   = (t - sum) - y;

sum = t;

}

 

return sum;

}

 

 

int main()

{

const int num = 10000;

float  sum    = 0.0;

double dSum   = 0.0;

 

for( int i = 0; i < num; i++ )

{

sum  += 0.0001;

dSum += 0.0001;

}

 

printf("sum : %16.16lf, dSum: %16.16lf\n", sum, dSum);

 

float arrayOfFloats[num];

for( int i = 0; i < num; i++ )

{

arrayOfFloats[i] = 0.0001;

}

 

float kSum = 0.0;

kSum = GetKahanSum(arrayOfFloats, num);

 

printf("kahan sum : %16.16lf\n", kSum);

 

return 0;

}

 

 

 

sum : 1.0000535249710083, dSum: 0.9999999999999062
kahan sum : 1.0000000000000000

 

 

 

http://sigsam.org/bulletin/articles/147/sumnums.pdf


PARTNERS