Skip to content

Large int bug #238

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

Merged
merged 8 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release 0.9.6
- This release fixes a bug printing large integers, in some environments: Large numbers such as 8.034023767017109e+27 whch were actually ints would be printed as an the int 8034023767017108950029959168 and then the C++ compiler would complain as that's more than the maximum int value. This has been fixed by only printing ints as ints if they are `MIN_INT < number < MAX_INT` else we print them as a float (and we get the sceintific notation).

# Release 0.9.6 (yanked)
- Fixed tests to pass with sympy 1.10 and required latest cellmlmanip, which also workes with sympy1.10. Updated sympy requirement to be >=1.9, < 1.11
- This release was yanked due to a bug printing large integers

# Release 0.9.5
- Corrected a type in the generated output for `--rush-larsen-c`
Expand Down
13 changes: 9 additions & 4 deletions chaste_codegen/_chaste_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from sympy.printing.precedence import precedence


C_MAX_INT = 2147483647
C_MIN_INT = -2147483647


class ChastePrinter(Printer):
"""
Converts Sympy expressions to strings for use in Chaste code generation.
Expand Down Expand Up @@ -141,18 +145,19 @@ def _print_ternary(self, cond, expr):
return parts

def _print_IntegerConstant(self, expr):
return cxxcode(int(expr), standard='C++11')
return self._print_int(float(expr))

def _print_float(self, expr):
""" Handles ``float``s. """
if expr.is_integer():
# print integers as int if they are between min & max int in c++
if expr.is_integer() and C_MIN_INT < expr < C_MAX_INT:
return cxxcode(int(expr), standard='C++11')
else:
return cxxcode(expr, standard='C++11')
return cxxcode(float(expr), standard='C++11')

def _print_int(self, expr):
""" Handles ``ints``s. """
return cxxcode(int(expr), standard='C++11')
return self._print_float(float(expr))

def _print_ITE(self, expr):
""" Handles ITE (if then else) objects by rewriting them as Piecewise """
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading