Discuss / Python / 不用math.sqrt()函数,暴力解方程,直接~ 粗暴~

不用math.sqrt()函数,暴力解方程,直接~ 粗暴~

Topic source

仙羽_幻境

#1 Created at ... [Delete] [Delete and Lock User]
x = abs(c)
s = []
while abs(x) <= abs(c):
    if -c == a *x ** 2 + b * x:
        s.append(x)        
    x =round(x-0.01,2)    
return tuple(s)

''' 用到了round(),说起这个就是泪啊 吗的,这个Python有毒,小数点计算的时候会有BUG, 大家测试一下吧: ''' x=1.0 while x>0 : x=x-0.1 print(x)

#这代码的输出结果应该是:0.9  0.8  0.7 ....0.1
#但是事实上根本就不是这样的结果,结果却是:
# 0.9
# 0.8
# 0.7000000000000001
# 0.6000000000000001
# 0.5000000000000001
# 0.40000000000000013
# 0.30000000000000016
# 0.20000000000000015
# 0.10000000000000014
# 1.3877787807814457e-16
# -0.09999999999999987
#
#告诉我,这是神马东东,马蛋,我就说的嘛,任何方程理论上都应该能暴力破解的,结果被这个BUG搞死了,害我弄了半天,看了又看,才发现根本就是BUG的问题,然后用 round()解决了问题。

仙羽_幻境

#2 Created at ... [Delete] [Delete and Lock User]
#其实连公式都可以不用变:
x = abs(c)
s = []
while abs(x) <= abs(c):
    if a*x**2 + b*x + c ==0:
        s.append(x)
    x = round(x - 0.01, 2)
return tuple(s)

无风之名

#3 Created at ... [Delete] [Delete and Lock User]

while abs(x) <= abs(c): 这时数学上的什么方法吗?

仙羽_幻境

#4 Created at ... [Delete] [Delete and Lock User]

上一章有说到的一个函数,abs()返回绝对值 ,abs(-5) 返回值为:5

无风之名

#5 Created at ... [Delete] [Delete and Lock User]

我知道是求绝对值的函数,我想问的是,为什么要把a、c进行绝对值的比较?暴力法的原理是什么?


  • 1

Reply