<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="">Please note that silently returning an incorrect value instead of crashing is the exact opposite of “safety” as understood in Swift.<div class=""><br class=""></div><div class="">(These should all crash, and as others have reported, they do in my environment. If you can reproduce abs(Int32.min), please report a bug with your swift compiler version and any other environmental details).<br class=""><div class=""><br class=""></div><div class="">– Steve<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2017, at 3:06 PM, C. Keith Ray via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Should these be consistent?&nbsp;</div><div class=""><br class=""></div><div class="">All crashing or none crashing?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="" class="">(</span>abs<span style="" class="">(</span><span style="color: #703daa" class="">Int8</span><span style="" class="">.</span><span style="color: #703daa" class="">min</span><span style="" class="">)) </span><span style="color: #008400" class="">// crash</span></div></div><div class=""><span style="color: #008400" class=""><br class=""></span></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="" class="">(</span>abs<span style="" class="">(</span><span style="color: #703daa" class="">Int16</span><span style="" class="">.</span><span style="color: #703daa" class="">min</span><span style="" class="">)) // crash</span></div></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class=""><span style="" class=""><br class=""></span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: #3e1e81" class="">print</span><span style="" class="">(</span><span style="color: #3e1e81" class="">abs</span><span style="" class="">(</span><span style="color: #703daa" class="">Int32</span><span style="" class="">.</span><span style="color: #703daa" class="">min</span><span style="" class="">)) </span>// prints -2147483648</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">print</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">(</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">abs</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">(</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline; color: rgb(112, 61, 170);" class="">Int64</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">.</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline; color: rgb(112, 61, 170);" class="">min</span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline;" class="">)) </span><span style="font-family: Menlo; font-size: 14px; text-decoration: underline; color: rgb(0, 132, 0);" class="">// cra</span><span style="font-family: Menlo; font-size: 14px; color: rgb(0, 132, 0);" class="">sh</span></div></div></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><span style="" class="">Should this be reported by Radar or another mechanism?</span></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><span style="" class="">I'm using this as a replacement:</span></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span><span style="" class=""> safeAbs&lt;T: </span>SignedInteger<span style="" class=""> &amp; </span>FixedWidthInteger<span style="" class="">&gt;(</span><span style="color: #ba2da2" class="">_</span><span style="" class=""> i: </span><span style="color: #4f8187" class="">T</span><span style="" class="">) -&gt; </span><span style="color: #4f8187" class="">T</span><span style="" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> i &gt;= <span style="color: #272ad8" class="">0</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> i</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; } </span><span style="color: #ba2da2" class="">else</span><span style="" class=""> </span><span style="color: #ba2da2" class="">if</span><span style="" class=""> i == </span><span style="color: #4f8187" class="">T</span><span style="" class="">.</span><span style="color: #703daa" class="">min</span><span style="" class=""> { </span>// can't negate this value</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>// return i &nbsp; // unconverted, violates postcondition, assert(result &gt;= 0)</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>// return 0 &nbsp; // or return an arbitrary "safe" value</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ba2da2" class="">return</span><span style="" class=""> </span><span style="color: #272ad8" class="">-(1</span><span style="" class="">+i) </span>// or return approximately the right value</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; } <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> -i</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93); background-color: rgb(255, 255, 255);" class=""><span style="color: #3e1e81" class="">print</span><span style="" class="">(</span>safeAbs<span style="" class="">(</span><span style="color: #703daa" class="">Int8</span><span style="" class="">.</span><span style="color: #703daa" class="">min</span><span style="" class="">)) // prints&nbsp;</span><b class="">127&nbsp;</b></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(62, 30, 129);" class="">print</span><span style="" class="">(</span><span style="color: rgb(49, 89, 93);" class="">safeAbs</span><span style="" class="">(</span><font color="#703daa" class="">Int16</font><span style="" class="">.</span><font color="#703daa" class="">min</font><span style="" class="">)) //&nbsp;</span>prints&nbsp;32767</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(62, 30, 129);" class="">print</span><span style="" class="">(</span><span style="color: rgb(49, 89, 93);" class="">safeAbs</span><span style="" class="">(</span><font color="#703daa" class="">Int32</font><span style="" class="">.</span><font color="#703daa" class="">min</font><span style="" class="">)) //&nbsp;</span>prints&nbsp;2147483647</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(62, 30, 129);" class="">print</span><span style="" class="">(</span><span style="color: rgb(49, 89, 93);" class="">safeAbs</span><span style="" class="">(</span><font color="#703daa" class="">Int64</font><span style="" class="">.</span><font color="#703daa" class="">min</font><span style="" class="">)) //&nbsp;</span>prints&nbsp;9223372036854775807</div></div><div class=""><span style="" class=""><br class=""></span></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></div></body></html>