[CM] translating spirals into music CLM

Bill Schottstaedt bil@ccrma.Stanford.EDU
Wed, 11 Jul 2007 15:56:03 -0700


Being of unsound mind (but the rest of me is ok), I once wrote a
program to generate ideal comma ratios out to 2000 digits.
Whenever I need a laugh, I run this program -- sort of like 
a box of chocolates (you need gmp):

#include <stdio.h>
#include <gmp.h>

int main (int argc,  char **argv)
{
  MP_INT tt, a1, b1, a2, b2, a, b, tmp1, tmp2, tmp3, num, den, lscl, res;
  MP_RAT x, xtmp, xxtmp;
  int happy;
  FILE *fp;
  fp = fopen("data1", "w");
  mpz_init_set_str(&tt, "1", 10);
  mpz_init_set_str(&a1, "0", 10);
  mpz_init_set_str(&res, "0", 10);
  mpz_init_set_str(&b2, "0", 10);
  mpz_init_set_str(&a2, "1", 10);
  mpz_init_set_str(&b1, "1", 10);
  mpz_init_set_str(&a, "0", 10);
  mpz_init_set_str(&b, "0", 10);
  mpz_init_set_str(&tmp1, "0", 10);
  mpz_init_set_str(&tmp2, "0", 10);
  mpz_init_set_str(&tmp3, "0", 10);
#if 0
  mpz_init_set_str(&num, "6931471805599453094172321", 10);
  mpz_init_set_str(&den, "4054651081081643819780131", 10);
#endif

  mpz_init_set_str(&num,
		   
"69314718055994530941723212145817656807550013436025525412068000949339362196969
471560586332699641868754200148102057068573368552023575813055703267075163507596
193072757082837143519030703862389167347112335011536449795523912047517268157493
206515552473413952588295045300709532636664265410423915781495204374043038550080
194417064167151864471283996817178454695702627163106454615025720740248163777338
963855069526066834113727387372292895649354702576265209885969320196505855476470
330679365443254763274495125040606943814710468994650622016772042452452961268794
654619316517468139267250410380254625965686914419287160829380317271436778265487
756648508567407764845146443994046142260319309673540257444607030809608504748663
852313818167675143866747664789088143714198549423151997354880375165861275352916
610007105355824987941472950929311389715599820565439287170007218085761025236889
213244971389320378439353088774825970171559107088236836275898425891853530243634
214367061189236789192372314672321720534016492568727477823445353476481149418642
386776774406069562657379600867076257199184734022651462837904883062033061144630
073719489002743643965002580936519443041191150608094879306786515887090060520346
842973619384128965255653968602219412292420757432175748909770675268711581705113
700915894266547859596489065305846025866838294002283300538207400567705304678700
184162404418833232798386349001563121889560650553151272199398332030751408426091
479001265168243443893572472788205486271552741877243002489794540196187233980860
831664811490930667519339312890431641370681397776498176974868903887789991296503
619270710889264105230924783917373501229842420499568935992206602204654941510613
918788574424557751020683703086661948089641218680779020818158858000168811597305
618667619918739520076671921459223672060253959543654165531129517598994005600036
651356756905124592682574394648316833262490180382424082423145230614096380570070
255138770268178516306902551370323405380214501901537402950994226299577964742713
815736380172987394070424217997226696297993931270694", 10);

  mpz_init_set_str(&den, 
"10986122886681096913952452369225257046474905578227494517346943336374942932186
089668736157548137320887879700290659578657423680042259305198210528018707672774
106031627691833813671793736988443609599037425703167959115211455919177506713470
549401667755802222031702529468975606901065215056428681380363173732985777823669
916547921318181490200301038236301222486527481982259910974524908964580534670088
459650857484441190188570876474948670796130858294116021661211840014098255143919
487688936798494302255731535329685345295251459213876494685932562794416556941578
272310355168866102118469890439943063138255285736466882824988136822800634143910
786893251456437510204451627561934973982116941585740535361758900975122233797736
969687754354795135712982177017581242122351405810163272465588937249564919185242
960796684234647069377237252655082032078333928055892853146873095132606458309184
397496822230325765467533311823019649275257599132217851353390237482964339502546
074245824934666866121881436526565429542767610505477795422933973323401173743193
974579847018559548494059478353943841010602930762292228131207489306344534025277
732685627148001681871547243978207187803444678021617815841904282007672124325573
801436417887682616104101681872424068790890992987420815218323752894275273253407
100283575069506240396546275224430846258845085978625308322477453888506800348832
434049008399005808094356528212237038870203680454860077621424408869725941358436
599922621173967080495095279271436315464044462308915818536711960837030485352090
967262958241504035599512135545033224174847410033198148783245256933470494993730
165633666099190395712282284488167431215062856999387403881901274483956479103477
288597211985064942279698579166995641855126504150219155471966585692972660652357
329373683002783092177660538703046200766158494670022601175679751800393479176327
784493514263496836003755785716070049818151918437343829093474666045775065927367
012111537058249647984793040420582396475385785096062609338991470612013024310826
0518262958640076003059494321166880446106134684533980", 10);

  mpz_sub(&den, &den, &num);
  mpz_init_set_str(&lscl, "17312340490667563", 10); /* (/ 1.0 (log (expt 2.0 (/ 1.0 1200)))) */
  mpq_init(&x);
  mpq_init(&xtmp);
  mpq_init(&xxtmp);
  mpq_set_num(&x, &num);
  mpq_set_den(&x, &den);
  happy = 1;
  while (happy)
    {
      mpz_mul(&tmp1, &a1, &tt);
      mpz_add(&a, &tmp1, &a2);
      mpz_mul(&tmp1, &b1, &tt);
      mpz_add(&b, &tmp1, &b2);
      mpz_mul(&tmp2, &den, &b);
      mpz_mul(&tmp1, &num, &a);
      mpz_sub(&tmp3, &tmp1, &tmp2);
      mpz_abs(&tmp1, &tmp3);
#if 0
      mpz_mul(&res, &tmp1, &lscl);
      /* printf("%s %s %s\n", mpz_get_str(NULL, 10, &res), mpz_get_str(NULL, 10, &b), mpz_get_str
(NULL, 10, &a)); */
      fprintf(fp, "%s %s %s\n", mpz_get_str(NULL, 10, &res), mpz_get_str(NULL, 10, &b), mpz_get_str
(NULL, 10, &a)); 
#endif
      fprintf(fp, "%s %s\n", mpz_get_str(NULL, 10, &b), mpz_get_str(NULL, 10, &a)); 

      fflush(fp);
      mpq_set_num(&xtmp, &tt);
      mpq_sub(&xxtmp, &x, &xtmp);
      mpq_inv(&x, &xxtmp);
      mpq_get_num(&tmp1, &x);
      mpq_get_den(&tmp2, &x);
      mpz_div(&tt, &tmp1, &tmp2);
      mpz_set(&a2, &a1);
      mpz_set(&b2, &b1);
      mpz_set(&a1, &a);
      mpz_set(&b1, &b);
      happy = mpz_cmp(&num, &b);
    }
  fclose(fp);
}

the printout starts:

1 1
2 1
5 3
12 7
41 24
53 31
306 179
665 389
15601 9126
31867 18641
79335 46408
111202 65049
190537 111457
10590737 6195184
10781274 6306641
53715833 31421748
171928773 100571885
225644606 131993633
397573379 232565518
6189245291 3620476403
6586818670 3853041921
65470613321 38297853692
137528045312 80448749305
753110839881 440541600217

which should be obvious to a microtuning devotee.