Python cos(90)和cos(270)不是0 [英] Python cos(90) and cos(270) not 0

查看:504
本文介绍了Python cos(90)和cos(270)不是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了什么事?

测试sin和cos函数以找出为什么在将坐标输出到SVG文件时能在错误的位置上得到如此精美的定位.因此,我编写了此测试代码,可以预测答案以找出原因.奇怪的是,没有什么影响计算本身会增加此行为,而只是增加我将要停留的位置.如果位置为0且在计算后将变为0,则它​​将不起作用,但如果位置为1且在计算后将变为1,则它将起作用.

Testing out sin and cos functions to find out why I get so beautiful positioning on the wrong places when outputting my coordinates into a SVG file. So I made this test code, which I can predict what the answer is to find out why. Oddly nothing that effects the calculation it self adds this behavior but simply the position of where I'm about to stay. If the position is 0 and will become 0 after the calculation it won't work, but if position is 1 and will become 1 after the calculation it works.

首次测试:

import math

cX = 2
cY = 2
r = 2

rcX = cX + (r * math.cos(math.radians(0)))
rcY = cY + (r * math.sin(math.radians(0)))

print rcX #4
print rcY #2
r = 1

rlX = rcX + (r * math.cos(math.radians(90)))
rlY = rcY + (r * math.sin(math.radians(90)))

print rlX #4
print rlY #3
r = 4

flX = rlX + (r * math.cos(math.radians(180)))
flY = rlY + (r * math.sin(math.radians(180)))

print flX #0
print flY #3
r = 2

print r * math.cos(math.radians(270))
print flX + (r * math.cos(math.radians(270))) #-3.67394039744e-16 should be 0
print flY + (r * math.sin(math.radians(270))) #1

现在,我将cX更改为3,即使它不影响以下计算,它也可以工作:

Now I change cX to 3 and it works even if it doesn't effect the calculation which is:

r * math.cos(math.radians(270))

该计算的结果将添加到x坐标

The result of that calculation is added to the x coordination

import math

cX = 3
cY = 2
r = 2

rcX = cX + (r * math.cos(math.radians(0)))
rcY = cY + (r * math.sin(math.radians(0)))

print rcX #5
print rcY #2
r = 1

rlX = rcX + (r * math.cos(math.radians(90)))
rlY = rcY + (r * math.sin(math.radians(90)))

print rlX #5
print rlY #3
r = 4

flX = rlX + (r * math.cos(math.radians(180)))
flY = rlY + (r * math.sin(math.radians(180)))

print flX #1
print flY #3
r = 2

print r * math.cos(math.radians(270))
print flX + (r * math.cos(math.radians(270))) #1
print flY + (r * math.sin(math.radians(270))) #1

推荐答案

实际上,它是一个非常低的数字,非常接近零.这是一篇很棒的文章,可以帮助您理解浮子的常见挑战和陷阱:"

Indeed, it is a very low number, awfully close to zero. Here's a great article which can help you understand the common challenges and pitfalls of floats: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"

这篇关于Python cos(90)和cos(270)不是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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