xcode - Impossible to achieve certain float value -



xcode - Impossible to achieve certain float value -

i testing code. have boolean conditions float numbers.

i assign float value test code:

float testfloatvalue = 0.9;

and boolean status not met:

if (testfloatvalue == 0.9) { }

because when debugging, float number has changed 0.9 0.899999976

i not understand anything!

due nature of floating point numbers values not possible represented exactly. never want direct check equality... there numerous articles on web , if search find many. here quick routine can utilize in objective c check equal.

bool arealmostequal(double result, double expectedresult) { homecoming fabs(result - expectedresult) < .0000000001; }

you utilize per values in original question:

if(arealmostequal(testfloatvalue, 0.9) { // }

xcode floating-accuracy

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -