<div dir="ltr">The bug is about the compiler reporting a compile error for a function that purposely is designed to allow for overflow and handling of said overflow. It should be allowed to be used in all scopes in code. It seems like the compiler is able to reason and prove overflow will happen in certain code scopes (but not all) resulting in a compile time error (which is nice) however it is doing this with a stdlib function that is meant to deal with overflow so it should in theory be allowed.</div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 22, 2017 at 8:41 AM Claude Pommerell via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<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>This is not a bug. The error reports correctly that -Int.min cannot be represented in the Int type. This holds true for all implementations of fixed-size signed integers in Swift and most languages. In fact, Int.min is defined as Int.min = -Int.max-1.</div><div><br></div><div>The reason is that almost all fixed-sized implementations of signed integers can represent an even number of integers. Since zero is both positive and negative, they cannot represent an equal number of positive and negative numbers. By convention, there is one more negative number, because arithmetics is easier to implement electronically this way.</div><div><br></div><div>Zero is represented in binary by 000…000, -1 by 111…111, Int.max by 011…111, and Int.min by 100…000. The first bit can be interpreted as a sign bit, it is 0 for all positive numbers, and 1 for all non-positive (that is, smaller than zero) numbers. You can negate all numbers by inverting all of their bits and than adding 1, ignoring the overflow to the unrepresentable 1000…000 when passing from -1 to 0, and signalling an overflow when inverting Int.min. All additions can be implemented as if the binary representations were unsigned, checking for overflow only if both terms have the same sign.</div></div><div style="word-wrap:break-word"><div><br></div><div><blockquote type="cite"><div>On 22 Sep 2017, at 03:42, Peter W A Wood via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-8701545093223739620Apple-interchange-newline"><div><div style="word-wrap:break-word">Entering the following statement in a playground gives an overflow error. Where should I report this?<div><br></div><div>Statement:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)"><span style="color:#703daa">Int</span><span>.</span><span style="color:#703daa">min</span><span>.</span><span style="text-decoration:underline">d</span>ividedReportingOverflow<span>(by:</span><span style="color:#272ad8">-1</span><span>)</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(62,30,129);background-color:rgb(255,255,255)"><span><br></span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)">Playground log:</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>Playground execution failed:</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"><b></b></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow</b></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>Int.min.dividedReportingOverflow(by:-1)</b></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><b><br></b></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Peter</span></div></div></div>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div>