abs() Bug?

Should these be consistent?

All crashing or none crashing?

print(abs(Int8.min)) // crash

print(abs(Int16.min)) // crash

print(abs(Int32.min)) // prints -2147483648

print(abs(Int64.min)) // crash

Should this be reported by Radar or another mechanism?

I'm using this as a replacement:

func safeAbs<T: SignedInteger & FixedWidthInteger>(_ i: T) -> T {
    if i >= 0 {
        return i
    } else if i == T.min { // can't negate this value
        // return i // unconverted, violates postcondition, assert(result >= 0)
        // return 0 // or return an arbitrary "safe" value
        return -(1+i) // or return approximately the right value
    } else {
        return -i
    }
}

print(safeAbs(Int8.min)) // prints 127
print(safeAbs(Int16.min)) // prints 32767
print(safeAbs(Int32.min)) // prints 2147483647
print(safeAbs(Int64.min)) // prints 9223372036854775807

this is correct behavior. −Int.min can’t be represented in signed 2’s
complement because the range of Int is 2^(bitWidth - 1) ..< 2^(bitWidth - 1)
so it correctly traps on SIGILL. what i’m more worried about is why
abs(Int32.min) is returning a value for you,, but mine traps as it should.

···

On Tue, Dec 12, 2017 at 2:06 PM, C. Keith Ray via swift-users < swift-users@swift.org> wrote:

Should these be consistent?

All crashing or none crashing?

print(abs(Int8.min)) // crash

print(abs(Int16.min)) // crash

print(abs(Int32.min)) // prints -2147483648 <(214)%20748-3648>

print(abs(Int64.min)) // crash

Should this be reported by Radar or another mechanism?

I'm using this as a replacement:

func safeAbs<T: SignedInteger & FixedWidthInteger>(_ i: T) -> T {
    if i >= 0 {
        return i
    } else if i == T.min { // can't negate this value
        // return i // unconverted, violates postcondition,
assert(result >= 0)
        // return 0 // or return an arbitrary "safe" value
        return -(1+i) // or return approximately the right value
    } else {
        return -i
    }
}

print(safeAbs(Int8.min)) // prints *127 *
print(safeAbs(Int16.min)) // prints 32767
print(safeAbs(Int32.min)) // prints 2147483647 <(214)%20748-3647>
print(safeAbs(Int64.min)) // prints 9223372036854775807

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Either Radar or bugs.swift.org <Issues · apple/swift · GitHub; is fine.

···

On Dec 12, 2017, at 12:06 PM, C. Keith Ray via swift-users <swift-users@swift.org> wrote:

Should this be reported by Radar or another mechanism?

--
Greg Parker gparker@apple.com <mailto:gparker@apple.com> Runtime Wrangler

Please note that silently returning an incorrect value instead of crashing is the exact opposite of “safety” as understood in Swift.

(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).

– Steve

···

On Dec 12, 2017, at 3:06 PM, C. Keith Ray via swift-users <swift-users@swift.org> wrote:

Should these be consistent?

All crashing or none crashing?

print(abs(Int8.min)) // crash

print(abs(Int16.min)) // crash

print(abs(Int32.min)) // prints -2147483648

print(abs(Int64.min)) // crash

Should this be reported by Radar or another mechanism?

I'm using this as a replacement:

func safeAbs<T: SignedInteger & FixedWidthInteger>(_ i: T) -> T {
    if i >= 0 {
        return i
    } else if i == T.min { // can't negate this value
        // return i // unconverted, violates postcondition, assert(result >= 0)
        // return 0 // or return an arbitrary "safe" value
        return -(1+i) // or return approximately the right value
    } else {
        return -i
    }
}

print(safeAbs(Int8.min)) // prints 127
print(safeAbs(Int16.min)) // prints 32767
print(safeAbs(Int32.min)) // prints 2147483647
print(safeAbs(Int64.min)) // prints 9223372036854775807

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

They all crash when I test it with Xcode 9.2 (default toolchain and dev
snapshot 2017-12-09, command line app), in what environment are you seeing
the non-crashing behavior?

My guess is that all of them crashing is the intended behavior, since the
operations result in an overflow..

/Jens

···

On Tue, Dec 12, 2017 at 9:06 PM, C. Keith Ray via swift-users < swift-users@swift.org> wrote:

Should these be consistent?

All crashing or none crashing?

print(abs(Int8.min)) // crash

print(abs(Int16.min)) // crash

print(abs(Int32.min)) // prints -2147483648

print(abs(Int64.min)) // crash

Should this be reported by Radar or another mechanism?

I'm using this as a replacement:

func safeAbs<T: SignedInteger & FixedWidthInteger>(_ i: T) -> T {
    if i >= 0 {
        return i
    } else if i == T.min { // can't negate this value
        // return i // unconverted, violates postcondition,
assert(result >= 0)
        // return 0 // or return an arbitrary "safe" value
        return -(1+i) // or return approximately the right value
    } else {
        return -i
    }
}

print(safeAbs(Int8.min)) // prints *127 *
print(safeAbs(Int16.min)) // prints 32767
print(safeAbs(Int32.min)) // prints 2147483647
print(safeAbs(Int64.min)) // prints 9223372036854775807

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users