(EDIT: In response to grumpy comments, No it isn\'t homework. I am working on pitch detection, taking an array of potential harmonic peaks, and attempt
The C program is fine, apart from the fact that you cannot trust the check on the remainder, as can be seen from computing x*p-q as well:
Iteration #1: 3: 3/1 - delta: 0.141592653589793116, rem: 0.141592653589793116
Iteration #2: 7: 22/7 - delta: -0.008851424871448188, rem: 0.062513305931051878
Iteration #3: 15: 333/106 - delta: 0.008821280518070296, rem: 0.996594406684156776
Iteration #4: 1: 355/113 - delta: -0.000030144353377892, rem: 0.003417231014946418
Iteration #5: 292: 103993/33102 - delta: 0.000019129331725765, rem: 0.634590879621879211
Iteration #6: 1: 104348/33215 - delta: -0.000011015021655680, rem: 0.575818424298580694
Iteration #7: 1: 208341/66317 - delta: 0.000008114310077190, rem: 0.736658567704091524
Iteration #8: 1: 312689/99532 - delta: -0.000002900711592702, rem: 0.357480987585133375
Iteration #9: 2: 833719/265381 - delta: 0.000002312886920208, rem: 0.797351564778957706
Iteration #10: 1: 1146408/364913 - delta: -0.000000587824615650, rem: 0.254151925163927682
Iteration #11: 3: 4272943/1360120 - delta: 0.000000549413016415, rem: 0.934654436927838420
Iteration #12: 1: 5419351/1725033 - delta: -0.000000038411599235, rem: 0.069914142051204637
Iteration #13: 14: 80143857/25510582 - delta: 0.000000011648808140, rem: 0.303257833981669641
Iteration #14: 3: 245850922/78256779 - delta: -0.000000003463355824, rem: 0.297524047014214316
Iteration #15: 3: 817696623/260280919 - delta: 0.000000001280568540, rem: 0.361072861287829440
Iteration #16: 2: 1881244168/598818617 - delta: -0.000000000931322575, rem: 0.769524124392304913
Iteration #17: 1: 2698940791/859099536 - delta: 0.000000000232830644, rem: 0.299504418772708979
Iteration #18: 3: 9978066541/3176117225 - delta: 0.000000000000000000, rem: 0.338848902789946401 ******* 'true' deviation below epsilon threshold