rraallvv
rraallvv

Reputation: 2933

Issue with Math.min on Android app

I’m tracking down an issue that is causing my app to have some flickering when dragging the touch on the screen.

The issue seems to be related to Math.min not returning the right result in some cases.

mPointerPos.mPos.x = Math.min(mPointerPos.mPos.x, 0.9f * mDragStartPos.x);

I changed the function call by a conditional statement but the problem persists.

float min = 0.9f * mDragStartPos.x;
Log.w("!", "" + mPointerPos.mPos.x + " " + min);
mPointerPos.mPos.x = mPointerPos.mPos.x < min ? mPointerPos.mPos.x : min;
Log.w("!\t->", "" + mPointerPos.mPos.x);

The output is:

01-12 08:36:34.133 26229-26229 W/!: -0.3032546
01-12 08:36:34.134 26229-26229 W/!: -0.3032546 -1.4999999
01-12 08:36:34.134 26229-26229 W/!  ->: -1.4999999
01-12 08:36:34.147 26229-26229 W/!: -0.2417283
01-12 08:36:34.148 26229-26229 W/!: -0.2417283 -1.4999999
01-12 08:36:34.152 26229-26229 W/!  ->: -1.5933207
01-12 08:36:34.158 26229-26229 W/!: -0.19270718
01-12 08:36:34.159 26229-26229 W/!: -0.19270718 -1.4999999
01-12 08:36:34.159 26229-26229 W/!  ->: -1.4999999

At line 6 I’m expecting to see -1.4999999, i.e. the minimum value between -0.2417283 and -1.4999999, instead I'm getting -1.5933207

Is there something I’m missing? mPointerPos.mPos and mDragStartPos are of type android.graphics.PointF

Upvotes: 0

Views: 118

Answers (1)

jytou
jytou

Reputation: 550

mPointerPos is obviously updated between the two lines by some other thread. How is mPointerPos updated? Where is this code executed? Answering these questions will probably give you the answer to your problem (which may simply be to add a synchronize block on both sides... or rewrite the entire logic of that part of the program).

Upvotes: 1

Related Questions