math.isnan(x)
wird ein TypeError
erhöhen, wenn x
ist weder ein float
noch ein Real
.
Es ist besser, etwas zu verwenden:
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
Sie dann self.assertIsNaN()
und self.assertIsNotNaN()
verwenden können.
möglich Duplikat von [Wie testen Sie, um festzustellen, ob ein Double gleich NaN in Java ist?] (Http://stackoverflow.com/questions/1456566/how-do-you-test-to-see-if -a-double-ist-gleich-zu-nan-in-java) – Raedwald