Raising powers in applescript

Does anyone know if its possible to raise powers in applescript?
I want to find
n
fn = f0 * (a)

eg:
submit the 'n’th interval and the “f0” fundamental frequency in hertz in a display dialog

and be able to return the nth interval in hertz

(3= the third interval of the fundamental (f0=440hz (A3 note)) which = 523.3Hz (C4 note))
3
f3 = 440Hz * (1.059463…) = 523.3 Hz

And also with the code example below, is there a way, to use the “round” command to round the “theMean” off to 2 decimal places? where 103.8934773294 Hz results in 103.89Hz.
Perhaps Python and Pandas would be the pragmatic choice


set A to 0

repeat with this_item in new_list
	set A to A + this_item
end repeat
set theMean to (A / (count new_list))
set rndMean to (round (theMean))

I hope someone cam help, thanks inn advance for your time.

Doug

Doug. There are many people on this forum with excellent math skills, but, unfortunately, I’m not one of them. However, as regards your second question, the round command by definition rounds to an integer. To round to a specified number of decimals, I use Nigel’s script in post 2 of the following thread.

https://macscripter.net/viewtopic.php?pid=192039

For example:

set new_list to {3, 9, 19}

set A to 0
repeat with this_item in new_list
	set A to A + this_item
end repeat
set theMean to (A / (count new_list)) --> 10.333333333333
set rndMean to roundThis(theMean, 2) --> 10.33

on roundThis(n, numDecimals)
	set x to 10 ^ numDecimals
	tell n * x
		if ((it mod 2) ^ 2 > 0.25) then return (it div 0.5 - it div 1) / x
		return it div 1 / x
	end tell
end roundThis

If you can’t round decimals maybe i could do
theMean x 100
round
then divide by 100

to get rndMean with 2 decimal places

oh just seen the replies

Thanks peavine, Nigels response seems to have disappeared

Yeah. Sorry. My idea for calculating the frequency was rubbish. :rolleyes:

Ah. Of course. Semitones. :rolleyes:

set fRoot to 440
set semitones to -9

set frequency to fRoot * (2 ^ (semitones / 12)) -- or fRoot * (1.059463094359 ^ semitones)
set frequency to (round (frequency * 100)) / 100 --> 261.63

Amazing Nigel,

Thank you so much. Just on my way home to give it a whirl. I’ll work some different modes into it then get the myriad tables out for some multiple choice action. Exciting!

Hi Doug.

If it’s for your own use and you want to type in the semitone numbers, the following’s quite simple. It doesn’t include any checks for incompatible input:

set fRoot to text returned of (display dialog "Choose fundemental frequency in Hertz" default answer "")

set dialogReply to text returned of (display dialog "Enter the required semitone offsets,
eg. (commas not necessary):" default answer "3, 4, 5, 7")
set theMode to dialogReply's words -- Good enough if this is for your own use.

set frequencies to {}
repeat with semitones in theMode
	set frequency to fRoot * (1.059463094359 ^ semitones)
	set frequency to (round (frequency * 100)) / 100
	set end of frequencies to frequency -- or to frequency as text.
end repeat
return frequencies -- List of numbers -- or of numeric texts.

You could instead select from a list of preset choices, but this probably isn’t what you want:

set fRoot to text returned of (display dialog "Choose fundemental frequency in Hertz" default answer "")

set presets to {}
repeat with i from -12 to 12
	set end of presets to i
end repeat
set theMode to (choose from list presets with prompt "Select the required semitone offset(s)." with multiple selections allowed)
-- 'choose from list' uniquely returns 'false' when the "Cancel" button's clicked.
if (theMode is false) then error number -128

set frequencies to {}
repeat with semitones in theMode
	set frequency to fRoot * (1.059463094359 ^ semitones)
	set frequency to (round (frequency * 100)) / 100
	set end of frequencies to frequency -- or to frequency as text.
end repeat
return frequencies -- List of numbers -- or of numeric texts.

I don’t understand the question. In a single piece of text like “3, 5, 7, 9”, the ‘words’ are the sections which AppleScript doesn’t think are spaces or punctuation. (There’s actually more to it than that — for instance, “2.4” and “2,4” are also interpreted as ‘words’ — but for text containing just representations of numbers with at least one space between them, it’s good enough.) The ‘words’ of “3, 5, 7, 9” is {“3”, “5”, “7”, “9”} — ie. a ‘list’ object containing those four pieces of text in that order. AppleScript automatically coerces the text values to number values when calculations are applied to them.

Nice man, thanks Fredrik