<div dir="ltr">On Wed, Apr 19, 2017 at 3:19 PM, Martin R <span dir="ltr">&lt;<a href="mailto:martinr448@gmail.com" target="_blank">martinr448@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span class=""><blockquote type="cite"><div>On 19. Apr 2017, at 01:48, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="m_2570945702166324521Apple-interchange-newline"><div><div dir="ltr">So, as I understand it, `Float.init(exactly: Double.pi) == nil`. I would expect NSNumber to behave similarly (a notion with which Martin disagrees, I guess). I don&#39;t see a test that shows whether NSNumber behaves or does not behave in that way.</div></div></blockquote><div><br></div></span><div>At present they behave differently: </div><div><br></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Double</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)"><wbr>pi</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">Any</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">    </span><span style="font-variant-ligatures:no-common-ligatures">// nil</span></div></div><span class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">NSNumbe<wbr>r</span><span style="font-variant-ligatures:no-common-ligatures">(value: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Double</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">pi</span><span style="font-variant-ligatures:no-common-ligatures">)) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">Any</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">    </span><span style="font-variant-ligatures:no-common-ligatures">// Optional(3.14159274)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">I realize that identical behavior would be logical and least surprising. My only concern was about cases like</span></div></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> num = ... </span><span style="font-variant-ligatures:no-common-ligatures">// some NSNumber from a JSON deserialization</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> fval = </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Float</span><span style="font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">num</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal">where one cannot know how the number is represented internally and what precision it needs. But then one could use the truncating conversion or `.floatValue` instead.</div></div></div></div></div></blockquote><div><br></div><div>JSON numbers are double-precision floating point, unless I&#39;m misunderstanding something. If someone writes `Float(exactly: valueParsedFromJSON)`, surely, that can only mean that they *really, really* prefer nil over an imprecise value. I can see no other reason to insist on using both Float and .init(exactly:).</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"></div></div></div><span class=""><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 18, 2017 at 11:43 AM, Philippe Hausler <span dir="ltr">&lt;<a href="mailto:phausler@apple.com" target="_blank">phausler@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div class="m_2570945702166324521h5"><br><div><blockquote type="cite"><div>On Apr 18, 2017, at 9:22 AM, Stephen Canon &lt;<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>&gt; wrote:</div><br class="m_2570945702166324521m_-6340981287830468260Apple-interchange-newline"><div><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br class="m_2570945702166324521m_-6340981287830468260Apple-interchange-newline">On Apr 18, 2017, at 12:17 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>&gt; wrote:<br><br><br><blockquote type="cite">On Apr 17, 2017, at 5:56 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br>It seems Float.init(exactly: NSNumber) has not been updated to behave similarly?<br><br>I would have to say, I would naively expect &quot;exactly&quot; to behave exactly as it says, exactly. I don&#39;t think it should be a synonym for Float(Double(exactly:)).<br>On Mon, Apr 17, 2017 at 19:24 Philippe Hausler via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>I posted my branch and fixed up the Double case to account for your concerns (with a few inspired unit tests to validate)<br><br><a href="https://github.com/phausler/swift/tree/safe_nsnumber" target="_blank">https://github.com/phausler/sw<wbr>ift/tree/safe_nsnumber</a><br><br>There is a builtin assumption here though: it does presume that the swift’s representation of Double and Float are IEEE compliant. However that is a fairly reasonable assumption in the tests.<br></blockquote><br></blockquote></div></blockquote></div></div></div></div></blockquote></div></div></div></div></blockquote><div><br></div><div><br></div></span><div>Even with the updated code at <a href="https://github.com/phausler/swift/tree/safe_nsnumber" target="_blank">https://github.com/<wbr>phausler/swift/tree/safe_<wbr>nsnumber</a><span class=""><div><br></div><div><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">    </span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">print</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">(</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Double</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">(exactly: </span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">NSNumb<wbr>er</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">(value: </span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int64</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">(</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">90000000000000<wbr>00001</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">))) </span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">as</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> </span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">Any</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">)</span></div></span><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">    </span><span style="font-variant-ligatures:no-common-ligatures">// Optional(9e+18)</span></div><div><br></div><div>still succeeds, however the reason seems to be an error in the `init(exactly value: someIntegerType)` inititializers of Float/Double, I have submitted a bug report: <a href="https://bugs.swift.org/browse/SR-4634" target="_blank">https://bugs.swift.<wbr>org/browse/SR-4634</a>.</div><div><br></div></div></div><span class=""><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div class="m_2570945702166324521h5"><div><blockquote type="cite"><div><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">(+Steve Canon) What is the behavior of Float.init(exactly: Double)? NSNumber&#39;s behavior would ideally be consistent with that.<br></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">The implementation is essentially just:</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span class="m_2570945702166324521m_-6340981287830468260Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">self.init(other)</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span class="m_2570945702166324521m_-6340981287830468260Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">guard Double(self) == other else {</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span class="m_2570945702166324521m_-6340981287830468260Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span class="m_2570945702166324521m_-6340981287830468260Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">return nil</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span class="m_2570945702166324521m_-6340981287830468260Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">}</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">i.e. if the result is not equal to the source when round-tripped back to double (which is always exact), the result is nil.</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">– Steve</span></div></blockquote></div><br></div></div><div>Pretty much the same trick inside of CFNumber/NSNumber</div></div></blockquote></div><br></div></div>
</div></blockquote></span></div><br></div></blockquote></div><br></div></div>