Mercurial > imath
changeset 4:d0ddac11aeef draft
Fix bugs in asin
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Thu, 22 Dec 2016 13:26:17 +0100 |
| parents | 230eac2a984f |
| children | 48601e076cb7 |
| files | asin.py imath.c |
| diffstat | 2 files changed, 22 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/asin.py Wed Dec 21 23:59:36 2016 +0100 +++ b/asin.py Thu Dec 22 13:26:17 2016 +0100 @@ -17,8 +17,8 @@ ANGLE( 0.448), # atan(1/128) ANGLE( 0.224), # atan(1/256) ANGLE( 0.112), # atan(1/512) - ANGLE( 0.056), # atan(1/1024) - ANGLE( 0.028), # atan(1/2048) +# ANGLE( 0.056), # atan(1/1024) +# ANGLE( 0.028), # atan(1/2048) ] scale = 1 << 14 @@ -35,23 +35,22 @@ x = scale y = 0 for i, atan in enumerate(ATAN): - if y < value: - for j in range(2): - tx = x - x -= y>>i - y += tx>>i - result += atan << 1 - else: - for j in range(2): - tx = x - x += y>>i - y -= tx>>i - result -= atan << 1 + for j in range(2): + dx = y >> i + dy = x >> i + dr = atan + if y < value: + dx = -dx + dy = -dy + dr = -dr + x += dx + y -= dy + result -= dr + value += value >> (i << 1) mx = max(x, mx) my = max(y, my) nx = min(x, nx) ny = min(y, ny) - value += value >> (i << 1) mv = max(value, mv) nv = min(value, nv) return result
--- a/imath.c Wed Dec 21 23:59:36 2016 +0100 +++ b/imath.c Thu Dec 22 13:26:17 2016 +0100 @@ -66,32 +66,32 @@ } /* - * Takes a number between -1<<14 and 1<<14 and returns an angle between - * -1800 (for -180°) and 1800 (for 180°). + * Takes a number between -1<<13 and 1<<13 and returns an angle between + * -900 (for -90°) and 900 (for 90°). */ int16_t iasin(int16_t value) { int16_t result = 0; - int16_t x = SCALE; + int16_t x = 1<<13; int16_t y = 0; int16_t dx, dy, dr; for (uint8_t i = 0; i < ITERATIONS; ++i) { for (uint8_t j = 0; j < 2; ++j) { dx = y >> i; - dy = -(x >> i); - dr = -pgm_read_word(&ATAN[i]); + dy = x >> i; + dr = pgm_read_word(&ATAN[i]); if (y < value) { dx = -dx; dy = -dy; dr = -dr; } x += dx; - y += dy; - result += dr; + y -= dy; + result -= dr; } value += value >> (i << 1); } - return value; + return result; } /*
