ios - Strange value of calculated float in Objective-C -


my app calculated this.

float containtaxvalue = 840; float taxrate = 5; float divisionednum = (100 + taxrate)/100; float removedtaxvalue = containtaxvalue/divisionednum; float taxvalue = containtaxvalue - removedtaxvalue; 

finally answer

nslog(@"%f",removedtaxvalue); => 800.000061 nslog(@"%f",containtaxvalue); => 840.000000 nslog(@"%f",taxvalue); => 39.999939 

i "taxvalue == 40.000000" in code.

i couldn't make sense what's issue. let me know advise please.

the ieee 754 standard way of storing floating-point numbers in easy manipulate way machine. method used intel , mot of processors.

ieee 754 specifies numbers stored in binary format reduce storage requirements , allow built-in binary arithmetic instructions available on microprocessors process data in relatively rapid fashion. however, numbers simple, non-repeating decimal numbers converted repeating binary numbers cannot stored perfect accuracy.

1/10 can represented in decimal form .1

but in binary form becomes: .0001100011000111000111 (and on)

and hence rounding-off error occurs. 

you have convert int round off.

the binary conversion of 1.05 goes on 00111111 10000110 01100110 01100110....


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -