back to list

help with ratio/vector algorithm

🔗monz <joemonz@yahoo.com>

3/3/2002 7:27:00 PM

i need help with an algorithm.

within the 3-limit, i'm trying to get an Excel
spreadsheet to automatically calculate the exponent
of 2 when it knows the exponent of 3, so that the
resulting ratio is in the usual form where n > d.

my first attempt worked fairly well, but had a
few systematic errors. i'll give it in a format
that will look familiar to programmers:

ratio r = 2^p * 3^q

if q < 0
then p = int((log(3^abs(q)) / log(2)) + 0.5)
else p = (int((log(3^abs(q)) / log(2)) + 1.5)) * -1
end if

but it's not foolproof: sometimes the exponent of 2 is
one less or one more than it should be. i've tried
setting up further nested if-statements, to check if
the absolute value of p is greater than that of q and
adjust accordingly, but there's always an error somewhere.

help!

-monz

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

🔗monz <joemonz@yahoo.com>

3/3/2002 7:33:39 PM

> From: monz <joemonz@yahoo.com>
> To: <tuning-math@yahoogroups.com>
> Sent: Sunday, March 03, 2002 7:27 PM
> Subject: [tuning-math] help with ratio/vector algorithm
>
>
> i need help with an algorithm.
>
> within the 3-limit, i'm trying to get an Excel
> spreadsheet to automatically calculate the exponent
> of 2 when it knows the exponent of 3, so that the
> resulting ratio is in the usual form where n > d.
>
> my first attempt worked fairly well, but had a
> few systematic errors. i'll give it in a format
> that will look familiar to programmers:
>
> ratio r = 2^p * 3^q
>
> if q < 0
> then p = int((log(3^abs(q)) / log(2)) + 0.5)
> else p = (int((log(3^abs(q)) / log(2)) + 1.5)) * -1
> end if
>
> but it's not foolproof: sometimes the exponent of 2 is
> one less or one more than it should be. i've tried
> setting up further nested if-statements, to check if
> the absolute value of p is greater than that of q and
> adjust accordingly, but there's always an error somewhere.

the errors are occurring because the spreadsheet has to
check to see if n > d, and i can't figure out how to
implement that without getting a circular reference.

-monz

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

🔗graham@microtonal.co.uk

3/4/2002 3:34:00 AM

In-Reply-To: <004101c1c32d$603c6540$af48620c@dsl.att.net>
monz wrote:

> > ratio r = 2^p * 3^q
> >
> > if q < 0
> > then p = int((log(3^abs(q)) / log(2)) + 0.5)
> > else p = (int((log(3^abs(q)) / log(2)) + 1.5)) * -1
> > end if
> >
> > but it's not foolproof: sometimes the exponent of 2 is
> > one less or one more than it should be. i've tried
> > setting up further nested if-statements, to check if
> > the absolute value of p is greater than that of q and
> > adjust accordingly, but there's always an error somewhere.
>
>
> the errors are occurring because the spreadsheet has to
> check to see if n > d, and i can't figure out how to
> implement that without getting a circular reference.

I think =MOD(N20, 1)-N20 does it where N20 is the magnitude of the
original interval in octaves. So you have to substitute that with
log(3^abs(q)) / log(2).

Graham

🔗Robert Walker <robertwalker@ntlworld.com>

3/4/2002 4:49:12 PM

Hi Monz,

It could be your (int)(expression + 0.5)

In this type of situation sometimes one needs
(int)(expression + 1e-12), or (int)(expression - 1e-12).
- replace 1e-12 by whatever precision you have.

It is usually 1e-14 for double precision arithmetic; one could add
a bit extra so say 1e-12 to have plenty of leeway.

That's because you are looking for the first integer above (or below)
rather than the nearest integer. The nearest integer will sometimes
be above, and sometimes be below.

Alternatively, find p, do the calculation, and then at the end
after you have calculated p, in a separate step,
check to see if the ratio is below 1. If so, add 1,
or if it is above 2, subtract 1 at the end.

Robert

🔗paulerlich <paul@stretch-music.com>

3/5/2002 1:12:49 PM

--- In tuning-math@y..., graham@m... wrote:
> In-Reply-To: <004101c1c32d$603c6540$af48620c@d...>
> monz wrote:
>
> > > ratio r = 2^p * 3^q
> > >
> > > if q < 0
> > > then p = int((log(3^abs(q)) / log(2)) + 0.5)
> > > else p = (int((log(3^abs(q)) / log(2)) + 1.5)) * -1
> > > end if
> > >
> > > but it's not foolproof: sometimes the exponent of 2 is
> > > one less or one more than it should be. i've tried
> > > setting up further nested if-statements, to check if
> > > the absolute value of p is greater than that of q and
> > > adjust accordingly, but there's always an error somewhere.
> >
> >
> > the errors are occurring because the spreadsheet has to
> > check to see if n > d, and i can't figure out how to
> > implement that without getting a circular reference.
>
> I think =MOD(N20, 1)-N20 does it where N20 is the magnitude of the
> original interval in octaves. So you have to substitute that with
> log(3^abs(q)) / log(2).
>
>
> Graham

in my periodicity block programs, i have a line that brings all
ratios to the octave between 1/1 and 2/1:

r=r./2.^(floor(log(r)/log(2)));