From: Stefan Behnel Date: Mon, 21 Apr 2008 06:22:10 +0000 (+0200) Subject: fix inf/NaN float constants X-Git-Tag: 0.9.6.14~20^2~27 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=495011ef41ac250481c84cd0c97ff366a7ec1952;p=cython.git fix inf/NaN float constants --- diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 77aa1bd9..e79ae49c 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -677,6 +677,17 @@ class FloatNode(ConstNode): def compile_time_value(self, denv): return float(self.value) + + def calculate_result_code(self): + strval = str(self.value) + if strval == 'nan': + return "NAN" + elif strval == 'inf': + return "INFINITY" + elif strval == '-inf': + return "(-INFINITY)" + else: + return strval class StringNode(ConstNode): diff --git a/tests/run/ct_DEF.pyx b/tests/run/ct_DEF.pyx index d85cfb06..1d92cc87 100644 --- a/tests/run/ct_DEF.pyx +++ b/tests/run/ct_DEF.pyx @@ -11,6 +11,12 @@ __doc__ = """ 666 >>> f() 12.5 + >>> nan() + nan + >>> infp() + inf + >>> infn() + -inf >>> s() 'spam' >>> two() @@ -32,6 +38,9 @@ DEF INT2 = 0x42 DEF INT3 = 042 DEF LONG = 666L DEF FLOAT = 12.5 +DEF FLOAT_NAN = float('nan') +DEF FLOAT_INFP = float('+inf') +DEF FLOAT_INFN = float('-inf') DEF STR = "spam" DEF TWO = TUPLE[1] DEF FIVE = TWO + 3 @@ -68,6 +77,21 @@ def f(): f = FLOAT return f +def nan(): + cdef float f + f = FLOAT_NAN + return f + +def infp(): + cdef float f + f = FLOAT_INFP + return f + +def infn(): + cdef float f + f = FLOAT_INFN + return f + def s(): cdef char *s s = STR