programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Decimal Powers.. simple question

Daniel Prene
Ranch Hand
Posts: 241
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
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

The multiplication part you can add as-is.

Daniel Prene
Ranch Hand
Posts: 241

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
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
Awesome! Thanks!

Daniel Prene
Ranch Hand
Posts: 241
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
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
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.