[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I'm programming a math expression parser for a class and

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 41
Thread images: 3

File: Untitled.png (8KB, 400x400px) Image search: [Google]
Untitled.png
8KB, 400x400px
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.
>>
>>9075153
Do you want the answer to be 9 or -9?
>>
>>9075153
-(3^2)

It would be silly to do it the other way.
>>
>>9075156
I don't want it either way, I'm asking which way is the correct way

>>9075159
Why would it be silly?
>>
>>9075160
https://duckduckgo.com/?q=-3%5E2&t=hb&ia=calculator
>>
>>9075160
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)
>>
>>9075170
>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.
>>
>>9075153
i would assume -3^2 = (-3)^2 = 9
>>
>>9075185
guess im wrong according to https://www.symbolab.com/solver/step-by-step/-3%5E%7B2%7D and >>9075169
>>
>>9075153
a computer (python, c++) would interpret that as -(3^2)
>>
-3 to the power of 2 ; (-3)^2.
>>
>>9075203
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.
>>
>>9075160
>Why would it be silly?

Because you would never want (-2)^10 in expression like -2^10+3^10 but rather -(2^10)
>>
File: python.png (25KB, 1071x97px) Image search: [Google]
python.png
25KB, 1071x97px
>>9075203
https://docs.python.org/2/reference/expressions.html#the-power-operator
>>
>>9075153
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.
>>
>>9075360
>Polish notation
Why would anybody follow Poland's example for anything
>>
>>9075360
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)
>>
>>9075153
Well, if you want it to be exact and give you the right answer, something like

-3^2 = (-(3^2))
>>
>>9075381
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
>>
>>9075719
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.
>>
>>9075153
>http://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.
>>
>>9075153
Why make a separate rule for unary when you can just treat it like subtraction?
>>
-3 is -1*3 so exponent first, -9
>>
>>9075253
>^ 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
>>
>>9075807
>because it's clear from the context what is meant.
it's actually not, which is the whole point of this thread
>>
>>9075153
(-3)^2
>>
>>9075253
>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.
>>
>>9075738
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
>>
>>9075386
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
>>
>>9076113
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.
>>
>>9076246
1. It's probably a HW assignment.
2. You're retarded
>>
>>9075153
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.
>>
>>9076900
-(3^3) is equal to (-3)^3, though.
>>
File: GJ.jpg (32KB, 1280x720px) Image search: [Google]
GJ.jpg
32KB, 1280x720px
>>9076929
Every instance of binary - can be replaced by binary + with unary -
GJ
>>
>>9076896
If it's a homework assignment, he needs to go to a real school.
Thread posts: 41
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.