Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float sum;
  6. float dl[ 100000 ];
  7.  
  8. // Aby zminimalizowac bledy uzyjemy algorytmu Kahana:
  9. float kahanSum();
  10.  
  11. int main() {
  12.     srand( time(NULL) );
  13.     int i;
  14.     for( i = 0; i < 100000; ++i ) {
  15.         // Zrobimy sobie kwadrat o boku 100000 i będziemy losować punkty:
  16.         int a = rand()%100000, b = rand()%100000;
  17.  
  18.         // Jeden punkt ma współrzędne (0, a), drugi ma (b, 0) - w końcu są to sąsiednie boki.
  19.         // Liczymy wektory:
  20.         int vec_x = b, vec_y = -a;
  21.         // Liczymy dlugosc wektora
  22.         float dl_ = sqrt( pow( vec_x, 2 ) + pow( vec_y, 2 ) );
  23.         dl[i] = dl_;
  24.     }
  25.  
  26.     sum = kahanSum();
  27.     // Skoro w specyfikacji dzialania mielismy kwadrat jednostkowy, to podzielmy.
  28.     float sr = sum / 100000. / 100000.;
  29.     printf( "%0.12f\n", sr );
  30.     return 0;
  31. }
  32.  
  33.  
  34. float kahanSum() {
  35.     float s = dl[0];
  36.     float c = 0.;
  37.  
  38.     int i;
  39.     for( i = 1; i < 100000; ++i ) {
  40.         float y = dl[i] - c;
  41.         float t = s + y;
  42.         c = (t-s) - y;
  43.     }
  44.     return s;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement