programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Decimal Powers.. simple question

Ranch Hand
Posts: 241
• Number of slices to send:
Optional 'thank-you' note:
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.

author and iconoclast
Posts: 24207
46
• Number of slices to send:
Optional 'thank-you' note:
The static method java.lang.Math.pow() is the Java Way to raise a number to a power.

High Plains Drifter
Posts: 7289
• Number of slices to send:
Optional 'thank-you' note:

The multiplication part you can add as-is.

Daniel Prene
Ranch Hand
Posts: 241
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 24207
46
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Awesome! Thanks!

Daniel Prene
Ranch Hand
Posts: 241
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 3061
• Number of slices to send:
Optional 'thank-you' note:

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 ]

(instanceof Sidekick)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
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.

 Quick! Before anybody notices! Cover it up with this tiny ad: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com