开发者

Xcode Obj-C Why is 7/10 not 0.7

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

answer=0.699999988 开发者_如何学C ( I expect 0.7 ??)


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?


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).


It is because floating point calculations are not precise.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜