本文介绍了对python中pi的sin和cos的怪异答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题.当我想查找某些角度的正弦或余弦时,我在python解释器中得到了奇怪的答案.

I have faced a really weird problem.when I want to find sin or cos of some angles, I get strange answers in python interpreter.

>>>from math import *
>>>sin(pi)
Out:1.2246467991473532e-16
>>>cos(pi)
Out:-1.0

它正确回答了cos,但罪恶很奇怪.而pi/2是反向的.

It answered cos correctly but sin was strange.while pi/2 is reverse.

>>>sin(pi/2)
Out: 1.0
cos(pi/2)
Out: 6.123233995736766e-17

我很困惑!

有人可以解释发生了什么事吗?

Can someone explain what's happening?

推荐答案

我认为以下内容的输出将回答您的问题.您的输出没有错,只是使用计算机进行计算的副作用.

I think the output to the following will answer your question. Your output isn't wrong, it's just the side effect of using computers for calculations.

print "sin(pi) = %.2f\ncos(pi/2) = %.2f" % (sin(pi), cos(pi/2))

这篇关于对python中pi的sin和cos的怪异答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:19