对python中pi的sin和cos的怪异答案 [英] weird answer to sin and cos of pi in python

查看:73
本文介绍了对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?

顺便说一句,我使用冠层和python 2.7.9

BTW I use canopy and python 2.7.9

推荐答案

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

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的怪异答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆