math.isnan(x)
поднимет TypeError
если x
не является ни float
, ни Real
.
Это лучше использовать что-то вроде этого:
import math
class NumericAssertions:
"""
This class is following the UnitTest naming conventions.
It is meant to be used along with unittest.TestCase like so :
class MyTest(unittest.TestCase, NumericAssertions):
...
It needs python >= 2.6
"""
def assertIsNaN(self, value, msg=None):
"""
Fail if provided value is not NaN
"""
standardMsg = "%s is not NaN" % str(value)
try:
if not math.isnan(value):
self.fail(self._formatMessage(msg, standardMsg))
except:
self.fail(self._formatMessage(msg, standardMsg))
def assertIsNotNaN(self, value, msg=None):
"""
Fail if provided value is NaN
"""
standardMsg = "Provided value is NaN"
try:
if math.isnan(value):
self.fail(self._formatMessage(msg, standardMsg))
except:
pass
Вы можете использовать self.assertIsNaN()
и self.assertIsNotNaN()
.
Возможный дубликат [Как вы проверяете, соответствует ли двойное значение NaN в Java?] (Http://stackoverflow.com/questions/1456566/how-do-you-test-to-see-if -a-дважды в равных к нан-в-Java) – Raedwald