Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #983 | Add Round function and implement the conversion to C, Fortran, and Python #996

Open
wants to merge 39 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8260ef1
Issue #983 | Add Round function and implement the conversion to C and…
Oct 15, 2021
c617738
Merge branch 'master' into master
nandiniraja348 Oct 15, 2021
d02c02a
Issue #983 | Add an import for randint in tests
Oct 16, 2021
c65cf40
Remove slots
Oct 16, 2021
cc8e2c4
Merge branch 'master' into master
EmilyBourne Dec 15, 2021
f8a9898
Merge branch 'master' into master
EmilyBourne May 2, 2022
85085bd
Put back slots. Fix merge
EmilyBourne May 2, 2022
bf79ca9
Add type check
EmilyBourne May 2, 2022
1c9640d
Correct merge
EmilyBourne May 2, 2022
25a7c95
Add ndigits argument to class
EmilyBourne May 2, 2022
ddef46d
Add missing slots
EmilyBourne May 2, 2022
3700029
Save ndigits. Use default python precision (instead of numpy precision)
EmilyBourne May 2, 2022
0d9e30d
Add more tests
EmilyBourne May 2, 2022
86a4ed8
Test middle case
EmilyBourne May 2, 2022
d857efc
Merge branch 'master' into master
EmilyBourne May 30, 2022
5526c55
Merge branch 'master' into master
EmilyBourne Jul 16, 2022
99e73ee
Update performance comparison
github-actions[bot] Jul 16, 2022
9944bd0
Try and prevent trigger
EmilyBourne Jul 16, 2022
5e2da50
Add printing functions
EmilyBourne Jul 16, 2022
e6524df
Prevent bench trigger
EmilyBourne Jul 16, 2022
0e5a57f
Correct typo
EmilyBourne Jul 16, 2022
a581983
Use banker's round
EmilyBourne Jul 16, 2022
5ac8200
Match python behaviour
EmilyBourne Jul 16, 2022
d24bb62
Typo. Missing import
EmilyBourne Jul 16, 2022
cadd954
Use pyc_bankers_round with ndigits argument
EmilyBourne Jul 16, 2022
0a3f405
Add tests. Add failing test in C
EmilyBourne Jul 16, 2022
8993b89
Remove debugging info
EmilyBourne Jul 16, 2022
1870ec2
Ensure developer mode is temporary
EmilyBourne Jul 16, 2022
f8ce870
Make into interface
EmilyBourne Jul 16, 2022
af5f147
Simplify statement
EmilyBourne Jul 16, 2022
f1b02cd
Merge branch 'master' into master
EmilyBourne Jul 19, 2022
8bbc357
Merge branch 'master' into master
yguclu Jul 19, 2022
a087bac
Correct expected dtype
EmilyBourne Jul 20, 2022
95017a8
Add missing functions
EmilyBourne Jul 20, 2022
09ac6cc
Add int tests
EmilyBourne Jul 20, 2022
55d95a2
Handle unexpected precision
EmilyBourne Jul 20, 2022
a4a947f
Correct cast
EmilyBourne Jul 20, 2022
c279c52
Correct stupid typo
EmilyBourne Jul 20, 2022
a786f87
Merge branch 'master' into master
yguclu Mar 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pyccel/ast/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'PythonZip',
'PythonMax',
'PythonMin',
'PythonRound',
'python_builtin_datatype'
)

Expand Down Expand Up @@ -319,6 +320,26 @@ def arg(self):
def __str__(self):
return 'float({0})'.format(str(self.arg))

# ===========================================================================
class PythonRound(PyccelAstNode):
EmilyBourne marked this conversation as resolved.
Show resolved Hide resolved
""" Represents a call to Python's native round() function.
"""
name = 'round'
_dtype = NativeReal()
_precision = default_precision['real']
_rank = 0
_shape = ()
_order = None
_attribute_nodes = ('_arg',)

def __init__(self, arg):
EmilyBourne marked this conversation as resolved.
Show resolved Hide resolved
self._arg = arg
super().__init__()

@property
def arg(self):
return self._arg

#==============================================================================
class PythonInt(PyccelAstNode):
""" Represents a call to Python's native int() function.
Expand Down Expand Up @@ -861,4 +882,5 @@ def python_builtin_datatype(name):
'not' : PyccelNot,
'map' : PythonMap,
'type' : PythonType,
'round' : PythonRound,
}
5 changes: 5 additions & 0 deletions pyccel/codegen/printing/ccode.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def _print_PythonAbs(self, expr):
func = "labs"
return "{}({})".format(func, self._print(expr.arg))

def _print_PythonRound(self, expr):
self._additional_imports.add("math")
func = "round"
return "{}({})".format(func, self._print(expr.arg))

def _print_PythonMin(self, expr):
arg = expr.args[0]
if arg.dtype is NativeReal() and len(arg) == 2:
Expand Down
6 changes: 6 additions & 0 deletions pyccel/codegen/printing/fcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ def _print_PythonAbs(self, expr):
"""
return "abs({})".format(self._print(expr.arg))

def _print_PythonRound(self, expr):
""" print the python builtin function round
args : variable
"""
return "nint({})".format(self._print(expr.arg))

def _print_PythonTuple(self, expr):
shape = tuple(reversed(expr.shape))
if len(shape)>1:
Expand Down
3 changes: 3 additions & 0 deletions pyccel/codegen/printing/pycode.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ def _print_PythonImag(self, expr):
def _print_PythonPrint(self, expr):
return 'print({})\n'.format(', '.join(self._print(a) for a in expr.expr))

def _print_PythonRound(self, expr):
return 'round({})'.format(self._print(expr.arg))

def _print_PyccelArraySize(self, expr):
arg = self._print(expr.arg)
index = self._print(expr.index)
Expand Down
17 changes: 17 additions & 0 deletions tests/epyccel/test_epyccel_round.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# pylint: disable=missing-function-docstring, missing-module-docstring/
from numpy.random import randint

from pyccel.decorators import types
from pyccel.epyccel import epyccel

def test_round_int(language):
@types('real')
def round_int(x):
return round(x)

f = epyccel(round_int, language=language)
x = randint(100) / 10

f_output = f(x)
round_int_output = round_int(x)
assert round_int_output == f_output
EmilyBourne marked this conversation as resolved.
Show resolved Hide resolved