tags:

views:

393

answers:

3

How can I implement the following function:

alt text

in C#?

Thanks.

+13  A: 
440 * Math.Pow(Math.Pow(2, 1.0/12), n - 49)
Yuriy Faktorovich
+1, strict to point
Rubens Farias
+27  A: 
double F = 440.0 * Math.Pow(2.0, (n-49.0)/12.0);
AraK
+1 for calling Math.Pow only once
ram
You are correct, the equation can be simplified to your answer, but I did a direct implementation.
Yuriy Faktorovich
+1  A: 
440 * 12th root of 2 raised to n-49 
 = 440 * (2 ^ 1/12) ^(n-49)
 = 440 * 2^(n/12) / 2^(49/12)
 = 440 * 2^(n/12) / (2^4 * 2^1/12)
 = 440 * ( 1 / 2^4 ) * 2^((n-1) /12)
 = 8 * 55 * ( 1/16 ) * 2^((n-1) /12)
 = 27.5 * 2^((n-1) /12)

so ....

double d = 27.5 * Math.Pow(2, (n-1) / 12.0)

And since 12th root of 2 = 1.0594630943592952645618252949463, then

double d  = 27.5 * Math.Pow(1.0594630943592952645618252949463, (n-1))

so...

 double d = 27.5 * Math.Pow(1.059463094359295, (n-1));
Charles Bretana
I hope that if this solution is used, a comment is placed for maintainability.
Yuriy Faktorovich
You sure that math is right? Try n = 49. You should get 440.
Eric Lippert
good catch 12th root of 2 = 1.0594630943592952645618252949463, not .083333333333333 ... I ran Windows calc badly... I have edited to correct.
Charles Bretana
You have not edited to correct the problem, which is that your method still does not give 440 when n is 49.
Eric Lippert
Thanks, I Just did... and tested it this time and now I get 440.. I unfortunately use the forum as an editor... last Error was at the end, where I made algebra error. (assumed 2^(a*b) = 2^a * 2^b, actually it's (2^a)^b ...
Charles Bretana
Another thing. You have a 32 digit number there. Doubles are automatically rounded to around 15 digits, and the human ear cannot hear a difference between two tones that differ by so little anyway. You might want to lose about twenty of those digits: writing code that has ludicrous amounts of precision like this fools the reader into believing that such code is meaningful.
Eric Lippert
@Eric, good point... I sort of expected the compiler to sqauwk at me if I put too many there... So when it didn't, I was too lazy to look up the limit for myself... You say it's around 15 digits ? I'll edit to that... ANd I tested with next three lower octaves of C, (n = 37, 25, and 13), and got 220, 110, and 55, respectively, so I 'spect it's right on now...
Charles Bretana