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

/prog/ - Programming

Programming

Name
Email
Subject
REC
STOP
Comment *
File
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶Show post options & limits]
Confused? See the FAQ.
Options

Allowed file types:jpg, jpeg, gif, png, webp,webm, mp4, mov
Max filesize is16 MB.
Max image dimensions are15000 x15000.
You may upload5 per post.


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

7c5b80 No.4144

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 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][Nerve Center][Random][Post a Reply]
Delete Post [ ]
[]
[ / / / / / / / / / / / / / ] [ dir / random / 93 / biohzrd / hkacade / hkpnd / tct / utd / uy / yebalnia ]