-1

Exact Duplicate:
Issue with float and double data types in objective C

[Ironically, to find the duplicate questions you need to know the answer.]

Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 2
    To quote from the [Wikipedia article](http://en.wikipedia.org/wiki/Floating_point) on the subject, "Some numbers (e.g., 1/3 and 0.1) cannot be represented exactly in binary floating-point no matter what the precision." – aroth Jan 12 '12 at 23:09
  • This is a common question having more to do with the general computer science topic of floating point representation than objective c. I'll tag one of the many duplicate questions. – Peter DeWeese Jan 12 '12 at 23:12
  • As for why `NSLog` prints differently than `gdb`, a 32-bit floating point number is good for about 7 digits of precision. So it appears that perhaps `NSLog` is truncating after the seventh digit to give a more meaningful output, while `gdb` is showing the exact value that is stored in your variable without taking precision into account. Interestingly, I think one could argue that both behaviors are correct in their particular context. – aroth Jan 12 '12 at 23:12
  • Are there implications for Javascript and Java of this, or is the problem "virtualized away"? – Dan Rosenstark Jan 12 '12 at 23:13
  • @Yar Same issue for C, C++, C#, Java, JavaScript, etc, etc – PengOne Jan 12 '12 at 23:14
  • @PengOne which is basically no issue, unless you happen to be dealing with numbers that require greater than the precision provided by your data type, right? – Dan Rosenstark Jan 12 '12 at 23:16
  • @Yar Not at all. It means using `==` with `float`s can get you into trouble. They may be the same algebraically, but if they were arrived at in different ways, they may not appear exactly equal. I've been vexed by this issue before. – PengOne Jan 12 '12 at 23:18
  • Duplicate of http://stackoverflow.com/questions/985601/strange-result-of-division-in-c and many others. – rob mayoff Jan 12 '12 at 23:19
  • Thanks @PeterDeWeese I had learned, forgotten and remembered this topic here. It is indeed basic. – Dan Rosenstark Jan 12 '12 at 23:21

2 Answers2

3

What Every Computer Scientist Should Know About Floating-Point Arithmetic

If it cannot be expressed in base 2, it will not be precise. See also floating point inaccuracy.

PengOne
  • 48,188
  • 17
  • 130
  • 149
0

0.1 is a 'repeating decimal' in binary (0.0001100110011...) so the representation of 0.1 is inexact. NSLog is likely rounding or truncating the output.

0x5f3759df
  • 2,349
  • 1
  • 20
  • 25
  • Repeating *decimal* in *binary*? (I know what you meant, that's just an odd way of saying it.() – Keith Thompson Jan 12 '12 at 23:11
  • Calling it a 'repeating binary in binary' sounded worse. Is there a standard way to refer to a number with a repeating fractional portion in other bases? – 0x5f3759df Jan 12 '12 at 23:12
  • "a number with a repeating fractional portion"? But I admit that "repeating decimal" get the idea across clearly. – Keith Thompson Jan 12 '12 at 23:15
  • I think "repeating decimal" is the correct term, regardless of the base. In this case "decimal" refers to "the stuff that comes after the decimal point" and not to the numbers base. Perhaps "binary repeating decimal" is the most concise descriptor? – aroth Jan 12 '12 at 23:20