handle inf and nan