整数溢出在Swift中给出了EXC_BAD_INSTRUCTION

TGO*_*TGO 6 xcode swift xcode7-beta3

在搞乱Swift时,我注意到当64位整数溢出时,我收到以下错误:

file:///Users/user/Documents/playground/MyPlayground.playground/:错误:游乐场执行中止:执行被中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0).

func fibonacci(which: Int) -> (fibOf: Int, isEqualTo: Int) {
    var i = 1, j = 1

    for var k = 2; k < which; k += 1 {
        let tmp = i + j // this line is highlighted when error occurs
        j = i
        i = tmp
    }

    return (which, i)
}

print (fibonacci(92))
print (fibonacci(93)) // this triggers an error
Run Code Online (Sandbox Code Playgroud)

第一个调用,即92作为参数,将运行良好.但是,当提供93值时,我得到了无关的EXC_BAD_INSTRUCTION错误.这是一个错误还是什么?通常我会期望它溢出.

Tom*_*art 10

这是预期的行为.如果要溢出,则需要使用溢出运算符.

  • 溢出添加(&+)
  • 溢出减法(&-)
  • 溢出乘法(&*)

  • 嗯,我明白了.但是,不直观的错误消息.谢谢 (2认同)