Skip to content

Commit cb5d1b9

Browse files
Large int bug (#238)
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).
1 parent 5fe5344 commit cb5d1b9

24 files changed

+844
-841
lines changed

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release 0.9.6
2+
- 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).
3+
4+
# Release 0.9.6 (yanked)
25
- 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
6+
- This release was yanked due to a bug printing large integers
37

48
# Release 0.9.5
59
- Corrected a type in the generated output for `--rush-larsen-c`

chaste_codegen/_chaste_printer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from sympy.printing.precedence import precedence
1313

1414

15+
C_MAX_INT = 2147483647
16+
C_MIN_INT = -2147483647
17+
18+
1519
class ChastePrinter(Printer):
1620
"""
1721
Converts Sympy expressions to strings for use in Chaste code generation.
@@ -141,18 +145,19 @@ def _print_ternary(self, cond, expr):
141145
return parts
142146

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

146150
def _print_float(self, expr):
147151
""" Handles ``float``s. """
148-
if expr.is_integer():
152+
# print integers as int if they are between min & max int in c++
153+
if expr.is_integer() and C_MIN_INT < expr < C_MAX_INT:
149154
return cxxcode(int(expr), standard='C++11')
150155
else:
151-
return cxxcode(expr, standard='C++11')
156+
return cxxcode(float(expr), standard='C++11')
152157

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

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

chaste_codegen/data/tests/chaste_reference_models/BE/dynamic_courtemanche_ramirez_nattel_1998.cpp

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chaste_codegen/data/tests/chaste_reference_models/BE/dynamic_courtemanche_ramirez_nattel_1998.cpp_alt

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chaste_codegen/data/tests/chaste_reference_models/BE/li_mouse_2010.cpp

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chaste_codegen/data/tests/chaste_reference_models/BE/li_mouse_2010.cpp_alt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)