首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python cos(90)和cos(270)非0

Python cos(90)和cos(270)非0
EN

Stack Overflow用户
提问于 2012-11-07 06:15:51
回答 3查看 14.3K关注 0票数 5

怎么一回事??

测试sin和cos函数,以找出为什么在将坐标输出到SVG文件中时,我会在错误的位置得到如此漂亮的定位。所以我写了这个测试代码,我可以预测答案是什么,以找出原因。奇怪的是,没有任何影响计算的东西,它自己添加了这个行为,而只是简单地说明了我将要停留的位置。如果位置为0,计算后将变为0,则不起作用,但如果位置为1,则计算后将变为1,则可以工作。

第一个测试:

代码语言:javascript
复制
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,即使它不影响计算,它也可以工作,这是:

代码语言:javascript
复制
r * math.cos(math.radians(270))

将计算结果与x坐标相加

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2012-11-07 06:20:47

事实上,这是一个非常低的数字,非常接近于零。这里有一篇很棒的文章,可以帮助你理解浮动的常见挑战和陷阱:"What Every Computer Scientist Should Know About Floating-Point Arithmetic

票数 8
EN

Stack Overflow用户

发布于 2012-11-07 06:19:45

您正在处理舍入错误,这在使用浮点数学时(大多数情况下)是不可避免的(请参阅其他人已经链接的paper以了解到底发生了什么)。

在许多情况下,你可以减少它们的影响(通过以“智能”顺序执行操作或以更“浮点友好”的方式重新制定表达式),但在你的情况下,最简单的做法是将结果舍入到例如6位小数,并对此满意。当然,你不需要更精确的定位,你会得到你所期望的“标准”角度的结果。

票数 6
EN

Stack Overflow用户

发布于 2022-01-15 04:38:57

为了今天的一个项目,我不得不和cos,sin,以及270度的晒黑打交道。如果我不用谷歌搜索结果来检查,我就不会知道有错误。(我知道无缘无故地将度数设为常量,只是把它变成弧度是荒谬的,这就是他们想要的。)

只要我先绕过弧度,cos和sin就会对我起作用,但晒黑的效果却很差。

我通过手动执行数学运算并捕获不能被0除的错误并将其设置为Undefined来修复切线:

代码语言:javascript
复制
    #variables for various trigonometric functions (sine, cosine and tangent) of 270 degrees
    DEGREE = 270
    RADIAN = round(m.radians(DEGREE), 3) #the degree has to be converted to a radian to input into the math.cos and math.sin functions
    COS = round(m.cos(RADIAN), 2) #pass the radian to get the cosine
    SIN = round(m.sin(RADIAN), 2) #pass the radian to get the sine

    try:
         # the math library did not return the correct answer for tangent when dividing by 0
         # so, I manually did the math, this catches the division by zero and correctly labels the tangent
         # as undefined
        tan = round(SIN/COS, 2)
        
    except:
        tan = "Undefined"

    print("\n\nMathematical Constants") #display a title for the list

    #headers list for each function
    headers = ["Pi", "e", "Sine(270\u00b0)", "Cosine(270\u00b0)", "Tangent(270\u00b0)"]
    #list of each constant
    dataList = [round(m.pi, 2), round(m.e, 2), SIN, COS , tan]

    #looping iterator to diplay each function
    i = 0
    while i < len(headers):
        print("------")
        print(headers[i] + ": " + str(dataList[i]))

        i += 1 #increase iterator by one
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13260296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档