[Python] Why does decimal library raise comparison errors and fail to return results?

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a math library, mainly for the challenge of it. I received the challenges, all right! I am trying to get the arctan() function working, though to no avail. arctan() uses tan(), which has been tested against a TI 30x IIs calculator and proven to be accurate.

    def arccos(ratioin): # working, sometimes
    if ratioin > 1 or ratioin < -1:
    return False
    else:
    attempt = Decimal('0')
    target = Decimal(str(ratioin))
    for i in range(-2, terms + 1): # determines the place value for editing
    for j in range(10): # check 10 times for each place value, possibly excessive, but resource usage isn't currently a concern
    if Decimal(str(cos(float(attempt)))) == target: # if correct, return.
    attempt *= -1
    while attempt > 180: # limit the range of answers to 180 degrees or less
    attempt -= 180
    if attempt == Decimal('-0'): # eliminate -0 answers
    attempt = Decimal('0')
    return attempt

    if Decimal(str(cos(float(attempt)))) < target: # if too low, increase by the current place value
    attempt += (Decimal('10') ** -Decimal(str(i)))
    print(attempt, end = ' ')

    if Decimal(str(cos(float(attempt)))) > target: # if too high, decrease by the current place value
    attempt -= (Decimal('10') ** -Decimal(str(i)))


    def arctan(ratioin): # not working
    if ratioin > 1 or ratioin < -1:
    return False
    else:
    attempt = Decimal('0')
    target = Decimal(str(ratioin))
    print(type(target))
    for i in range(-2, terms + 1):
    for j in range(10):
    if Decimal(str(tan(float(attempt)))) == Decimal(target): # note that in these comparison lines, tan is used, rather than cos
    return attempt

    if Decimal(str(tan(float(attempt)))) < target:
    attempt += (Decimal('10') ** -Decimal(str(i)))

    if Decimal(str(tan(float(attempt)))) > target:
    attempt -= (Decimal('10') ** -Decimal(str(i)))


    The theory of operation behind arctan() is such:

    • Starting with the 100s place, add or subtract 100 to attempt, based on whether tan(attempt) is greater or less than the input.
    • repeat the above actions, moving to 10s place, 1s, place, 0.1s, 0.01, down until the number of places specified by terms.

    When I call arctan in the terminal, the following error is thrown:

    >>>num.arctan(1)
    <class 'decimal.Decimal'>
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:/Users/ccronk22/Documents/Python_Scripts\numerics.py", line 119, in arctan
    if Decimal(str(tan(float(attempt)))) == Decimal(target):
    decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
    >>>


    What I don't understand is that my arccos() function uses this same line, except that tan is replaced with cos. Strangely, though, arccos() sometimes works, provided you don't pass 4.9 or 5 in the following:

    >>>num.arccos(num.cos(4))
    Decimal('4')
    >>>num.arccos(num.cos(4.9))
    >>>num.arccos(num.cos(5))
    >>>


    Provided you don't give magic numbers, sin(), cos(), arccos(), and tan() are accurate according to my TI 30x IIs calculator. If needed, I can paste the entire library and post it on request.

    Continue reading...

Compartilhe esta Página