<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">I posted my branch and fixed up the Double case to account for your concerns (with a few inspired unit tests to validate)</div><div class=""><br class=""></div><div class=""><a href="https://github.com/phausler/swift/tree/safe_nsnumber" class="">https://github.com/phausler/swift/tree/safe_nsnumber</a></div><div class=""><br class=""></div><div class="">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.</div><div><br class=""><blockquote type="cite" class=""><div class="">On Apr 15, 2017, at 13:12, Philippe Hausler <<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Apr 14, 2017, at 22:51, Martin R <<a href="mailto:martinr448@gmail.com" class="">martinr448@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thank you for the response, but I have more questions. Will</div><div class=""><br class=""></div><div class=""> Float(exactly: NSNumber(value: Double.pi))</div></div></div></blockquote><div class=""><br class=""></div><div class="">This will succeed in that the value is representable without loosing mantissa</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""> </div><div class="">fail because Float cannot represent the number Double.pi exactly? Or</div><div class=""><br class=""></div><div class=""> Double(exactly: NSDecimalNumber(string: "1.9”))</div></div></div></blockquote><div class=""><br class=""></div><div class="">Again it would be representable without losing mantissa but… </div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""> </div><div class="">because Double cannot represent the decimal fraction 1.9 exactly?</div></div></div></blockquote><div class=""><br class=""></div><div class="">Neither can NSDecimalNumber btw ;X and NSDecimalNumber is not in the scope of this proposal (it is it’s own type and bridges to Decimal)</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I find it difficult to evaluate the proposal without a description of the intended behavior of the "exact" conversions which covers all possible combinations (integers, floating point values, booleans). At present, the behavior is described only for stored integer types.</div></div></div></blockquote><div class=""><br class=""></div><div class="">I can post the patch but the real machinery is in NSNumber itself. The primary test is that if the value can round trip as the expected type and evaluate to equal to a NSNumber it will.</div><div class=""><br class=""></div><div class="">The conversion roughy is a cast to and from the stored type;</div><div class=""><br class=""></div><div class="">extension Double {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init?(exactly number: NSNumber) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let value = number.doubleValue</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>guard NSNumber(value: value) == number else { return nil }</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>self = value</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">The effective result of this is a verification of the stored type being equal to the fetched value. But specifically this only traverses via tagged pointers (if the are present). It is worth noting that this is not the exact implementation but an approximation with public apis.</div><div class=""><br class=""></div><div class="">Overall this is by far a better behavior than just permissively allowing all conversions (which is the current alternative of doing nothing…). As one of the responsible maintainers for NSNumber I would claim that a generally permissive cast as it is today is incorrect usage of NSNumber.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Regards, Martin</div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><br class=""></span></font><div class=""><blockquote type="cite" class=""><div class="">On 14. Apr 2017, at 23:23, Philippe Hausler <<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><br class="Apple-interchange-newline">On Apr 14, 2017, at 2:11 PM, Martin R via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">Apologies if I am overlooking something, but it seems to me that the proposal does not clearly define the behavior of the "exact" conversions between integer and floating point values. Does</div><div class=""><br class=""></div><div class=""> Int(exactly: NSNumber(value: 12.34))</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;" class="">The exact value of a float or double constructed NSNumber will only happen for integral values: e.g. 1.0, -32.0 etc</div><br class="" style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;"><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">fail because Int cannot represent the number exactly? Or are floating point values truncated silently and the conversion to an integer fails only if it overflows? And the other way around: Does</div><div class=""><br class=""></div><div class=""> Double(exactly: NSNumber(value: Int64(9000000000000000001)))</div><div class=""> </div><div class="">fail because Double cannot represent the number exactly?</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;" class="">I believe this will fail because the Int64 value will exceed the mantissa representation of the Double from my current implementation. </div><br class="" style="font-family: Helvetica; font-size: 13px; 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; -webkit-text-stroke-width: 0px;"><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">Regards, Martin</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On 14. Apr 2017, at 20:45, Ben Cohen via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">Apologies, if you are reading this in HTML the links appear to be pointing to the incorrect proposal. </div><div class=""><br class=""></div><div class="">Here is the corrected link:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md</a></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On Apr 14, 2017, at 11:30 AM, Ben Cohen <<a href="mailto:ben_cohen@apple.com" class="">ben_cohen@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hello Swift community,<br class=""><br class="">The review of “SE-0170: NSNumber bridging and Numeric types" begins now and runs through the Friday after next, April 14th. The proposal is available here:<div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md</a></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class=""><div class="">Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div><div class="">or, if you would like to keep your feedback private, directly to the review manager. When replying, please try to keep the proposal link at the top of the message:</div><div class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>Proposal link: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0170-nsnumber_bridge.md</a></div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>Reply text</div><div class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>Other replies</div><div class=""><br class=""></div><div class=""><b class="">What goes into a review?</b><br class=""><br class="">The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:<br class=""><br class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>• What is your evaluation of the proposal?<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>• Is the problem being addressed significant enough to warrant a change to Swift?<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>• Does this proposal fit well with the feel and direction of Swift?<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>• If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>• How much effort did you put into your review? A glance, a quick reading, or an in-depth study?<br class=""></div><div class=""><br class=""></div>More information about the Swift evolution process is available at <a href="https://github.com/apple/swift-evolution/blob/master/process.md" class="">https://github.com/apple/swift-evolution/blob/master/process.md</a><div class=""><br class=""><div class="">Thank you,</div><div class=""><br class=""></div><div class="">Ben Cohen</div><div class="">Review Manager</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>