c# - Why does this evaluate to False? -


i'm bit confused , can't explain behaviour:

vector3 k = new vector3(mathf.negativeinfinity, mathf.negativeinfinity,mathf.negativeinfinity); debug.log(k==k); // evaluates false 

though

debug.log(mathf.mathf.negativeinfinity == mathf.mathf.negativeinfinity) // evaluates true expected 

i'm using unity version 5.3.5f1.

from unity's documentation, == returns "true vectors close being equal". implementation produces problems when vector initialized negative infinity x,y,z.

let's take @ how == defined vector3:

public static bool operator == (vector3 lhs, vector3 rhs) {     return vector3.sqrmagnitude (lhs - rhs) < 9.999999e-11; } 

before doing sqrmagnitude, first perform lhs - rhs, let's see how - defined:

public static vector3 operator - (vector3 a, vector3 b) {     return new vector3 (a.x - b.x, a.y - b.y, a.z - b.z); } 

this fine normal numbers, however, since a.x, b.x...etc. mathf.negativeinfinity, subtraction result in nan. when sqrmagnitude:

public float sqrmagnitude {     {         return this.x * this.x + this.y * this.y + this.z * this.z;     } } 

this return nan.

from docs, note following:

  • if either operand nan, the result false operators except !=, result true.

therefore, when go code:

return vector3.sqrmagnitude (lhs - rhs) < 9.999999e-11; 

it simplifies return nan < 9.999999e-11; return false stated in docs.


also, reason why debug.log(mathf.mathf.negativeinfinity == mathf.mathf.negativeinfinity) behaves expected documented here.

  • negative , positive zeros considered equal.
  • a negative infinity considered less other values, but equal negative infinity.
  • a positive infinity considered greater other values, equal positive infinity.

Comments