-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Negative number raised to even power gives negative result if exponent is a variable #1720
Comments
Happy to review a PR |
Would you mind if I'll take a look onto this during this weekend? |
Anyone is welcome to work on any issue at any time, no need to ask. |
Some more details about this issue. There are two problems: differences with respect to the results obtained with python and discrepancies between different inputs in jinja. source codeIn the following I will list some examples for different templates obtained with the following code: env = jinja2.Environment()
env.globals = {"x": 2}
jinja_output = env.from_string(f"{{{{ {expression} }}}}").render()
python_output = eval(f"{expression}",env.globals) examples for different input expressionsexpression: expression: expression: expression: conclusionThe cause of the differences with respect to python could be in the parsing stage since, independently from the use of parenthesis, the result of the parser for Discrepancies between jinja outputs with and without variable exponent could be found in the code generation step, where an optimization step is applied when the exponent is a |
I've raised this as an issue against Home Assistant, and have now seen this related issue. |
Unexpected behavior of the power operator with a variable of even value as exponent. It returns the negative value of the expected result.
mcve
From which I get
'-1'
Expected behavior
I expected the positive value
'1'
as a resultIt instead works as expected in a Sandboxed environment when intercepting the
**
operator:From which I get
'1'
Environment:
The text was updated successfully, but these errors were encountered: