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

answer=0.699999988 ( I expect 0.7 ??)

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Ron
  • 3
  • 4
    You should search before asking - this question has been asked on SO 1,262,543 times before in various forms :-) See http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples for some background, and the canonical http://docs.sun.com/source/806-3568/ncg_goldberg.html – paxdiablo Jun 28 '10 at 07:42
  • @paxdiablo:- will you please give any of the useful link out of all for record? – Salil Jun 28 '10 at 07:45
  • @Salil - He's provided some good links, and there's also "The pitfalls of verifying floating-point computations": http://hal.archives-ouvertes.fr/hal-00128124/en/ – Brad Larson Jun 28 '10 at 12:51
  • 2
    possible duplicate of [Problem with calculating floats](http://stackoverflow.com/questions/2879639/problem-with-calculating-floats) – Brad Larson Jun 28 '10 at 12:52

4 Answers4

3

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?

Community
  • 1
  • 1
nos
  • 223,662
  • 58
  • 417
  • 506
1

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

futureelite7
  • 11,462
  • 10
  • 53
  • 87
0

It is because floating point calculations are not precise.

5ound
  • 1,179
  • 6
  • 9
0

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.

tc.
  • 33,468
  • 5
  • 78
  • 96