c++ - math functions in thread returning wrong values, with android ndk -



c++ - math functions in thread returning wrong values, with android ndk -

i having issue while trying port code android ndk, doing fine on ios. code renders 3d models , uses opengl es 1.1 that, performs many calculations using standard c math library (including math.h).

here snippet of code used test anomaly:

double e; e = sqrt(25); assert(e == 5); e = sqrt(16); assert(e == 4); e = sqrt(9); assert(e == 3); e = sqrt(4); assert(e == 2); e = sqrt(1); assert(e == 1); e = sqrt(0); assert(e == 0);

these asserts work fust fine anywhere, while this:

int vec[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; int res[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; double d; (int i=0; i<10; i++) { d = sqrt(vec[i]); assert(d == res[i]); }

crashes on thread other main one. example, returns me 0.37500000008731149 sqrt(0), why first assert on loop fails.

it seems problem lies on calculations using content of variables, not calculations on hardcoded values. may problem memory mapping on different threads? debugged it, , printed values of vec[i], though, , correct.

does math library have issues when used in different threads? can give me explanation why behaving weird?

edit: have seen same weird behaviour other math functions, such pow() , sin(). plus, tried sqrtf , powf. both seem give different, wrong, values double counterparts.

d == res[i]

here problem. aside fact you're comparing double , int , have no thought 1 casted other's type, you're comparing floating point numbers == operator.

comparing floats , doubles doesn't work because

4.00000000000001 == 4.0

will homecoming false, , floating point arithmetic not perfect, , things sqrt(16) may not homecoming 4.

instead of using == operator, utilize function (i haven't tested if compiles)

bool areequal(double a, double b) { homecoming < b + 0.00001 && > b - 0.00001; }

instead.

android c++ math android-ndk math.h

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -