Float number damaged in selected device

I recently found that the float number issue is corrupted on the selected android device. I was wondering if any of you ran into a similar problem as mine , but is there a way to reproduce it with a simplified block of code?

I faced a similar issue on Nexus 5 devices. The issue does not appear on the Genymotion emulator.

This only happens in the loop selected for the code and is very difficult to re-create in another block of code.

My situation is as follows: -

float rectangleWidth2 = 0.0f;
float startX = (float) (left + xPixelsPerUnit * (xValue - minX));
float stopX = startX;
float _left = startX - rectangleWidth2;
float _right = stopX + rectangleWidth2;

// I expect "_left" and "_right" will have same value. However, at this point,
// "_right" will become an arbitary large value, something like 5.3482353354E20
// However, I expect the value range for "_left" and "_right" within [-1000,1000]

      

If I change the code to

float _left = startX - rectangleWidth2;
float _right = startX + rectangleWidth2;

// "_left" and "_right" will then having same value.

      

A "workable" workaround for my case is to avoid using floats as suggested by the reporter. I use double when possible and only do the necessary floating casting when needed.

The same problem occurs even though I am using Eclipse or Android Studio. I'm going to get my Nexus 4 this week to see if all the problems still occur ...

Links to links

+3


source to share





All Articles