• Post Reply Bookmark Topic Watch Topic
  • New Topic

Decimal Powers.. simple question  RSS feed

 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question; How would I represent the following in java:

x = 8.1758 * 2 ^ (y/12)

The only part I can't figure out is how to raise a number to a decimal power. I found an algorithm on google, but it only supports ints.

Thank you for atleast reading this,
-D.P.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method java.lang.Math.pow() is the Java Way to raise a number to a power.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The multiplication part you can add as-is.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if fed 69 it will return 416 when on a calculator I get 440... What's going on?

I'm trying to simulate this:
http://www.borg.com/~jglatt/tutr/notefreq.htm
[ September 15, 2005: Message edited by: Daniel Prene ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mixed-type arithmetic is tricky. Doing math with some integers and some doubles means that some of your partial results ( that "aNote/440", for example) are going to be done in integer math, which of course truncates fractional parts. The quickest fix here would be to use doubles for all the constants:



Any operation that uses a double will give a double result, so this works fine with "int midiNote" because that "9." is a double.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thanks!
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do it that way it rounds... I solved that by avoiding ints at all and doing


public double getFrequency(int midiNote) {
double aNote = 440;
double note = (double)midiNote;
return (aNote / 32) * java.lang.Math.pow(2, (note - 9) / 12);
}

thanks again for the help
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daniel Prene:
If I do it that way it rounds... I solved that by avoiding ints at all and doing


public double getFrequency(int midiNote) {
double aNote = 440;
double note = (double)midiNote;
return (aNote / 32) * java.lang.Math.pow(2, (note - 9) / 12);
}

thanks again for the help


I would like to point out that you haven't completly avoided using ints here. Notice that 440, 32, 9, and 12 are all ints. If you want to ensure that these are doubles, you need to add a decimal place: 440.0, 32.0, 9.0, and 12.0.

HTH

Layne
[ September 15, 2005: Message edited by: Layne Lund ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have this running, I'm curious how it's used. Given a MIDI note number (0..127) it returns the frequency? Do you have something that will play a specified frequency? I wonder how it would sound.

There must be a name for such a mathematically derived tuning ... maybe Pathogorean ... he figured out the ratios of the pitches. It sure isn't "tempered" though.

Wendy Carlos (of Switched On Bach fame) does a lot of interesting work on alternate tunings - papers and synthesizers programmed to play them. If you get deep into MIDI and sysex messages I bet you'll find you can re-tune most synths. I heard a neat recording of one that analyzes harmonies in real time and retunes for "perfect" tuning on the fly. Some really good vocal groups (e.g. gospel and barbershop quartets) do this, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!