How to guard from SIMD3<Float>(-nan, -nan, -nan)

Hello, in some point on my code, I have the value

    ...
    let puckcoords: SIMD3<Float>
    if let puck = puck {
        puckcoords = [Float(puck.x), Float(puck.y), Float(puck.z)]
    } else {
        puckcoords = [0, 0, 0]
    }

puckcoords: SIMD3(-nan, -nan, -nan)

And then this value pass to Metal API, and then Metal catapulted. I don't know why it happens, I just want to install the guard statement in the middle of this function to prevent pass this value to Metal. How to check SIMD3 if it contains the nan?

Oh. I found isNaN property in Docs. Hope it will work...

To test if at least one coordinate is NaN:

if any(puckcoords .!= puckcoords) { puckcoords = [0, 0, 0] }

Use all if you only want it to be replaced when every coordinate is NaN.

3 Likes

I found the reason. I tried to get normalized vectors from zero vectors, like here

    public func getNan() {
        let z: SIMD2<Double> = [1, 0]
        let x: SIMD2<Double> = [2, 0]
        let r = z - z // (miss type)
        let n = simd_normalize(r)
        print(n) //  SIMD2<Double>(-nan, -nan)
    }