Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jean-Noël Grad
pystencils
Commits
959241b0
Commit
959241b0
authored
Dec 21, 2021
by
Jan Hönig
Browse files
Merge branch 'RoundOffError' into 'master'
Fix RoundOff problems See merge request
pycodegen/pystencils!282
parents
29e0e84e
19852424
Changes
3
Hide whitespace changes
Inline
Side-by-side
pystencils/backends/cbackend.py
View file @
959241b0
...
...
@@ -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
):
...
...
pystencils/simp/simplifications.py
View file @
959241b0
...
...
@@ -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
)
...
...
pystencils_tests/test_types.py
View file @
959241b0
...
...
@@ -101,8 +101,9 @@ def test_sqrt_of_integer():
kernel
(
f
=
arr_single
)
code
=
ps
.
get_code_str
(
kernel
.
ast
)
assert
"1.7320508075688772f"
in
code
# ps.show_code(kernel.ast)
# 1.7320508075688772935 --> it is actually correct to round to ...773. This was wrong before !282
assert
"1.7320508075688773f"
in
code
assert
1.7
<
arr_single
[
0
]
<
1.8
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment