-
Notifications
You must be signed in to change notification settings - Fork 157
Do not convert floats to integers #2324
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
Open
ThrudPrimrose
wants to merge
3
commits into
main
Choose a base branch
from
float_print_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−4
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Copyright 2019-2026 ETH Zurich and the DaCe authors. All rights reserved. | ||
| import sympy | ||
| from dace.symbolic import sympy_numeric_fix, pystr_to_symbolic, symstr | ||
|
|
||
|
|
||
| def test_float_zero_stays_float(): | ||
| """sympy.Float(0.0) must not be demoted to int(0).""" | ||
| result = sympy_numeric_fix(sympy.Float(0.0)) | ||
| assert isinstance(result, sympy.Float), \ | ||
| f"Float(0.0) demoted to {type(result).__name__}" | ||
| assert float(result) == 0.0 | ||
|
|
||
|
|
||
| def test_float_one_stays_float(): | ||
| """sympy.Float(1.0) must not be demoted to int(1).""" | ||
| result = sympy_numeric_fix(sympy.Float(1.0)) | ||
| assert isinstance(result, sympy.Float) | ||
| assert float(result) == 1.0 | ||
|
|
||
|
|
||
| def test_float_five_stays_float(): | ||
| """sympy.Float(5.0) must not be demoted to int(5).""" | ||
| result = sympy_numeric_fix(sympy.Float(5.0)) | ||
| assert isinstance(result, sympy.Float) | ||
| assert float(result) == 5.0 | ||
|
|
||
|
|
||
| def test_fractional_float_preserved(): | ||
| """sympy.Float(0.7) must stay Float(0.7).""" | ||
| result = sympy_numeric_fix(sympy.Float(0.7)) | ||
| assert isinstance(result, sympy.Float) | ||
|
ThrudPrimrose marked this conversation as resolved.
|
||
| assert abs(float(result) - 0.7) < 1e-15 | ||
|
|
||
|
|
||
| def test_float_prints_clean(): | ||
| """5.0 should print as '5.0', not '5.00000000000000'.""" | ||
| result = sympy_numeric_fix(sympy.Float(5.0)) | ||
|
ThrudPrimrose marked this conversation as resolved.
|
||
| s = symstr(result) | ||
| assert s == '5.0', f"Expected '5.0', got '{s}'" | ||
|
|
||
|
|
||
| def test_huge_python_int_becomes_oo(): | ||
| """Python int beyond float64 range must map to sympy.oo. | ||
| Original comment: int(1.8e308) == expr is True because Python | ||
| has variable-bit integers, but numpy.float64() overflows.""" | ||
| result = sympy_numeric_fix(10 ** 309) | ||
| assert result == sympy.oo | ||
|
|
||
|
|
||
| def test_huge_negative_python_int_becomes_neg_oo(): | ||
| """Negative Python int beyond float64 range must map to -sympy.oo.""" | ||
| result = sympy_numeric_fix(-(10 ** 309)) | ||
| assert result == -sympy.oo | ||
|
|
||
|
|
||
| def test_max_float_literal_roundtrip(): | ||
| """Parsing 'max(a, 0.0)' and printing via symstr must preserve '0.0', | ||
| not demote to '0'. Demotion causes Max<int>(a, 0) in C++ codegen.""" | ||
| expr = pystr_to_symbolic("max(a, 0.0)") | ||
| result = symstr(expr, cpp_mode=True) | ||
|
|
||
| # Must contain 0.0 (the float literal), not bare 0 (int literal) | ||
| assert "0.0" in result, \ | ||
| f"Float literal 0.0 was demoted to int: '{result}'. " \ | ||
| f"This causes Max<int>(a, 0) in C++ template instantiation." | ||
|
|
||
|
|
||
| def test_max_float_literal_not_int(): | ||
| """Complement: ensure the printed string does NOT match 'Max(a, 0)' | ||
| where 0 is an integer literal (no decimal point).""" | ||
| expr = pystr_to_symbolic("max(a, 0.0)") | ||
| result = symstr(expr, cpp_mode=True) | ||
|
|
||
| # Strip spaces for robust matching | ||
| clean = result.replace(" ", "") | ||
| # Should not end with ,0) — that's the int literal | ||
| assert not clean.endswith(",0)"), \ | ||
| f"Got integer literal in Max call: '{result}'" | ||
|
|
||
|
|
||
| def test_max_int_literal_stays_int(): | ||
| """Parsing 'max(a, 0)' with an explicit int should keep it as int. | ||
| This is the correct behavior when the user wrote 0, not 0.0.""" | ||
| expr = pystr_to_symbolic("max(a, 0)") | ||
| result = symstr(expr, cpp_mode=True) | ||
|
|
||
| # This one SHOULD have bare 0 — user explicitly wrote int | ||
| clean = result.replace(" ", "") | ||
| assert "0.0" not in clean, \ | ||
| f"Integer literal 0 was promoted to float: '{result}'" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.