Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally, I'm the one who comes in and explains to people that the number they entered is not representable as a floating-point number, and where the rounding errors are, blah blah blah.</p> <p>This question is much more fun than what we usually see, and it illustrates exactly what's wrong with the crowd wisdom of "floating point is inexact, read '<em>what every computer scientist should know...</em>' lolz".</p> <p><code>36.76662445068359375</code> is not just any 19-digit decimal number. It happens to be a 19 digit decimal number that is also exactly representable in double precision binary floating point. Thus, the initial conversion implicit in:</p> <pre><code>double wtf = 36.76662445068359375000; </code></pre> <p>is exact. <code>wtf</code> contains exactly <code>b100100.11000100010000011</code>, and no rounding has occurred. </p> <p>The spec for NSDecimalNumber says that it represents numbers as a 38 digit decimal mantissa and a decimal exponent in the range [-127,128], so the value in <code>wtf</code> is also exactly representable as an NSDecimalNumber. Thus, we may conclude that <code>numberWithDouble</code> is not delivering a correct conversion. Although I cannot find documentation that claims that this conversion routine is correctly rounded, there is no good reason for it not to be. <strong>This is a real bug</strong>, <a href="http://developer.apple.com/bugreporter/" rel="noreferrer">please report it</a>.</p> <p>I note that the string formatters on iPhoneOS seem to deliver correctly rounded results, so you can probably work around this by first formatting the double as a string with 38 digit precision and then using <code>decimalNumberWithString</code>. Not ideal, but it may work for you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload