Ver código fonte

* libee/math.c:

+ ee_sqrt() now uses unsigned ints.
    + Avoid overflows in ee_sqrt().


git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@172 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 21 anos atrás
pai
commit
0c6668d42e
2 arquivos alterados com 19 adições e 14 exclusões
  1. +1
    -1
      libee/ee.h
  2. +18
    -13
      libee/math.c

+ 1
- 1
libee/ee.h Ver arquivo

@@ -95,7 +95,7 @@ void ee_draw_thin_triangle(int, int, int, int, int, int);
void ee_fill_triangle(int, int, int, int, int, int, char);

int ee_rand(int, int);
int ee_sqrt(int);
unsigned int ee_sqrt(unsigned int);

struct ee_sprite * ee_load_sprite(const char *);
int ee_get_sprite_frames(struct ee_sprite *);


+ 18
- 13
libee/math.c Ver arquivo

@@ -32,23 +32,28 @@ int ee_rand(int min, int max)
return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0));
}

int ee_sqrt(int a)
unsigned int ee_sqrt(unsigned int a)
{
int x;
if(a == 0)
return 0;

if(a <= 0)
if(a < 1000000000)
{
return 0;
unsigned int x = a < 10 ? 1
: a < 1000 ? 10
: a < 100000 ? 100
: a < 10000000 ? 1000
: 10000;

/* Newton's method. Three iterations would be more than enough. */
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;

return x;
}

x = a > 100000 ? 1000 : a > 1000 ? 100 : a > 10 ? 10 : 1;

/* Newton's method. Three iterations would be more than enough. */
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;
x = (x * x + a) / x / 2;

return x;
return 2 * ee_sqrt(a / 4);
}


Carregando…
Cancelar
Salvar