<div dir="ltr">this is correct behavior. <span style="font-family:monospace,monospace">−Int.min</span> can’t be represented in signed 2’s complement because the range of <span style="font-family:monospace,monospace">Int</span> is <span style="font-family:monospace,monospace">2^(bitWidth - 1) ..< 2^(bitWidth - 1)</span><span style="font-family:arial,helvetica,sans-serif"> so it correctly traps on </span><span style="font-family:monospace,monospace">SIGILL</span>. what i’m more worried about is why <span style="font-family:monospace,monospace">abs(Int32.min)</span> is returning a value for you,, but mine traps as it should.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 12, 2017 at 2:06 PM, C. Keith Ray via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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"><div>Should these be consistent? </div><div><br></div><div>All crashing or none crashing?</div><div><br></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)">print<span style="color:#000000">(</span>abs<span style="color:#000000">(</span><span style="color:#703daa">Int8</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) </span><span style="color:#008400">// crash</span></div></div><div><span style="color:#008400"><br></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)">print<span style="color:#000000">(</span>abs<span style="color:#000000">(</span><span style="color:#703daa">Int16</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) // 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)"><span style="color:#000000"><br></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)"><div style="margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:#3e1e81">print</span><span style="color:#000000">(</span><span style="color:#3e1e81">abs</span><span style="color:#000000">(</span><span style="color:#703daa">Int32</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) </span>// prints -<a href="tel:(214)%20748-3648" value="+12147483648" target="_blank">2147483648</a></div><div><br></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><span style="font-family:Menlo;font-size:14px;text-decoration:underline">print</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">(</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline">abs</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">(</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(112,61,170)">Int64</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">.</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(112,61,170)">min</span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,0,0)">)) </span><span style="font-family:Menlo;font-size:14px;text-decoration:underline;color:rgb(0,132,0)">// cra</span><span style="font-family:Menlo;font-size:14px;color:rgb(0,132,0)">sh</span></div></div></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">Should this be reported by Radar or another mechanism?</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">I'm using this as a replacement:</span></div><div><span style="color:#000000"><br></span></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(112,61,170);background-color:rgb(255,255,255)"><span style="color:#ba2da2">func</span><span style="color:#000000"> safeAbs<T: </span>SignedInteger<span style="color:#000000"> & </span>FixedWidthInteger<span style="color:#000000">>(</span><span style="color:#ba2da2">_</span><span style="color:#000000"> i: </span><span style="color:#4f8187">T</span><span style="color:#000000">) -> </span><span style="color:#4f8187">T</span><span style="color:#000000"> {</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">if</span> i >= <span style="color:#272ad8">0</span> {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">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)"><span style="color:#000000"> } </span><span style="color:#ba2da2">else</span><span style="color:#000000"> </span><span style="color:#ba2da2">if</span><span style="color:#000000"> i == </span><span style="color:#4f8187">T</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000"> { </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)"><span style="color:#000000"> </span>// return i // unconverted, violates postcondition, assert(result >= 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)"><span style="color:#000000"> </span>// return 0 // 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)"><span style="color:#000000"> </span><span style="color:#ba2da2">return</span><span style="color:#000000"> </span><span style="color:#272ad8">-(1</span><span style="color:#000000">+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)"> } <span style="color:#ba2da2">else</span> {</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> -i</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;background-color:rgb(255,255,255);min-height:14px"><br></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)"><span style="color:#3e1e81">print</span><span style="color:#000000">(</span>safeAbs<span style="color:#000000">(</span><span style="color:#703daa">Int8</span><span style="color:#000000">.</span><span style="color:#703daa">min</span><span style="color:#000000">)) // prints </span><b>127 </b></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int16</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints 32767</div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int32</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints <a href="tel:(214)%20748-3647" value="+12147483647" target="_blank">2147483647</a></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(49,89,93)">safeAbs</span><span style="color:rgb(0,0,0)">(</span><font color="#703daa">Int64</font><span style="color:rgb(0,0,0)">.</span><font color="#703daa">min</font><span style="color:rgb(0,0,0)">)) // </span>prints 9223372036854775807</div></div><div><span style="color:#000000"><br></span></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>