Skip to content

Commit d64be7f

Browse files
bauomEmilyBourne
andauthored
improved print_constant (pyccel#1120)
Print unknown constants using their values. This fixes pyccel#1047 but may be reverted in pyccel#1811 to prefer declaring constants with names. However this PR also adds support for `NaN` in C and an associated test. It also removes the duplicate `_print_Constant` function. --------- Co-authored-by: EmilyBourne <[email protected]>
1 parent 0d5c8c2 commit d64be7f

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ All notable changes to this project will be documented in this file.
3333
- #1785 : Add missing cast when creating an array of booleans from non-boolean values.
3434
- #1821 : Ensure an error is raised when creating an ambiguous interface.
3535
- #1842 : Fix homogeneous tuples incorrectly identified as inhomogeneous.
36+
- #1853 : Fix translation of a file whose name conflicts with Fortran keywords.
37+
- Link and mention `devel` branch, not `master`.
38+
- #1047 : Print the value of an unrecognised constant.
3639

3740
### Changed
3841

pyccel/codegen/printing/ccode.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from pyccel.ast.basic import ScopedAstNode
1111

12-
from pyccel.ast.builtins import PythonRange, PythonComplex
12+
from pyccel.ast.builtins import PythonRange, PythonComplex, DtypePrecisionToCastFunction
1313
from pyccel.ast.builtins import PythonPrint, PythonType
1414
from pyccel.ast.builtins import PythonList, PythonTuple
1515

@@ -1963,28 +1963,6 @@ def _print_FunctionCall(self, expr):
19631963
else:
19641964
return call_code
19651965

1966-
def _print_Constant(self, expr):
1967-
""" Convert a Python expression with a math constant call to C
1968-
function call
1969-
1970-
Parameters
1971-
----------
1972-
expr : Pyccel ast node
1973-
Python expression with a Math constant
1974-
1975-
Returns
1976-
-------
1977-
string
1978-
String represent the value of the constant
1979-
1980-
Example
1981-
-------
1982-
math.pi ==> 3.14159265358979
1983-
1984-
"""
1985-
val = LiteralFloat(expr.value)
1986-
return self._print(val)
1987-
19881966
def _print_Return(self, expr):
19891967
code = ''
19901968
args = [ObjectAddress(a) if isinstance(a, Variable) and self.is_c_pointer(a) else a for a in expr.expr]
@@ -2329,14 +2307,19 @@ def _print_Constant(self, expr):
23292307
if expr == math_constants['inf']:
23302308
self.add_import(c_imports['math'])
23312309
return 'HUGE_VAL'
2310+
elif expr == math_constants['nan']:
2311+
self.add_import(c_imports['math'])
2312+
return 'NAN'
23322313
elif expr == math_constants['pi']:
23332314
self.add_import(c_imports['math'])
23342315
return 'M_PI'
23352316
elif expr == math_constants['e']:
23362317
self.add_import(c_imports['math'])
23372318
return 'M_E'
23382319
else:
2339-
raise NotImplementedError("Constant not implemented")
2320+
cast_func = DtypePrecisionToCastFunction[expr.dtype]
2321+
return self._print(cast_func(expr.value))
2322+
23402323

23412324
def _print_Variable(self, expr):
23422325
if self.is_c_pointer(expr):

pyccel/codegen/printing/fcode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ def _print_FunctionCallArgument(self, expr):
963963
return '{}'.format(self._print(expr.value))
964964

965965
def _print_Constant(self, expr):
966+
if expr == math_constants['nan']:
967+
errors.report("Can't print nan in Fortran",
968+
severity='error', symbol=expr)
966969
val = LiteralFloat(expr.value)
967970
return self._print(val)
968971

tests/pyccel/scripts/print_nan.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pylint: disable=missing-function-docstring, missing-module-docstring
2+
from math import nan
3+
4+
if __name__ == '__main__':
5+
print(nan)
6+

tests/pyccel/test_pyccel.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,19 @@ def test_print_strings(language):
733733
types = str
734734
pyccel_test("scripts/print_strings.py", language=language, output_dtype=types)
735735

736+
#------------------------------------------------------------------------------
737+
@pytest.mark.parametrize( 'language', (
738+
pytest.param("python", marks = pytest.mark.python),
739+
pytest.param("c", marks = pytest.mark.c),
740+
pytest.param("fortran", marks = [
741+
pytest.mark.skip(reason="Can't print NaN in Fortran"),
742+
pytest.mark.fortran])
743+
)
744+
)
745+
def test_print_nan(language):
746+
types = str
747+
pyccel_test("scripts/print_nan.py", language=language, output_dtype=types)
748+
736749
#------------------------------------------------------------------------------
737750
def test_print_integers(language):
738751
types = str

0 commit comments

Comments
 (0)