[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ][Options][ watchlist ]

/prog/ - Programming

Programming
You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.

Name
Email
Subject
REC

0:00

Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶Show post options & limits]
Confused? See the FAQ.
Expand all images

File (hide): 1461439931944.jpg (18.75 KB,300x225,4:3,python.jpg) (h) (u)

[–]

7c5b80 (2) No.4144 [Watch Thread][Show All Posts]

What in the god damn is wrong with my Python code?


from math import sqrt
a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
posQuad=(-b+sqrt(b**2-4*a*c))/2*a
negQuad=(-b-sqrt(b**2-4*a*c))/2*a
print(str(posQuad) + ", " + str(negQuad))

It keep giving me a 'ValueError' for not calling math for 'sqrt' even though I did in the first line and when I do it individually for each 'sqrt' function, it gives me a 'NameError math not defined'.

____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

7c5b80 (2) No.4147

I ran it, got this:


$ ./trash.py
a=3
b=2
c=1
Traceback (most recent call last):
File "./trash.py", line 7, in <module>
posQuad=(-b+sqrt(b**2-4*a*c))/2*a
ValueError: math domain error
$ ./trash.py
a=1
b=5
c=1
-0.20871215252208009, -4.7912878474779195
$

You're not running into an error about not declaring math. That's a math domain error. If your error was about the module, you'd get a NameError. Python doesn't make assumptions like you think it does. If you used sqrt without importing it, it wouldn't tell you shit about the "math" module, it would just complain that it doesn't know what sqrt is. Think about what a "domain" is in math. The problem is that you are using numbers that make your determinant negative, which means you're running a square root of a negative, which python's math module can't do.

A bigger issue is your order of operations is fucked up. Quadratic equation is x = (-b +/- sqrt((b^2) - (2ac)))/(2a). The way you have it there is wrong because order of operations places multiplication on the same precedence as division, which multiplies a after the division, effectively putting a in the numerator.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Screencap][Nerve Center][Random][Update] ( Scroll to new posts) ( Auto) 5
1 replies | 0 images | 1 UIDs | Page 6
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ][ watchlist ]