Evaluate Component Inconsistency in Rhino Grasshopper

Dear McNeel Forum Members,

I hope this message finds you well. This is my first appearance on the forum, and I extend my greetings to all fellow members.

I am reaching out to share an observation regarding the Evaluate Component in Rhino Grasshopper, which has posed quite a challenge for me. I’ve encountered what appears to be an inconsistency in the output of expressions, particularly when involving negative signs and exponents.

In a series of evaluations using the variables x and y, with x=3 and y=5, I entered the following expressions:

  1. -x^2*y - x^2*y with an outcome of 0
  2. -(x^2)*y - x^2*y with an outcome of -90
  3. -(x^2)*y - (x^2)*y with an outcome of -90
  4. -x^2*y - (x^2)*y with an outcome of 0

According to the PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) order of operations, the outcome should consistently be -90 in all cases. However, it appears that only when the first term begins with a “-” sign and without parentheses, the evaluation yields the unexpected result.

I have attached a screenshot of the components in Grasshopper along with the Grasshopper file itself for reference.

My inquiry to the forum and to McNeel is whether this behavior is a known issue or if there might be an explanation for it within the logic of the Evaluate Component in Grasshopper.

Your insights and feedback on this matter would be greatly appreciated.

Best regards,

Ed_2001

Attached: Screenshot of Grasshopper Components, Grasshopper File:


Evaluate Component Inconsistency in Rhino Grasshopper.gh (12.1 KB)

Hello
you are right, I have done also an equation component (faster for large number of points than GH one but it lacks the minus sign at the moment so I added -1). It is consistent with the rules of priority

What is strange is results from GH2, far worse ! @DavidRutten
equation_gh2.ghz (105.8 KB)

1 Like

right there with math is racist and 1+1=3 :rofl:

@laurent_delrieu , thank you for conducting further research in Gh1 and for addressing this issue in Grasshopper 2. While I have a grasp of what is going wrong in Grasshopper 1, I am also keen on understanding the underlying explanation for the behavior of the Evaluate component in Gh2.

As David did surely the 2 components, he’ll surely have an explanation.

1 Like

I did write both and they use wildly different approaches. Expressions in gh1 are interpreted using a fairly simple state machine based on VB syntax. In gh2 expressions are translated into C# and then compiled.

I’ll dig into why gh2 gets such weird answers, but it’ll have to wait until after I get back from holiday.

4 Likes

Of course 5 minutes after I got into the car I realised what was up. In C#, the ^ operator is not meant for exponentiation. There is no exponentiation in C#. VB uses ^, Python uses **, but in C# you must use Pow(b, e). This is something I’ll have to look into whether and how to fix.

2 Likes

Hehe … :wink:
That belongs to the “C” in C#.