简体   繁体   English

无法将负数提高到小数幂

[英]Can't Raise negative numbers to a fractional power

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

My program takes user input and solves the quadratic function. 我的程序接受用户输入并解决二次函数。 However it is unable to process negative numbers, and I get the fractional power error. 但是,它无法处理负数,并且出现小数幂错误。 I want to know how to make it so that my program can accept negative numbers and still give correct answers. 我想知道如何制作它,以便我的程序可以接受负数并仍然给出正确的答案。

I tried following but it doesn't give me an output, just blank lines: 我尝试了以下操作,但它没有给我输出,只是空白行:

x1 = -b + (b **2 - 4*a*c+0j) ** 0.5
x2 = x1 / (2 * a)

you should work with complex numbers for this purpose use cmath: 为此,您应该使用cmath使用复数:

import cmath
x1 = -b + cmath.sqrt(b **2 - 4*a*c)
x2 = x1 / (2 * a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM