From 09c6f788222f940d9b40dc3194d53c0c62eac9ea Mon Sep 17 00:00:00 2001
From: Markus Holzer <markus.holzer@fau.de>
Date: Mon, 20 Dec 2021 22:04:02 +0100
Subject: [PATCH] Fix RoundOff problems

---
 pystencils/backends/cbackend.py    | 4 ++--
 pystencils/simp/simplifications.py | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py
index a17ee7269..8437bdb68 100644
--- a/pystencils/backends/cbackend.py
+++ b/pystencils/backends/cbackend.py
@@ -441,7 +441,7 @@ class CustomSympyPrinter(CCodePrinter):
     def _print_Pow(self, expr):
         """Don't use std::pow function, for small integer exponents, write as multiplication"""
         if not expr.free_symbols:
-            return self._typed_number(expr.evalf(), get_type_of_expression(expr.base))
+            return self._typed_number(expr.evalf(17), get_type_of_expression(expr.base))
 
         if expr.exp.is_integer and expr.exp.is_number and 0 < expr.exp < 8:
             return f"({self._print(sp.Mul(*[expr.base] * expr.exp, evaluate=False))})"
@@ -452,7 +452,7 @@ class CustomSympyPrinter(CCodePrinter):
 
     def _print_Rational(self, expr):
         """Evaluate all rationals i.e. print 0.25 instead of 1.0/4.0"""
-        res = str(expr.evalf().num)
+        res = str(expr.evalf(17))
         return res
 
     def _print_Equality(self, expr):
diff --git a/pystencils/simp/simplifications.py b/pystencils/simp/simplifications.py
index 114b86a40..720abb52a 100644
--- a/pystencils/simp/simplifications.py
+++ b/pystencils/simp/simplifications.py
@@ -234,7 +234,7 @@ def apply_sympy_optimisations(assignments):
 
     # Evaluates all constant terms
     evaluate_constant_terms = ReplaceOptim(lambda e: hasattr(e, 'is_constant') and e.is_constant and not e.is_integer,
-                                           lambda p: p.evalf())
+                                           lambda p: p.evalf(17))
 
     sympy_optimisations = [evaluate_constant_terms] + list(optims_c99)
 
-- 
GitLab