Question about how the sgn function works in vexcode

I was reading through a purdue robotics article on pure pursuit and a segment of their pseudocode uses sign and I don’t understand how it’s being used in the psuedocode.
Article link : LINK
Psuedocode:

        sol_x1 = (D * dy + sgn(dy) * dx * np.sqrt(discriminant)) / dr**2
        sol_x2 = (D * dy - sgn(dy) * dx * np.sqrt(discriminant)) / dr**2

I don’t understand how the sgn(dy) is being used here, and how order of operations would read this. Is it multiplying all the numbers and subtracting/adding sgn(dy), or is the code multiplying the numbers times (dy + sgn(dy)) and (dy - sgn(dy).
Please let me know if you have any insight into how this equation is supposed to be read.

Well, it’s Python not pseudocode.

the sgn function was defined as

# helper function: sgn(num)
# returns -1 if num is negative, 1 otherwise
def sgn (num):  
    if num >= 0:
        return 1
    else:
        return -1

so it’s either 1 or -1. multiplication happens before addition/subtraction in the equation.

2 Likes

So could the equation be re written as this?

sol_x1 = ((D * dy * dx * np.sqrt(discriminant)) / dr**2) + sgn(dy) 
sol_x2 = ((D * dy * dx * np.sqrt(discriminant)) / dr**2) - sgn(dy) 

Or would it be re written in a different way?

No. parenthesis are evaluated before addition (which is what you have).

I’m not sure why you want to re-write it… you can flip around the order of variables being multiplied, but other than that you shouldn’t move things. If it makes you feel better, you can add another set of parenthesis to make it clear that the multiplication happens first:

sol_x1 = ((D * dy) + (sgn(dy) * dx * np.sqrt(discriminant))) / dr**2

This is what the expression would be written out as:

image

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.