expr icon indicating copy to clipboard operation
expr copied to clipboard

Possible issue with calculation

Open seekerkoruth opened this issue 3 years ago • 9 comments

code:= 1+1/(4+5)*(1*5)+3.3+3.3+3.3+3.3/3.3*3.3

When I run this code in excel, I get 14.7555. When I run it in expr, I get 14.2.

What accounts for the difference?

however this works: 1.0+1/(4.0+5)*(1*5.0)+3.3+3.3+3.3+3.3/3.3*3.3 yields 14.7555

It looks like you have to marshal the integers into floats by adding .0

Thank you.

seekerkoruth avatar Jun 20 '21 11:06 seekerkoruth

I think this can be an issue: 1/(4+5)*(1*5)

antonmedv avatar Jun 20 '21 21:06 antonmedv

How do you propose this can be solved? At the code level or at the end consumer level by adding the .0 hint to a literal numerator or denominator when there is division? However, if both the numerator and denominator are both integer variables then we may have less control.

seekerkoruth avatar Jun 21 '21 12:06 seekerkoruth

What about float as default for every constants in 1/2 expression?

antonmedv avatar Jun 21 '21 13:06 antonmedv

What about float as default for every constants in 1/2 expression?

I think this will work. Float as a default for all division operations is a excellent rational solution to this issue as this will cover integer variables as numerators or denominators also and not just constants/literals.

seekerkoruth avatar Jun 23 '21 02:06 seekerkoruth

@davidklassen what do you think?

antonmedv avatar Jun 23 '21 18:06 antonmedv

There is another issue related to precedence, I don't know if it is the same root cause. The expression "-(1*2)^2" it is expected "-4" but it was returned "4".

mrxrsd avatar Aug 16 '21 20:08 mrxrsd

The expression "-(1*2)^2" it is expected "-4" but it was returned "4".

The correct answer is 4.

antonmedv avatar Aug 16 '21 22:08 antonmedv

Not according to google and wolfram alpha =p

https://www.wolframalpha.com/input/?i=-%2812%29%5E2 https://letmegooglethat.com/?q=-(12)%5E2

mrxrsd avatar Aug 16 '21 22:08 mrxrsd

I guess unary op is higher priority than exponential in expr.

antonmedv avatar Aug 16 '21 22:08 antonmedv

Fixed in https://github.com/antonmedv/expr/commit/2aec311b2bcdb9ab5a8efee88c08138dde84a65c

antonmedv avatar Nov 05 '22 19:11 antonmedv