# Visual Basic > Visual Basic 6 and Earlier >  Help To Find One Common Formula

## mms_

In my music notation program, I need to space different note values differently.

I can hard-code any spacing ratio I choose, but I would like to have one common formula to cover the following 3 spacing ratios,
(or theoretically any spacing ratio in-between).


```
spacing ratio = 2 (each note value takes 2 times as much space as the prev value)
1/16   0.0625 x 64    =  4      
1/8    0.125  x 64    =  8      ( 4 x 2 = 8)
1/4    0.25   x 64    =  16     ( 8 x 2 = 16)
1/2    0.5    x 64    =  32     (16 x 2 = 32)
1      1      x 64    =  64     (32 x 2 = 64)

spacing ratio = 1.5 (each note value takes 1.5 times as much space as the prev value)
1/16   0.0625 x 64    =  4
1/8    0.125  x 48    =  6      ( 4   x 1.5 =  6   )
1/4    0.25   x 36    =  9      ( 6   x 1.5 =  9   )
1/2    0.5    x 27    =  13.5   ( 9   x 1.5 = 13.5 )  
1      1      x 20.25 =  20.25  (13.5 x 1.5 = 20.25)  

spacing ratio = 1 (each note value takes an equal value)
1/16   0.0625 x 64/1  =  4
1/8    0.125  x 64/2  =  4      ( 4   x 1 = 4 )
1/4    0.25   x 64/4  =  4      ( 4   x 1 = 4 )
1/2    0.5    x 64/8  =  4      ( 4   x 1 = 4 )  
1      1      x 64/16 =  4      ( 4   x 1 = 4 )
```

I'm seeing a pattern for sure, but cannot come up with a nice simple formula.

Can anyone help?

----------


## OptionBase1

4 * SpacingRatio^(log(16*LeftFraction)/log(2))

----------


## mms_

*OptionBase1*
WOW!    :Smilie: 

I would have *never* come up with that solution.
I was thinking it was a simple linear equation in the form of *y=mx+b*

Thank you again!

Edit:
Just so I can learn... is there some sort of methodology in coming up with that?

----------


## VanGoghGaming

I've got no idea how he got to this final formula by it's pretty easy to deconstruct and reach a simpler formula. MSDN says that "Log" returns the natural logarithm of a number and then:

_The natural logarithm is the logarithm to the base e. The constant e is approximately 2.718282.

You can calculate base-n logarithms for any number x by dividing the natural logarithm of x by the natural logarithm of n as follows:

Logn(x) = Log(x) / Log(n)_

So that explains the logarithm division in the formula, it just returns the base 2 logarithm (Log2(x)):

4 * SpacingRatio^(log(16*LeftFraction)/log(2)) = 4 * SpacingRatio^(log2(16*LeftFraction)) = 4 * SpacingRatio^(log2(16) + log2(LeftFraction)) = 4 * SpacingRatio^(4 + log2(LeftFraction)) = 
4 * SpacingRatio^4 * SpacingRatio^log2(LeftFraction) = 4 * SpacingRatio^(4 - PowersOfTwoExponentsFromLeftFraction)

So the final formula would be faster to calculate (if speed is a concern): 4 * SpacingRatio^(4 - PowersOfTwoExponentsFromLeftFraction)

PowersOfTwoExponentsFromLeftFraction (for lack of a better word), given your samples are:

1/16 -> 4
1/8 -> 3
1/4 -> 2
1/2 -> 1
1/1 -> 0

So instead of supplying fractions as entry data, you could supply integers:

spacing ratio = 2

4  ->  4 * SpacingRatio^(4-4)   =  4
3  ->  4 * SpacingRatio^(4-3)   =  8
2  ->  4 * SpacingRatio^(4-2)   =  16
1  ->  4 * SpacingRatio^(4-1)   =  32
0  ->  4 * SpacingRatio^(4-0)   =  64

spacing ratio = 1.5

4  ->  4 * SpacingRatio^(4-4)   =  4
3  ->  4 * SpacingRatio^(4-3)   =  6
2  ->  4 * SpacingRatio^(4-2)   =  9
1  ->  4 * SpacingRatio^(4-1)   =  13.5
0  ->  4 * SpacingRatio^(4-0)   =  20.25

spacing ratio = 1

4  ->  4 * SpacingRatio^(4-4)   =  4
3  ->  4 * SpacingRatio^(4-3)   =  4
2  ->  4 * SpacingRatio^(4-2)   =  4
1  ->  4 * SpacingRatio^(4-1)   =  4
0  ->  4 * SpacingRatio^(4-0)   =  4

----------


## OptionBase1

> I've got no idea how he got to this final formula by it's pretty easy to deconstruct and reach a simpler formula. MSDN says that "Log" returns the natural logarithm of a number and then:
> 
> _The natural logarithm is the logarithm to the base e. The constant e is approximately 2.718282.
> 
> You can calculate base-n logarithms for any number x by dividing the natural logarithm of x by the natural logarithm of n as follows:
> 
> Logn(x) = Log(x) / Log(n)_
> 
> So that explains the logarithm division in the formula, it just returns the base 2 logarithm (Log2(x)):
> ...


How is that simpler?  Provide the math for PowersOfTwoExponentsFromLeftFracton, simplify, and it will look like what I gave.

If we're going to nitpick CPU speed, then I could have said:

4 * SpacingRatio^(4 + log(LeftFraction)/log(2))

And that would replace a multiplication by 16 with an addition by 4 (because log(a*b) = log(a) + log(b)), which I suppose might result in a non-zero (but essentially zero) change in calculation time.

----------


## VanGoghGaming

Yeah I have edited my post to suggest using integers instead of fractions for parameters to this function.

----------


## OptionBase1

> Yeah I have edited my post to suggest using integers instead of fractions for parameters to this function.


So the OP needs to change their input, and you still haven't provided the math formula for PowersOfTwoExponentsFromLeftFraction, just the a->b values.  Not trying to argue, but I don't see a simpler formula.

----------


## OptionBase1

> 1/16 -> 4
> 1/8 -> 3
> 1/4 -> 2
> 1/2 -> 1
> 1/1 -> 0


I'll save you the math work.  The PowersOfTwoExponentsFromLeftFraction formula to go from the Left value to the Right value is (drum roll):

-log(LeftFraction)/log(2)

So, let's plug that into the rest of your equation:

4 * SpacingRatio^(4 - PowersOfTwoExponentsFromLeftFraction)
= 4 * SpacingRatio^(4 - (-log(LeftFraction)/log(2)) ' (substitution)
= 4 * SpacingRatio^(4 + log(LeftFraction)/log(2)) ' (simplification, and is what I posted in post 5)
= 4 * SpacingRatio^(log(16)/log(2) + log(LeftFraction)/log(2)) ' (substitution)
= 4 * SpacingRatio^((log(16) + log(LeftFraction))/log(2)) ' (combining like terms)
= 4 * SpacingRatio^(log(16*LeftFraction)/log(2)) ' (sums of logs identity, and my original formula)

----------

