Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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创建单位圆计算器?_Python_Math_Trigonometry_Fractions_Symbolic Math - Fatal编程技术网

使用Python创建单位圆计算器?

使用Python创建单位圆计算器?,python,math,trigonometry,fractions,symbolic-math,Python,Math,Trigonometry,Fractions,Symbolic Math,作为一名年轻的程序员,我一直在努力寻找我技能的应用 无论如何,我现在正在学习三角,我们正在研究单位圆,从度到坐标的转换公式是(sinθ,cosθ)(据我所知) 然而,我遇到的困难是,我需要将这些值保持为分数 基本上,我计划的算法是: i = 0 while i < 360: print(i, "=", calc(i)) i += 15 i=0 而我1/.5=2->1/2我能想到的最好办法就是把十进制数变成分数,就像你在六年级时那样。例如.5->1/.5=2->1/2正如伊

作为一名年轻的程序员,我一直在努力寻找我技能的应用

无论如何,我现在正在学习三角,我们正在研究单位圆,从度到坐标的转换公式是(sinθ,cosθ)(据我所知)

然而,我遇到的困难是,我需要将这些值保持为分数

基本上,我计划的算法是:

i = 0
while i < 360:
    print(i, "=", calc(i))
    i += 15
i=0
而我<360:
打印(i,“=”,计算(i))
i+=15
现在,calc可以被赋予任何名称,它将是一个函数,在给定x=sinθ和y=cosθ的情况下,返回x和y的坐标(可能是元组)

我遇到的问题是Python中的sin返回一个介于-1和1之间的浮点,但是,我需要找到一种方法让它返回一个分数。例如,坐标中有有理数


我该怎么办?我应该写我自己的正弦和余弦函数吗?如果是,我应该怎么做?

你试过这个模块了吗?我自己从来没有用过它,但在这个问题上我一直盯着它看。

你试过这个模块吗?我自己从来没有用过它,但在这个问题上我一直盯着它看。

我能想到的最好的方法就是把十进制数变成分数,就像你在六年级时那样。例如.5->1/.5=2->1/2

我能想到的最好办法就是把十进制数变成分数,就像你在六年级时那样。例如.5->1/.5=2->1/2

正如伊格纳西奥在评论中所说,对于大多数角度,你不能将正弦和余弦表示为分数,因为它们是无理数。因此,编写自己的正弦和余弦函数没有帮助。(你可以试试;这是一个有趣的练习,但速度会非常慢。Python中内置的正弦和余弦的实现是用C语言编写的,基于可能已有40年历史的代码,几代计算机科学家已经对它们进行了优化,所以你可能做得再好不过了。)

事实上,即使角度是整数度,通常也不能将正弦和余弦表示为分数,但在许多情况下,可以将它们的平方表示为分数。所以我建议计算正弦平方和余弦平方。(以45度为例)

当然,即使使用很好的圆角,也无法从(平方)正弦和余弦函数中得到分数,因为它们返回浮点数。最好的办法是将(近似的)十进制数转换成分数。您可以按照建议使用
分数
模块来执行此操作;如果需要,可以自由使用
limit_deminator
函数,因为您知道要查找的分数的分母很小。或者,您可以自己编写算法;这是另一个有指导意义的练习


最后,有一个提示:实际上,x坐标对应于余弦,y坐标对应于正弦,假设你的角度是按照通常的方式定义的。

正如Ignacio在评论中所说,对于大多数角度,你不能将正弦和余弦表示为分数,因为它们是无理数。因此,编写自己的正弦和余弦函数没有帮助。(你可以试试;这是一个有趣的练习,但速度会非常慢。Python中内置的正弦和余弦的实现是用C语言编写的,基于可能已有40年历史的代码,几代计算机科学家已经对它们进行了优化,所以你可能做得再好不过了。)

事实上,即使角度是整数度,通常也不能将正弦和余弦表示为分数,但在许多情况下,可以将它们的平方表示为分数。所以我建议计算正弦平方和余弦平方。(以45度为例)

当然,即使使用很好的圆角,也无法从(平方)正弦和余弦函数中得到分数,因为它们返回浮点数。最好的办法是将(近似的)十进制数转换成分数。您可以按照建议使用
分数
模块来执行此操作;如果需要,可以自由使用
limit_deminator
函数,因为您知道要查找的分数的分母很小。或者,您可以自己编写算法;这是另一个有指导意义的练习


最后,有一个提示:实际上,x坐标对应于余弦,y坐标对应于正弦,假设您的角度是按照通常的方式定义的。

看起来您需要第三方模块,例如:


看起来您需要一个第三方模块,例如:


下面的例子将帮助您了解如何找到30度的cos

>>> angle=30*math.pi/180 #30 degree in randian
>>> cosine = math.cos(angle) #Lets find the cosine of 30 degree
>>> #Square it. Helps to represent a range of irrational numbers to rational numbers
>>> cos2 = cosine ** 2
>>> # Lets drop some precision. Don't forget about float approximation
>>> cos2 = round(cos2,4)
>>> num = fractions.Fraction(cos2).numerator #Just the Numerator of the fraction
>>> den = fractions.Fraction(cos2).denominator #The denominator of the fraction
>>> def PerfSquare(n): #Square root in an Integer
    return int(n**0.5)**2 == n
# If Perfect Square then Find the Square root or else represent as a root

>>> num = str(num**0.5) if PerfSquare(num) else "root{0}".format(num) # If Perfect Square then Find the Square root or else represent as a root
>>> den = str(den**0.5) if PerfSquare(den) else "root{0}".format(den)
>>> cos = "{0}/{1}".format(num,den) #Combine Numerator and Denominator
>>> print cos
root3/2.0
这是一个由上述原理构成的函数

>>> HIGHVALUE=1000
>>> def foo(degree,trigfn):
    angle=degree*math.pi/180 #in randian
    trigval = trigfn(angle) #Lets find the trig function
    #Square it. Helps to represent a range of irrational numbers to rational numbers
    trigval2 = trigval ** 2
    # Lets drop some precission. Don't forget about float aproximation
    trigval2 = round(trigval2,5)
    if trigval > HIGHVALUE:
        return u'\u221e'
    num = fractions.Fraction(trigval2).numerator #Just the Numerator of the fraction
    den = fractions.Fraction(trigval2).denominator #The denominator of the fraction
    if (num > HIGHVALUE or den > HIGHVALUE):
        trigval2 = round(1/trigval2,4)
        den = fractions.Fraction(trigval2).numerator #Just the Numerator of the fraction
        num = fractions.Fraction(trigval2).denominator #The denominator of the fraction
    if num > HIGHVALUE or den > HIGHVALUE or num < 1 or den < 1:
        #Cannot be represented properly
        #Just return the value
        return str(round(trigval,4))
    # If Perfect Square then Find the Square root or else represent as a root
    num = str(int(num**0.5)) if PerfSquare(num) else u"\u221a{0}".format(num)
    den = str(int(den**0.5)) if PerfSquare(den) else u"\u221a{0}".format(den)
    return u"{0}".format(num) if den == "1" else u"{0}/{1}".format(num,den) #Combine Numerator and Denominator

下面的例子将帮助您了解如何找到30度的cos

>>> angle=30*math.pi/180 #30 degree in randian
>>> cosine = math.cos(angle) #Lets find the cosine of 30 degree
>>> #Square it. Helps to represent a range of irrational numbers to rational numbers
>>> cos2 = cosine ** 2
>>> # Lets drop some precision. Don't forget about float approximation
>>> cos2 = round(cos2,4)
>>> num = fractions.Fraction(cos2).numerator #Just the Numerator of the fraction
>>> den = fractions.Fraction(cos2).denominator #The denominator of the fraction
>>> def PerfSquare(n): #Square root in an Integer
    return int(n**0.5)**2 == n
# If Perfect Square then Find the Square root or else represent as a root

>>> num = str(num**0.5) if PerfSquare(num) else "root{0}".format(num) # If Perfect Square then Find the Square root or else represent as a root
>>> den = str(den**0.5) if PerfSquare(den) else "root{0}".format(den)
>>> cos = "{0}/{1}".format(num,den) #Combine Numerator and Denominator
>>> print cos
root3/2.0
这是一个由上述原理构成的函数

>>> HIGHVALUE=1000
>>> def foo(degree,trigfn):
    angle=degree*math.pi/180 #in randian
    trigval = trigfn(angle) #Lets find the trig function
    #Square it. Helps to represent a range of irrational numbers to rational numbers
    trigval2 = trigval ** 2
    # Lets drop some precission. Don't forget about float aproximation
    trigval2 = round(trigval2,5)
    if trigval > HIGHVALUE:
        return u'\u221e'
    num = fractions.Fraction(trigval2).numerator #Just the Numerator of the fraction
    den = fractions.Fraction(trigval2).denominator #The denominator of the fraction
    if (num > HIGHVALUE or den > HIGHVALUE):
        trigval2 = round(1/trigval2,4)
        den = fractions.Fraction(trigval2).numerator #Just the Numerator of the fraction
        num = fractions.Fraction(trigval2).denominator #The denominator of the fraction
    if num > HIGHVALUE or den > HIGHVALUE or num < 1 or den < 1:
        #Cannot be represented properly
        #Just return the value
        return str(round(trigval,4))
    # If Perfect Square then Find the Square root or else represent as a root
    num = str(int(num**0.5)) if PerfSquare(num) else u"\u221a{0}".format(num)
    den = str(int(den**0.5)) if PerfSquare(den) else u"\u221a{0}".format(den)
    return u"{0}".format(num) if den == "1" else u"{0}/{1}".format(num,den) #Combine Numerator and Denominator

只有某些定义明确的角度具有有理三角值。
坐标是有理数。
。错了,这是不合理的。例如,根(3)/2 30度的x坐标是一个无理数。只有某些定义明确的角度具有有理三角值。
坐标是有理数。
。错了,这是不合理的。例如根(3)/2 30度的x坐标是一个无理数。为什么要重新发明轮子?一个由专业人士编写、测试和使用的模块是可用的,然后提出通用公式并没有多大帮助。我不知道有一个用于此的模块,我只是简单地解释了完成此任务的编程方式。如果可以做得更容易,那太好了!程序员的工作量更少。这没关系,但我不知道这是否是最好的方法,我希望有可能