Perlin noise generator in Swift

空扰寡人 提交于 2019-11-30 22:26:00
Martin R

The exception is caused by an "arithmetic overflow" that occurs if the result of one your calculations cannot be represented as an Int.

Unlike (Objective-)C, adding and multiplying integers in Swift does not "wrap around" or "truncate", but causes an error if the result does not fit into the data type.

But you can use the Swift "overflow operators" &* and &+ instead, which always truncate the result:

func makeNoise1D(x : Int) -> Float{
    var x = x
    x = (x >> 13) ^ x;
    x = (x &* (x &* x &* seed! &+ 19990303) &+ 1376312589) & 0x7fffffff
    let inner = (x &* (x &* x &* 15731 &+ 789221) &+ 1376312589) & 0x7fffffff
    return ( 1.0 - ( Float(inner) ) / 1073741824.0)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!