Xcode Obj-C Why is 7/10 not 0.7

2019-09-21 21:42发布

问题:

int a=7
int b=10
float answer = (float)a/b;

answer=0.699999988 ( I expect 0.7 ??)

回答1:

The short version is: Floating points are not accurate, it's only a finite set of bits, and a finite set of bits cannot be used to represent an infinite set of numbers.

The longer version is here: What Every Computer Scientist Should Know About Floating-Point Arithmetic

See also:

How is floating point stored? When does it matter?

Why is my number being rounded incorrectly?



回答2:

Floating point numbers are accurate only to a certain finite number of digits of precision. You will need to do some rounding to get whole numbers.

If you need more precision, use the double data type, or the NSDecimal class (Which will preserve your decimal digits at the expense of complexity).



回答3:

It is because floating point calculations are not precise.



回答4:

The only thing I rely on is the existence of exact small integers (namely -2, -1, 0, 1, 2, as you might use for representing [0,1] plus some special values), and some people frown on using that too.