Int.min is the smallest negative value, and Int.max is the largest positive value (that fits in an Int). However, the absolute value of Int.min is larger than the absolute value of Int.max. So you can't convert Int.min into -Int.min because it's larger than Int.max.
In other words, this is expected behaviour :)
For example:
Int.min + Int.max = 1
If they were the same value, it would be zero.
Alex
···
On 22 Sep 2017, at 02:42, Peter W A Wood via swift-users <swift-users@swift.org> wrote:
Entering the following statement in a playground gives an overflow error. Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
Playground execution failed:
error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow
Int.min.dividedReportingOverflow(by:-1)
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.
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.
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.
···
On 22 Sep 2017, at 03:42, Peter W A Wood via swift-users <swift-users@swift.org> wrote:
Entering the following statement in a playground gives an overflow error. Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
Playground execution failed:
error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow
Int.min.dividedReportingOverflow(by:-1)
func dividedReportingOverflow(by other: Int <apple-reference-documentation://hsf4N66ABq>) -> (partialValue: Int <apple-reference-documentation://hsf4N66ABq>, overflow: Bool <apple-reference-documentation://hsvynwdxKM>)
is to report an overflow in the return value. And actually this compiles and runs in Xcode 9 if the code is on top-level in main.m:
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
print(r1) // (partialValue: -9223372036854775808, overflow: true)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
print(r2) // (partialValue: -9223372036854775808, overflow: true)
But the same code inside a function (or do-block) fails to compile:
func foo() {
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
// error: division '-9223372036854775808 / -1' results in an overflow
print(r1)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
// error: division by zero
print(r2)
}
Martin
···
On 22. Sep 2017, at 10:19, Alex Blewitt via swift-users <swift-users@swift.org> wrote:
Int.min is the smallest negative value, and Int.max is the largest positive value (that fits in an Int). However, the absolute value of Int.min is larger than the absolute value of Int.max. So you can't convert Int.min into -Int.min because it's larger than Int.max.
In other words, this is expected behaviour :)
For example:
Int.min + Int.max = 1
If they were the same value, it would be zero.
Alex
On 22 Sep 2017, at 02:42, Peter W A Wood via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Entering the following statement in a playground gives an overflow error. Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
Playground execution failed:
error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow
Int.min.dividedReportingOverflow(by:-1)
On 22 Sep 2017, at 10:14, Martin R <martinr448@gmail.com> wrote:
But the purpose of
func dividedReportingOverflow(by other: Int <apple-reference-documentation://hsf4N66ABq>) -> (partialValue: Int <apple-reference-documentation://hsf4N66ABq>, overflow: Bool <apple-reference-documentation://hsvynwdxKM>)
is to report an overflow in the return value. And actually this compiles and runs in Xcode 9 if the code is on top-level in main.m:
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
print(r1) // (partialValue: -9223372036854775808, overflow: true)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
print(r2) // (partialValue: -9223372036854775808, overflow: true)
But the same code inside a function (or do-block) fails to compile:
func foo() {
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
// error: division '-9223372036854775808 / -1' results in an overflow
print(r1)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
// error: division by zero
print(r2)
}
Martin
On 22. Sep 2017, at 10:19, Alex Blewitt via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Int.min is the smallest negative value, and Int.max is the largest positive value (that fits in an Int). However, the absolute value of Int.min is larger than the absolute value of Int.max. So you can't convert Int.min into -Int.min because it's larger than Int.max.
In other words, this is expected behaviour :)
For example:
Int.min + Int.max = 1
If they were the same value, it would be zero.
Alex
On 22 Sep 2017, at 02:42, Peter W A Wood via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Entering the following statement in a playground gives an overflow error. Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
Playground execution failed:
error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow
Int.min.dividedReportingOverflow(by:-1)
On 22 Sep 2017, at 10:14, Martin R <martinr448@gmail.com <mailto:martinr448@gmail.com>> wrote:
But the purpose of
func dividedReportingOverflow(by other: Int <apple-reference-documentation://hsf4N66ABq>) -> (partialValue: Int <apple-reference-documentation://hsf4N66ABq>, overflow: Bool <apple-reference-documentation://hsvynwdxKM>)
is to report an overflow in the return value. And actually this compiles and runs in Xcode 9 if the code is on top-level in main.m:
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
print(r1) // (partialValue: -9223372036854775808, overflow: true)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
print(r2) // (partialValue: -9223372036854775808, overflow: true)
But the same code inside a function (or do-block) fails to compile:
func foo() {
let minusOne = -1
let r1 = Int.min.dividedReportingOverflow(by: minusOne)
// error: division '-9223372036854775808 / -1' results in an overflow
print(r1)
let zero = 0
let r2 = Int.min.dividedReportingOverflow(by: zero)
// error: division by zero
print(r2)
}
Martin
On 22. Sep 2017, at 10:19, Alex Blewitt via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Int.min is the smallest negative value, and Int.max is the largest positive value (that fits in an Int). However, the absolute value of Int.min is larger than the absolute value of Int.max. So you can't convert Int.min into -Int.min because it's larger than Int.max.
In other words, this is expected behaviour :)
For example:
Int.min + Int.max = 1
If they were the same value, it would be zero.
Alex
On 22 Sep 2017, at 02:42, Peter W A Wood via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Entering the following statement in a playground gives an overflow error. Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
Playground execution failed:
error: MyPlayground.playground:3:9: error: division '-9223372036854775808 / -1' results in an overflow
Int.min.dividedReportingOverflow(by:-1)
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.
···
On Fri, Sep 22, 2017 at 8:41 AM Claude Pommerell via swift-users < swift-users@swift.org> wrote:
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.
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.
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.
On 22 Sep 2017, at 03:42, Peter W A Wood via swift-users < > swift-users@swift.org> wrote:
Entering the following statement in a playground gives an overflow error.
Where should I report this?
Statement:
Int.min.dividedReportingOverflow(by:-1)
Playground log:
*Playground execution failed:*
*error: MyPlayground.playground:3:9: error: division '-9223372036854775808
/ -1' results in an overflow*
*Int.min.dividedReportingOverflow(by:-1)*