Skip to content

Instantly share code, notes, and snippets.

@joshuachris2001
Last active March 13, 2025 02:07
Show Gist options
  • Save joshuachris2001/ed0dc62e90addef8c43433ca8a13e7c4 to your computer and use it in GitHub Desktop.
Save joshuachris2001/ed0dc62e90addef8c43433ca8a13e7c4 to your computer and use it in GitHub Desktop.

Revisions

  1. joshuachris2001 revised this gist Mar 13, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions find_x_where_y_is_zero.py
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,14 @@
    driv_f = lambda x: 13*cos(x)
    starting_point = -4

    _loop_limits = 8
    _loop_limits = 20
    _loop_snap_shot = 5

    _ans = starting_point - ( f(starting_point) / driv_f(starting_point))

    for loop in range(_loop_limits):
    _ans = _ans - ( f(_ans) / driv_f(_ans) )
    if (loop % _loop_snap_shot):
    print("loop snap",loop,'\n',_ans)
    _ans = _ans - ( f(_ans) / driv_f(_ans) )
    if (loop % _loop_snap_shot) == 0:
    print("loop snap",loop,'\n',_ans)

    print("final loop",'\n',_ans)
  2. joshuachris2001 revised this gist Mar 13, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion find_x_where_y_is_zero.py
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@

    for loop in range(_loop_limits):
    _ans = _ans - ( f(_ans) / driv_f(_ans) )
    if (_loop_snap_shot == loop):
    if (loop % _loop_snap_shot):
    print("loop snap",loop,'\n',_ans)

    print("final loop",'\n',_ans)
  3. joshuachris2001 revised this gist Mar 12, 2025. No changes.
  4. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -32,13 +32,13 @@ def matrix_xy_stats(matrix):
    print("Y:")
    print("{0}".format(y))
    print("theta degriess:")
    theta = (atan(x/y) * 180) / pi # degriees is disired
    print(theta) # pre processed
    theta = (atan2(y, x) * 180) / pi # degriees is disired
    print(theta) # pre processed
    while theta < 0:
    theta += 360
    while theta > 360:
    while theta >= 360:
    theta -= 360
    print(theta) # after filters
    print(theta) # after filters
    print("magnitude:")
    print("abs(abs({0}))".format(sqrt((x)**2+(y)**2)))
    print("sqrt({0})".format((x)**2+(y)**2))
  5. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ def matrix_xy_stats(matrix):
    print("Y:")
    print("{0}".format(y))
    print("theta degriess:")
    theta = (atan(x/y) * 180) / pi
    theta = (atan(x/y) * 180) / pi # degriees is disired
    print(theta) # pre processed
    while theta < 0:
    theta += 360
  6. joshuachris2001 revised this gist Mar 12, 2025. No changes.
  7. joshuachris2001 revised this gist Mar 12, 2025. No changes.
  8. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -16,31 +16,31 @@
    #from cmath import *

    if 'complex' in globals():
    __not_numworks = True
    __not_numworks = True
    else:
    __not_numworks = False
    __not_numworks = False

    def matrix_xy_stats(matrix):
    if len(matrix[0]) == 2:
    x = matrix[0][0]
    y = matrix[0][1]
    else:
    x = matrix[0][0]
    y = matrix[1][0]
    print("X:")
    print("{0}".format(x))
    print("Y:")
    print("{0}".format(y))
    print("theta degriess:")
    theta = atan(x/y)
    print(theta) # pre processed
    while theta < 0:
    theta += 360
    while theta > 360:
    theta -= 360
    print(theta) # after filters
    print("magnitude:")
    print("abs(abs({0}))".format(sqrt((x)**2+(y)**2)))
    print("sqrt({0})".format((x)**2+(y)**2))
    print("the imaginary i formula is:")
    print("({0})+({1})*1j".format(x,y))
    if len(matrix[0]) == 2:
    x = matrix[0][0]
    y = matrix[0][1]
    else:
    x = matrix[0][0]
    y = matrix[1][0]
    print("X:")
    print("{0}".format(x))
    print("Y:")
    print("{0}".format(y))
    print("theta degriess:")
    theta = (atan(x/y) * 180) / pi
    print(theta) # pre processed
    while theta < 0:
    theta += 360
    while theta > 360:
    theta -= 360
    print(theta) # after filters
    print("magnitude:")
    print("abs(abs({0}))".format(sqrt((x)**2+(y)**2)))
    print("sqrt({0})".format((x)**2+(y)**2))
    print("the imaginary i formula is:")
    print("({0})+({1})*1j".format(x,y))
  9. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -13,16 +13,20 @@
    """

    from math import *
    from cmath import *
    #from cmath import *

    if 'complex' in globals():
    __not_numworks = True
    else:
    __not_numworks = False

    def matrix_xy_stats(matrix):
    if len(matrix) == 2:
    if len(matrix[0]) == 2:
    x = matrix[0][0]
    y = matrix[0][1]
    else:
    x = matrix[0][0]
    y = matrix[1][0]

    print("X:")
    print("{0}".format(x))
    print("Y:")
    @@ -35,8 +39,8 @@ def matrix_xy_stats(matrix):
    while theta > 360:
    theta -= 360
    print(theta) # after filters

    print("magnitude:")
    print("abs(abs({0}))".format(sqrt((x)**2+(y)**2)))
    print("sqrt({0})".format((x)**2+(y)**2))
    print("the imaginary i formula is:")
    print("({0})+({1})*1j".format(x,y))
  10. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ def matrix_xy_stats(matrix):
    print("{0}".format(x))
    print("Y:")
    print("{0}".format(y))
    print("theta:")
    print("theta degriess:")
    theta = atan(x/y)
    print(theta) # pre processed
    while theta < 0:
  11. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@
    matrix_xy_stats() - function to analize a 2d matix to output information,
    such as angle between <0,0> as theta, representing as x+yi, etc.
    will use degriess as it will be easier.
    valid inputs:
    [[X,Y]] OR [[X],[Y]]
    @@ -25,5 +27,16 @@ def matrix_xy_stats(matrix):
    print("{0}".format(x))
    print("Y:")
    print("{0}".format(y))
    print("theta:")
    theta = atan(x/y)
    print(theta) # pre processed
    while theta < 0:
    theta += 360
    while theta > 360:
    theta -= 360
    print(theta) # after filters

    print("magnitude:")
    print("abs(abs({0}))".format(sqrt((x)**2+(y)**2)))
    print("the imaginary i formula is:")
    print("({0})+({1})*1j".format(x,y))
  12. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,29 @@
    """
    2d matrix for XY graph analysis
    matrix_xy_stats() - function to analize a 2d matix to output information,
    such as angle between <0,0> as theta, representing as x+yi, etc.
    valid inputs:
    [[X,Y]] OR [[X],[Y]]
    """

    from math import *
    from cmath import *

    def matrix_xy_stats(matrix):
    if len(matrix) == 2:
    x = matrix[0][0]
    y = matrix[0][1]
    else:
    x = matrix[0][0]
    y = matrix[1][0]

    print("X:")
    print("{0}".format(x))
    print("Y:")
    print("{0}".format(y))
    print("the imaginary i formula is:")
    print("({0})+({1})*1j".format(x,y))
  13. joshuachris2001 revised this gist Mar 12, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions xy_matrix_info.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    from math import *
  14. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,8 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000):
    if (_is_neg):
    current_numerator*=-1
    return current_numerator, current_denominator
    assert brute_continued_fraction(0.5,max_denominator = 10) == (-1,2)

    assert brute_continued_fraction(-0.5,max_denominator = 10) == (-1,2)
    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)


  15. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000):
    if (_is_neg):
    current_numerator*=-1
    return current_numerator, current_denominator

    assert brute_continued_fraction(0.5,max_denominator = 10) == (-1,2)
    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)


  16. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000):
    current_numerator = x
    current_denominator = y
    if (_is_neg):
    current_denominator*=-1
    current_numerator*=-1
    return current_numerator, current_denominator

    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)
  17. joshuachris2001 revised this gist Mar 5, 2025. No changes.
  18. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000):
    _is_neg = False
    if (_x < 0):
    _is_neg = True
    _x = abs(_x)
    current_numerator, current_denominator = max_denominator, max_denominator
    for x in range(1,max_denominator):
    for y in range(1,max_denominator):
  19. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ def _decToFrac():
    print("The decimal " + str(decimal) + " as a simplified fraction is\n " + str(numerator)+"/"+str(denominator))

    # backup brute forcing
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000000):
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000):
    _is_neg = False
    if (_x < 0):
    _is_neg = True
  20. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,18 @@ def _decToFrac():

    # backup brute forcing
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000000):
    _is_neg = False
    if (_x < 0):
    _is_neg = True
    current_numerator, current_denominator = max_denominator, max_denominator
    for x in range(1,max_denominator):
    for y in range(1,max_denominator):
    D = x / y
    if abs(D - _x) <= tolerance and y < current_denominator:
    current_numerator = x
    current_denominator = y
    if (_is_neg):
    current_denominator*=-1
    return current_numerator, current_denominator

    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)
  21. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions find_x_where_y_is_zero.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,17 @@
    from math import *

    f = lambda x: 13* sin(x)-12
    driv_f = lambda x: 13*cos(x)
    starting_point = -4

    _loop_limits = 8
    _loop_snap_shot = 5

    _ans = starting_point - ( f(starting_point) / driv_f(starting_point))

    for loop in range(_loop_limits):
    _ans = _ans - ( f(_ans) / driv_f(_ans) )
    if (_loop_snap_shot == loop):
    print("loop snap",loop,'\n',_ans)

    print("final loop",'\n',_ans)
  22. joshuachris2001 revised this gist Mar 5, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions find_x_where_y_is_zero.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    from math import *
  23. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 90 additions and 1 deletion.
    91 changes: 90 additions & 1 deletion trig_wheel.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,90 @@
    from math import *
    from math import*
    from turtle import*

    speed(10)
    hideturtle()

    width(2)
    up()
    goto(0,-90)
    down()
    circle(90)

    liste_angl=(0,30,45,60,90,120,135,150,180,210,225,240,270,300,315,330)
    liste_str=("0 2π","π/6","π/4","π/3","π/2","2π/3","3π/4","5π/6","π -π","7π/6","5π/4","4π/3","3π/2","5π/3","7π/4","11π/6")

    x=0
    width(1)
    color("grey")
    down()

    while x<=15:

    goto(0,0)
    setheading(liste_angl[x])
    forward(90)
    x=x+1

    y=0
    x=0
    color("black")
    up()

    while x<=4:
    y=-2
    goto(0,0)
    setheading(liste_angl[x])
    forward(90+y)
    write((liste_str[x]))
    x=x+1

    while 4<x<8:
    y=17
    goto(0,0)
    setheading(liste_angl[x]+12)
    forward(90+y)
    write((liste_str[x]))
    x=x+1

    while 8<=x<=12:
    y=23
    goto(0,0)
    setheading(liste_angl[x])
    if x==8:
    y=y+13
    forward(90+y)
    write((liste_str[x]))
    x=x+1

    while 12<x<=15:
    y=14
    goto(0,0)
    setheading(liste_angl[x])
    forward(90+y)
    write((liste_str[x]))
    x=x+1

    up()
    goto(0,0)
    down()
    write("0")

    up()
    goto(22,42)
    down()
    write("x")

    up()
    goto(22,-42)
    down()
    write("-x")

    up()
    goto(-42,42)
    down()
    write("x-π")

    up()
    goto(-42,-42)
    down()
    write("π-x")
  24. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions trig_wheel.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    from math import *
  25. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    import math
    from math import pi

    def _decToFrac():
    decimal = float(input("Enter a decimal number to convert to a fraction: "))
  26. joshuachris2001 revised this gist Feb 24, 2025. No changes.
  27. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,7 @@ def _continued_fraction_to_fraction(cf):

    return (denominator, numerator) if len(cf) % 2 == 0 else (denominator, numerator)

    def continued_fraction(dec):
    def _continued_fraction(dec):
    cf = _decimal_to_continued_fraction(dec)
    n, d = _continued_fraction_to_fraction(cf)
    #print(n,d)
    @@ -121,6 +121,9 @@ def __continued_fraction(x, tolerance=1e-9, max_denominator=1000000):
    print(h0, k0)
    return h0, k0

    assert continued_fraction(0.5) == (1,2)
    assert continued_fraction(0.3333) == (1,3)
    assert continued_fraction(0.666666) == (2,3)
    assert _continued_fraction(0.5) == (1,2)
    assert _continued_fraction(0.3333) == (1,3)
    assert _continued_fraction(0.666666) == (2,3)

    def to_frac(dec):
    return _continued_fraction(dec)
  28. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 5 additions and 38 deletions.
    43 changes: 5 additions & 38 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import math

    def decToFrac():
    def _decToFrac():
    decimal = float(input("Enter a decimal number to convert to a fraction: "))
    # Handle negative numbers
    is_negative = decimal < 0
    @@ -16,24 +16,6 @@ def decToFrac():
    #print(f"The decimal {decimal} as a simplified fraction is {numerator}/{denominator}")
    print("The decimal " + str(decimal) + " as a simplified fraction is\n " + str(numerator)+"/"+str(denominator))

    def decToRad():
    decimal = float(input("Enter a decimal number to convert to a fraction of π: "))
    pi = math.pi
    ratio = decimal / pi
    # Handle negative numbers
    is_negative = ratio < 0
    ratio = abs(ratio)

    # Tolerance for stopping the algorithm
    #tolerance = 1e-10
    #max_denominator = 1000000 # To prevent excessively large denominators

    numerator, denominator = continued_fraction(ratio)#, tolerance, max_denominator)
    if is_negative:
    numerator = -numerator
    #print(f"The decimal {decimal} as a fraction of π is {numerator}/{denominator}π")
    print("The decimal " + str(decimal) + " as a fraction of π is " + str(numerator) +"/"+ str(denominator)+"π")

    # backup brute forcing
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000000):
    current_numerator, current_denominator = max_denominator, max_denominator
    @@ -48,7 +30,7 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000000):
    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)


    def decimal_to_continued_fraction(x, tolerance=1e-2, max_terms=400):
    def _decimal_to_continued_fraction(x, tolerance=1e-2, max_terms=400):
    cf = []
    sign = -1 if x < 0 else 1
    x = abs(x)
    @@ -72,7 +54,7 @@ def decimal_to_continued_fraction(x, tolerance=1e-2, max_terms=400):
    #print(cf)
    return cf

    def continued_fraction_to_fraction(cf):
    def _continued_fraction_to_fraction(cf):
    if not cf:
    return 0, 1

    @@ -85,8 +67,8 @@ def continued_fraction_to_fraction(cf):
    return (denominator, numerator) if len(cf) % 2 == 0 else (denominator, numerator)

    def continued_fraction(dec):
    cf = decimal_to_continued_fraction(dec)
    n, d = continued_fraction_to_fraction(cf)
    cf = _decimal_to_continued_fraction(dec)
    n, d = _continued_fraction_to_fraction(cf)
    #print(n,d)
    return n, d

    @@ -142,18 +124,3 @@ def __continued_fraction(x, tolerance=1e-9, max_denominator=1000000):
    assert continued_fraction(0.5) == (1,2)
    assert continued_fraction(0.3333) == (1,3)
    assert continued_fraction(0.666666) == (2,3)

    def main():
    for i in range(1):
    print("\n1. Convert decimal to fraction")
    print("2. Convert decimal to fraction of π")
    print("3. Exit")
    choice = input("Choose an option: ")
    if choice == "1":
    decToFrac()
    elif choice == "2":
    decToRad()
    elif choice == "3":
    break
    else:
    print("Invalid choice. Please choose a valid option.")
  29. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    import math
    from math import pi

    def decToFrac():
    decimal = float(input("Enter a decimal number to convert to a fraction: "))
    @@ -88,7 +87,7 @@ def continued_fraction_to_fraction(cf):
    def continued_fraction(dec):
    cf = decimal_to_continued_fraction(dec)
    n, d = continued_fraction_to_fraction(cf)
    # print(n,d)
    #print(n,d)
    return n, d

    def __continued_fraction(x, tolerance=1e-9, max_denominator=1000000):
  30. joshuachris2001 revised this gist Feb 24, 2025. 1 changed file with 94 additions and 3 deletions.
    97 changes: 94 additions & 3 deletions simpledectofrac.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    from math import *
    import math
    from math import pi

    def decToFrac():
    decimal = float(input("Enter a decimal number to convert to a fraction: "))
    @@ -36,7 +36,7 @@ def decToRad():
    print("The decimal " + str(decimal) + " as a fraction of π is " + str(numerator) +"/"+ str(denominator)+"π")

    # backup brute forcing
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 10000):
    def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 1000000):
    current_numerator, current_denominator = max_denominator, max_denominator
    for x in range(1,max_denominator):
    for y in range(1,max_denominator):
    @@ -46,9 +46,100 @@ def brute_continued_fraction(_x, tolerance = 1e-5, max_denominator = 10000):
    current_denominator = y
    return current_numerator, current_denominator

    assert brute_continued_fraction(0.5,max_denominator = 5) == (1,2)
    assert brute_continued_fraction(0.5,max_denominator = 50) == (1,2)


    def decimal_to_continued_fraction(x, tolerance=1e-2, max_terms=400):
    cf = []
    sign = -1 if x < 0 else 1
    x = abs(x)

    for _ in range(max_terms):
    term = math.floor(x)
    cf.append(term)
    remainder = x - term

    if remainder < tolerance:
    break

    try:
    x = 1 / remainder
    except ZeroDivisionError:
    break

    if cf:
    cf[0] *= sign # Apply sign to first term
    cf[0]
    #print(cf)
    return cf

    def continued_fraction_to_fraction(cf):
    if not cf:
    return 0, 1

    numerator = 1
    denominator = cf[-1]

    for term in reversed(cf[:-1]):
    numerator, denominator = denominator, term * denominator + numerator

    return (denominator, numerator) if len(cf) % 2 == 0 else (denominator, numerator)

    def continued_fraction(dec):
    cf = decimal_to_continued_fraction(dec)
    n, d = continued_fraction_to_fraction(cf)
    # print(n,d)
    return n, d

    def __continued_fraction(x, tolerance=1e-9, max_denominator=1000000):
    """Finds the fraction approximation for x using continued fractions."""
    from math import floor

    # Handle exact integers
    if x == int(x):
    return int(x), 1

    a0 = floor(x)
    h1, k1 = 1, 0 # Numerators and denominators for previous two convergents
    h0, k0 = a0, 1

    x = x - a0 # Fractional part of x

    if x == 0:
    return h0, k0

    while True:
    x = 1 / x
    a = floor(x)
    x = x - a

    h2 = a * h0 + h1
    k2 = a * k0 + k1

    if k2 > max_denominator:
    break



    approx = h2 / k2
    actual = (h0 + h1 / k1) / (k0 + k1 / k1)
    #try:
    # actual = (h0 + h1 / k1) / (k0 + k1 / k1)
    #except Exception as e:
    # actual = (h0 + h1 / (k1+1)) / (k0 + k1 / (k1+1))
    error = abs(approx - actual)
    if error < tolerance:
    break

    h1, k1 = h0, k0
    h0, k0 = h2, k2-1

    if x == 0 or abs(x) < tolerance:
    break

    print(h0, k0)
    return h0, k0

    assert continued_fraction(0.5) == (1,2)
    assert continued_fraction(0.3333) == (1,3)
    assert continued_fraction(0.666666) == (2,3)