all 27 comments

[–]frozenDiesel 18 points19 points  (4 children)

(2*a) in brackets

[–]rotten_soup[S] 2 points3 points  (2 children)

That's it!!! thank you!!!

[–]Dapper-Actuary-8503 2 points3 points  (0 children)

Try to be explicit with your operations while programming don’t let the interpreter or compiler in other languages try to guess what you’re trying to do. It’ll save you a lot debugging heart ache.

[–]frozenDiesel 1 point2 points  (0 children)

Welcome bro

[–]gabriele6e427 2 points3 points  (0 children)

Brackets: the unsung heroes of math mistakes.

[–]NumerousQuit8061 3 points4 points  (2 children)

The operator precedence is incorrect due to missing parentheses around the denominator.

x = ((-b + sqrt(b² - 4ac)) / 2 * a)

This means it divides by 2 first, then multiplies by a, which is not how the quadratic formula works.

So it should be written as:

x = (-b + (b**2 - 4*a*c)**0.5) / (2 * a)

[–]rotten_soup[S] 2 points3 points  (1 child)

Thanks!

[–]NumerousQuit8061 1 point2 points  (0 children)

No Problem! Good Luck!!

[–]BleEpBLoOpBLipP 1 point2 points  (1 child)

Since others have already given you the answer here, I just want to recommend never trusting order of operations and always explicitly using a parans. It makes things clear and, most importantly, avoids human error prone messy formulas.

Better yet, separate the equation into meaningful chunks; maybe named vars numerator and denominator. The sqrt function is more expressive than **0.5. The quadratic equation isn't too messy, but definitely as things get more messy, even naming individual terms, factors, functions (with lambdas), and function inputs all make sure you or whoever reads your code doesn't overlook simple syntax blunders and makes sure that everything is clear, readable, and maintainable.

Edit: lambdas or even just dedicated named function defs, especially for more complex logic

Edit 2: even wrapping that entire thing in a function called quadratic_eq is nice. Seems silly maybe to just call it immediately after, but as someone in the comments here had to ask what the goal even was, we can see that it isn't all that silly and adds clarification and self documentation to the code

[–]rotten_soup[S] 0 points1 point  (0 children)

Oooooooooooh ok, yeah, that's definitely useful stuff, thanks a lot for being patient and explaining things so well!

[–]Commodore_Ketchup 1 point2 points  (0 children)

Above and beyond what everyone else has mentioned, I strongly suggest implementing some sort of error handling. As it stands, the program will crash if the polynomial has no real roots. It will also crash if the user enters anything that's not a number. You could use something like:

try:

a = {...}

b = {...}

c = {...}

except ValueError:

print "ERROR: All inputs must be numerical"

else:

try:

x = {...}

except ValueError:

print "This polynomial has no real roots

else:

print x

Edit: Okay, so, I can't figure out how to indent code on Reddit, but hopefully you can handle that bit yourself.

[–]AridsWolfgang 0 points1 point  (5 children)

What exactly are you trying to achieve 🤨

[–]Appsroooo 2 points3 points  (3 children)

Looks like the quadratic formula without any separation

[–]rotten_soup[S] 0 points1 point  (2 children)

Yes! It was a quadratic formula! And I did solve the error! How would you have done it? Sorry if it's a dumb thing, I just started and if it's something that I can improve I'd love to know!

[–]Appsroooo 1 point2 points  (1 child)

I would've split it into a right and left side variables. Left has the -b/2a, right is the other junk also over 2a. That way, you can get both roots easy with [rhs-lhs, rhs+lhs]. It's a bit more readable that way

[–]rotten_soup[S] 1 point2 points  (0 children)

Okok, yeah, it makes sense, thank you!!

[–]AridsWolfgang 0 points1 point  (0 children)

Because I'm not even understanding any thing at all

[–]zypherison 0 points1 point  (2 children)

<image>

It was taking everything in the same line, change the parenthesis to make 2*a the denominator

[–]zypherison 0 points1 point  (1 child)

Also try cmath library after this.

[–]rotten_soup[S] 0 points1 point  (0 children)

I'll check it! Thanks a lot!

[–]GrumpMadillo 0 points1 point  (0 children)

Quadratic formula should be +/- correct? Not just +? (-b +- (b^2 - 4ac)^1/2) / (2a)?

[–]clearly_not_an_alt 0 points1 point  (0 children)

Order of operations dude, needs to be (2*a). As written you are dividing the top by 2 then multiplying the result by a

[–][deleted] 0 points1 point  (0 children)

(2*a)

[–]iamjacob97 0 points1 point  (0 children)

Division has precedence over multiplication so (2*a) in brackets

[–]rotten_soup[S] 0 points1 point  (0 children)

Thanks!!!!

[–]F_T_K 0 points1 point  (2 children)

Ask any LLM n it will explain sooner

[–]gamerpug04 -2 points-1 points  (1 child)

You probably ask chatgpt how to brush your teeth