Skip to content

Commit

Permalink
Large int bug (#238)
Browse files Browse the repository at this point in the history
There was an issue where in some environments (but not all) large numbers such as 8.034023767017109e+27 whch were 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).
  • Loading branch information
MauriceHendrix authored Apr 12, 2022
1 parent 5fe5344 commit cb5d1b9
Show file tree
Hide file tree
Showing 24 changed files with 844 additions and 841 deletions.
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

0 comments on commit cb5d1b9

Please sign in to comment.