How to safely check if a value is NaN when it might be a non-numeric type?