Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python math.acos中的域错误,输入有效_Python_Math_Trigonometry - Fatal编程技术网

python math.acos中的域错误,输入有效

python math.acos中的域错误,输入有效,python,math,trigonometry,Python,Math,Trigonometry,这是我的python代码。我试图找到以列表表示的两个向量之间的角度。运行此代码时,输出为: def dot(x, y): return sum([ x[i]*y[i] for i in range(1,10)]) def magnitude(x): return math.sqrt(sum([x[i]*x[i] for i in range(1,10)])) def cosine_similarity(x, y): x = dot(x, y) / (magnitude

这是我的python代码。我试图找到以列表表示的两个向量之间的角度。运行此代码时,输出为:

def dot(x, y):
    return sum([ x[i]*y[i] for i in range(1,10)])

def magnitude(x):
    return math.sqrt(sum([x[i]*x[i] for i in range(1,10)]))

def cosine_similarity(x, y):
    x = dot(x, y) / (magnitude(x)*magnitude(y))
    print(x)
    print(math.acos(x))
    return math.acos(x) / math.pi
1.0
回溯(最近一次呼叫最后一次):
文件“problem5.py”,第64行,在
打印(余弦_相似性(库[0][0],库[0][0]))
文件“problem5.py”,第41行,余弦形式
打印(数学acos(x))
ValueError:数学域错误
如果我在控制台或文件中键入 math.acos(1.0)
它准确地给了我0。我已经做了类型检查等,一切似乎都检查出来了

您会遇到舍入问题<由于浮点舍入错误,code>x可以采用类似
1.0000000000000002
的值,因此会触发错误。您可以用
numpy.acos
替换
math.acos
,以静默观察追加的内容。

您正在覆盖参数
x
。避免运行代码不显示任何内容。请显示调用
cosine\u similarity()
函数并给出错误的代码(可能缩小到较小的大小)。什么版本的Python?
1.0
Traceback (most recent call last):
  File "problem5.py", line 64, in <module>
    print(cosine_similarity(reservoir[0][0], reservoir[0][0]))
  File "problem5.py", line 41, in cosine_similarity
    print(math.acos(x))
ValueError: math domain error