I'm programming a math expression parser for a class and I have a question, I'm getting conflicting answers

I'm programming a math expression parser for a class and I have a question, I'm getting conflicting answers.

How should -3^2 be parsed?

As (-3)^2 or -(3^2)?

PEMDAS does not apply here, the '-' is not subtraction, it's the unary negative operator.

Other urls found in this thread:

duckduckgo.com/?q=-3^2&t=hb&ia=calculator
symbolab.com/solver/step-by-step/-3^{2}
docs.python.org/2/reference/expressions.html#the-power-operator
en.cppreference.com/w/cpp/language/operator_precedence
twitter.com/NSFWRedditGif

Do you want the answer to be 9 or -9?

-(3^2)

It would be silly to do it the other way.

I don't want it either way, I'm asking which way is the correct way

Why would it be silly?

duckduckgo.com/?q=-3^2&t=hb&ia=calculator

Well pal, do you want to multiply the number that is 3 less than 0 times itself, or do you want to multiply the number that is 3 more than 0 times itself and multiply it by -1?
The common simplification for -x is (-1)(x) (in which case PEMDAS does apply because exponents are before exponents), so [(-1)(x)]^2=x^2 while (-1)(x^2)=-(x^2)

>exponents are before exponents
Before multiplication, I mean.

Look up how they do it in PHP.

Then do the opposite of that. I will be guaranteed to be correct.

i would assume -3^2 = (-3)^2 = 9

guess im wrong according to symbolab.com/solver/step-by-step/-3^{2} and

a computer (python, c++) would interpret that as -(3^2)

-3 to the power of 2 ; (-3)^2.

What do you mean? I am writing the parser in python. A program will interpret it as whatever it is programmed to interpret it as. ^ isn't even used as power operator in programming as it's a bit operator.

>Why would it be silly?

Because you would never want (-2)^10 in expression like -2^10+3^10 but rather -(2^10)

docs.python.org/2/reference/expressions.html#the-power-operator

Fuck PEMDAS.
You make the rules to fit what you want.
Just make sure both outcomes are possible.
Also look up Polish notation and Reverse-Polish notation.

>Polish notation
Why would anybody follow Poland's example for anything

My parser converts it to postfix(rpn) so it can be easily solved with a stack. Of course I follow PEMDAS because nobody realistically writes their expressions directly in postfix.

-1*3^2 = -1*9 = -9
PEMDAS

The standard is -(3^2)

You have to include parenthesis if you want (-3)^2 on all other calculators

Therefore if it doesn't have parenthesis just evaluate -3^2 as the the standard -(3^2)

Well, if you want it to be exact and give you the right answer, something like

-3^2 = (-(3^2))

its actually quite useful. This case would be:

-3^2 = (* -1 (^ 3 2))

-3^2 =
-( ^(3,2) ) =
-( 9 ) =
-9

(-3)^2 =
^( (-(3)),2 ) =
^( -3,2 ) =
9

That's the standard for calculators and most programs. But I've been writing -3^2 =9 by hand all my life and teachers, professors, and other people never have issues with it because it's clear from the context what is meant.

>en.cppreference.com/w/cpp/language/operator_precedence

Since you're programming, you're probably actually asking about operator precedence. It's up to you what that precedence will be, but I think you should conform to long standing convention. Unary evaluates before other arithmetic operators.

Why make a separate rule for unary when you can just treat it like subtraction?

-3 is -1*3 so exponent first, -9

>^ isn't even used as power operator in programming as it's a bit operator.
no, it's commonly used as a power operator in high level languages

>because it's clear from the context what is meant.
it's actually not, which is the whole point of this thread

(-3)^2

>writing a parser
>in python
1. Never write anything in python.
2. Never write a parser for things that the language already handles just fine.

In prefix or postfix notation, those parentheses are superfluous. In prefix notation, and assuming a unary `-` the two possible interpretations of -3^2 are:

- ^ 3 2

^ - 3 2

Look at old HP calculators...
It's a lost art, I'm afraid.
I think the only advantage of infix is that the operator is close to the operands.
But it only works for binary (2 args, not 1,0) operators.

"I'm not a robot" lol

You must not have read my post. When I write, I make it clear that it's 9, not -9. I didn't say anything about the post.

If you wanted to write 2^3-1^2, you really wouldn't want to be doing 2^3-(3^2) all the time. To simplify equations, append "0+" to the beginning of every expression to differentiate.

1. It's probably a HW assignment.
2. You're retarded

Are you fucking retarded?

It's (-3) ^ 2

What if it was (-3)^3, your second answer wouldn't make any sense

when I made my compiler, unary minus was given precedence over all other operations.

-(3^3) is equal to (-3)^3, though.

Every instance of binary - can be replaced by binary + with unary -
GJ

If it's a homework assignment, he needs to go to a real school.