HP Forums
Understanding How a Program Is Treating Parentheses - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Understanding How a Program Is Treating Parentheses (/thread-9909.html)



Understanding How a Program Is Treating Parentheses - kvloxx - 01-11-2018 09:13 PM

Hi there,

I'm trying to make a simple program to return an expression for the curvature of a single variable function in X. My intention would be to execute this in the home view and plot the result.

Code:
EXPORT CurvX(EX)
BEGIN
LOCAL DEX=CAS.diff(EX,'X'), DDEX=CAS.diff(EX,'X',2);
  RETURN (ABS(DDEX))/((1+(DEX)^(2))^(3/2));
END;

For a lot of functions this works just fine, but if I try it on the exponential function, I get some unexpected behavior.

[Image: GkQIyUD.png]

When textbook display is enabled, the result seems exactly as expected, but once I copy it back to input, the (e^X)^2 term becomes e^(X)^2.

[Image: lPkcpSD.png]

If I disable textbook display, we see that the function is actually returning e^(X)^2, which, strictly speaking, represents e^(X^2) instead of the desired e^(2X). If I evaluate the copied expression, you can even see that the extra parentheses are inserted afterwards to reflect this parsing:

[Image: H3VzxVA.png]

I'm pretty sure the problem lies in the way my program is evaluating the return expression, but I'm unaware of how it should be written to ensure parentheses are always included before squaring the derivative.

Any insight would be much appreciated!

Thanks