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
base: devel
Are you sure you want to change the base?
Conversation
Hi, how are you doing? Are you still working on this PR? Do you need a hand? I just wanted to remind you to keep your branch up to date with the master branch. There have been a few aesthetic changes recently (mainly PR #1012) which should be merged sooner rather than later to avoid accumulating conflicts |
This needs to be handled, maybe we need to implement our own |
An error is raised when I compile the following code for Fortran: if __name__ == "__main__":
print(round(42, ndigits=5)) The error:
|
@aihya Good catch! This is because the two functions implemented in Fortran require the first argument to be a float (single or double precision), while here you are passing an integer. @nandiniraja348 Unit tests should be added to cover the case of integer input. cc: @EmilyBourne |
Following code is compiled for C if __name__ == "__main__":
print(round(42, ndigits=4.2)) Output: $> pyccel test_round.py --language c
/home/glitch/Documents/nandiniraja348/pyccel/codegen/compiling/compilers.py:431: UserWarning: /home/glitch/Documents/nandiniraja348/__pyccel__/prog_test_round.c: In function ‘main’:
/home/glitch/Documents/nandiniraja348/__pyccel__/prog_test_round.c:7:15: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘double’ [-Wformat=]
7 | printf("%ld\n", lrint(42 * pow(10.0, 4.2)) / pow(10.0, 4.2));
| ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| long int double
| %f
warnings.warn(UserWarning(err))
$> ./test_round
140733447113224 Incorrect rounded value when passing non-integer to if __name__ == "__main__":
print(round(4.2, ndigits=4.2)) Output: |
@aihya
gives: TypeError Traceback (most recent call last)
<ipython-input-1-dd8329dcb3e7> in <module>
----> 1 print(round(4.2, ndigits=4.2))
TypeError: 'float' object cannot be interpreted as an integer |
The last failing test is due to an integer not being stored accurately in a floating point. I'm not sure how to fix this without using python's integer round method (operating on strings). Does anyone have any ideas? |
I have added the Round Python function.
Added tests.