Page 1 of 1
how to cause Iterations
Posted: Mon Jun 28, 2010 8:03 pm
by Equiarch
Equiarch wrote:Thanks Bernd
for correcting my terrible spelling nevertheless the code does not work so I am trying a new angle:
Code: Select all
put 0 into n
on mouseup
put field "userNum" into f
if f>=1*2^n then
put "The" && n && Octave into field result
else
<---- This is where I am stuck------->
end mouseup
what I want to happen is that variable "n" is increased by 1 and then the "if" statement runs again with the new value for "n" until
f>= 1*2^n and the result is displayed in field "result" as for example "
The 3 Octave"
Peace
Mario
bn wrote:Hi Mario,
put 0 into n
on mouseup
put field "userNum" into f
if f>=1*2^n then
put "The" && n && Octave into field result
else
<---- This is where I am stuck------->
end mouseup
if I see this correctly then in the first round
1*2^n when n is 0 this evaluates to 1
is that what you want?
could you give some example numbers for f? What is the value range of f, are those integers?
And post the whole script for the if-then part?
regards
Bernd
PS. maybe you would want to start a new topic if the problem is different from the original post.
Hey Bernd
Yes, I want the first iteration to evaluate to 1. The value range of f is any intger from 1 to infiniti. For example 1 or 261.6 are both possible values entered by the user.
As far as the if-then scipt is concerned, i have not completed it as I don't know how. but I will attempt to do so if it will help you to help me.
Code: Select all
put 0 into n
on mouseup
put field "userNum" into f
if f>=1*2^n then
put "The" && n && Octave into field result
else
put n+1 into n and repeat from line 4 until f>=1*2^n
end mouseup
I hope this makes thing at least somewhat more clear.
Thanks,
Mario
Re: how to cause Iterations
Posted: Mon Jun 28, 2010 9:11 pm
by bn
Hi Mario,
I would like to help you but unfortunately I don't understand what you want. Neither from your code nor from your explanation.
Try this:
let alone the code, just tell me the story of the problem you are trying to solve. What is the algorithm / formula. In what context.
Please explain to me how an octave is calculated. I have no idea. What is your aim.
If you explain it to someone who does not know anything about what you already know, that also may help you to find a way to some code.
If I don't understand what your want I can not help you. Please explain.
regards
Bernd
Re: how to cause Iterations
Posted: Tue Jun 29, 2010 2:43 am
by Equiarch
OK Bernd
Here we go! I am trying to build a Octave calculator where a user can enter a frequency value, such as the frequency of middle C on a piano (261.6 hz) Hertz being the measure of cycles per second in other words 1cycle in one second equals 1hz.
Once the user clicks the calculate Button the app calculates what Octave the entered frequency value belongs to. Octaves start at the 1st and continue, 2nd 3rd 4th and so on into infinity; 1hz-2hz belonging to the 1st octave 2hz-4hz belonging to the second 4hz-8hz belonging to the 3rd and so on as the frequency doubles the octave increases by 1. I thought that a good way to do this calculation would to compare the entered number to a counter that started at the 1st octave and if the enetered number was greater than the highest frequency within that octave the counter would goto the next octive and the entered number would be compared the highest frequency in that octave and so on until the entered frequency was machtched to the octave that it belongs to and then that octave number would be displayed. I use 1*2^n where in the first iteration n=0 giving a product of one as in 1st octave. n would then rise to 1 in the second iteration with a product of 2 as in the 2nd octave and so on until the entered frquency was less than 1*2^n and the value of n would be displayed as the octave of the entered frequency.
Thus the user enters 256 and clicks the button "The 9th Octave" is displayed. 9, being the final value of n.
I hope this better explains my intentions for my app.
Thanks,
Mario
Re: how to cause Iterations
Posted: Tue Jun 29, 2010 11:21 am
by Equiarch
I solved part of my problem by eleminating the need for iterations. I can just use the log2 operator to do the needed calculation:
Code: Select all
on mouseUp
put field "Entry1" into f
put log2(f) into c
if c is an integer then
put c into field "result"
else
put first char of c into e <---doesn't work if the number is more than 9 as the higher digits are left out because of the statment--->
put e+1 into field "result"
end if
end mouseUp
How do I round up to the next integer, even if the number is 1.1 or 5.02, I want them to round up to 2 and 6 respectively?
Also I would like to add the suffixes "st", "nd", "rd" and "th" as in 1st 2nd 3rd and 4th to the result so that what is grammatically correct when read. The problem here is that the app has to differintiate numbers ending with 1, 2 or 3 from all other numbers and numbers ending with 11, 12, and 13 must be grouped with the other numbers that don't end with 1, 2, or 3.
hope some can help
Peace,
Mario
Re: how to cause Iterations
Posted: Tue Jun 29, 2010 12:41 pm
by bn
Hi Mario,
glad you found a solution.
for the rounding up regardless of the fractional part of the number:
Code: Select all
on mouseUp
put field "Entry1" into f
put log2(f) into c
put offset(".",c) into tPositionOfPeriod
if tPositionOfPeriod > 0 then
put char 1 to tPositionOfPeriod - 1 of c into c
add 1 to c
end if
put c into field "result"
end mouseUp
You could add a little error checking at the beginning. If f is not a number then ...
What do you do with a number lower then 1? And is 1 supposed the be the beginning of octave 1 or is it octave 0.
Likewise is 2 octave 1 and 2.1 octave 2?
One note: you name your field "result". Be careful, it is better to not use reserved names. It can make it very difficult to track down bugs. If you forget the quotation marks funny things can happen. One way to overcome this would be to prepend you field names with an "f", buttons with "b" etc. Or choose other names alltogether.
regards
Bernd
Re: how to cause Iterations
Posted: Tue Jun 29, 2010 7:36 pm
by Equiarch
Thanks, once again Bernd
You have helped me imensely once again, as usual.
I figured out the suffix issue as well. I guess I am starting to get this, but again not without your help
Thanks again.
Peace and Blessings,
Mario