Skip to content

FIX: Handle zero-valued expression variables properly.#6376

Merged
Samuelopez-ansys merged 2 commits into
mainfrom
fix/zero_expression
Jul 10, 2025
Merged

FIX: Handle zero-valued expression variables properly.#6376
Samuelopez-ansys merged 2 commits into
mainfrom
fix/zero_expression

Conversation

@Samuelopez-ansys

Copy link
Copy Markdown
Member

I noticed the Hfss.evaluate_expression() method was incorrectly returning the symbolic value of an expression, instead of the float value, when the expression evaluated to zero.

For example, this code would print the string 'test - test' instead of 0.0 as expected:

app = Hfss("ZeroVariableTest")
app['x'] = '10 mm'
app["test"] = 'x / 1mm'

print(f'{app.evaluate_expression('test - test')=}') # should be 0.0

I tracked it down to the Variable class constructor, where `si_value' was being directly used as a truth value:

if si_value:
    self._value = si_value
else:
    self._value = self._calculated_value

Unfortunately, float(0.0) is a Falsy value in Python (bool(0.0) == False), so the above incorrectly treats a 0.0 float the same as None. The correct condition for None-ness is if si_value is not None instead of if si_value.

(cherry picked from commit 2e0c753)

Description

Please provide a brief description of the changes made in this pull request.

Issue linked

Please mention the issue number or describe the problem this pull request addresses.

Checklist

  • I have tested my changes locally.
  • I have added necessary documentation or updated existing documentation.
  • I have followed the coding style guidelines of this project.
  • I have added appropriate tests (unit, integration, system).
  • I have reviewed my changes before submitting this pull request.
  • I have linked the issue or issues that are solved by the PR if any.
  • I have agreed with the Contributor License Agreement (CLA).

I noticed the Hfss.evaluate_expression() method was incorrectly
returning the symbolic value of an expression, instead of the float
value, when the expression evaluated to zero.

For example, this code would print the string 'test - test' instead of
0.0 as expected:

    app = Hfss("ZeroVariableTest")
    app['x'] = '10 mm'
    app["test"] = 'x / 1mm'

    print(f'{app.evaluate_expression('test - test')=}') # should be 0.0

I tracked it down to the Variable class constructor, where `si_value'
was being directly used as a truth value:

    if si_value:
        self._value = si_value
    else:
        self._value = self._calculated_value

Unfortunately, float(0.0) is a Falsy value in Python (`bool(0.0) ==
False`), so the above incorrectly treats a 0.0 float the same as
None. The correct condition for None-ness is `if si_value is not None`
instead of `if si_value`.

(cherry picked from commit 2e0c753)
@Samuelopez-ansys Samuelopez-ansys requested a review from a team as a code owner July 10, 2025 06:45
@ansys-reviewer-bot

Copy link
Copy Markdown
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

@github-actions github-actions Bot added the bug Something isn't working label Jul 10, 2025
@Samuelopez-ansys Samuelopez-ansys enabled auto-merge (squash) July 10, 2025 06:47
@codecov

codecov Bot commented Jul 10, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.35%. Comparing base (710ce3a) to head (c1d7681).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6376      +/-   ##
==========================================
- Coverage   85.36%   85.35%   -0.02%     
==========================================
  Files         175      175              
  Lines       65989    65989              
==========================================
- Hits        56333    56326       -7     
- Misses       9656     9663       +7     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Samuelopez-ansys Samuelopez-ansys merged commit 3b06299 into main Jul 10, 2025
37 checks passed
@Samuelopez-ansys Samuelopez-ansys deleted the fix/zero_expression branch July 10, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants