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

1 投票
5 回答
4988 浏览
提问于 2025-04-17 09:22

作为一名年轻的程序员,我一直在寻找能用上我技能的地方。

现在,我正在学习三角函数,正在研究单位圆,转换角度到坐标的公式是 (sinθ, cosθ)(据我所知是这样的)。

不过,我遇到的困难是,我需要把这些值保持为分数。

简单来说,我计划的算法是:

i = 0
while i < 360:
    print(i, "=", calc(i))
    i += 15

这里,calc 可以取任何名字,它会是一个函数,给定 x = sin θ 和 y = cos θ,返回一个坐标(可能是一个元组)包含 x 和 y。

我遇到的问题是,Python 中的 sin 函数返回的是一个浮点数,范围在 -1 到 1 之间,但我需要找到一种方法让它返回分数。例如,在 这张图片 中,坐标是有理数。

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

5 个回答

0

下面这个例子是用来计算30度的余弦值的,能帮助你理解怎么做这件事。

>>> 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

这是运行后的结果。

>>> def Bar():
    print 'Trig\t'+'\t'.join(str(x) for x in xrange(0,91,15))
    for fn in [math.sin,math.cos,math.tan]:
        print fn.__doc__.splitlines()[0],'\t',
        print '\t'.join(foo(angle,fn) for angle in xrange(0,91,15) )

>>> Bar()
Trig    0       15       30       45       60       75       90
sin(x)  0.0    0.2588    1/2    1/√2    √3/2    0.9659    1
cos(x)  1      0.9659    √3/2    1/√2    1/2    0.2588    0.0
tan(x)  0.0    0.2679    1/√3    1      √3    3.7321    ∞
4

你试过使用fraction模块吗?我自己从来没有用过这个模块,不过我为了这个问题查了一下资料。

5

看起来你需要一个第三方模块,比如 sympy

>>> import sympy
>>> for i in range(0, 360, 15):
...     print i, sympy.sin(sympy.Rational(i, 180) * sympy.pi)
...

0 0
15 sin(pi/12)
30 1/2
45 2**(1/2)/2
60 3**(1/2)/2
75 sin(5*pi/12)
90 1
105 sin(5*pi/12)
120 3**(1/2)/2
135 2**(1/2)/2
150 1/2
165 sin(pi/12)
180 0
195 -sin(pi/12)
210 -1/2
225 -2**(1/2)/2
240 -3**(1/2)/2
255 -sin(5*pi/12)
270 -1
285 -sin(5*pi/12)
300 -3**(1/2)/2
315 -2**(1/2)/2
330 -1/2
345 -sin(pi/12)

撰写回答