Author |
Message |
Robot
Joined: Aug 18, 2007 Posts: 2 Location: Brooklyn
|
Posted: Mon Sep 10, 2007 5:48 pm Post subject:
float to int? |
|
|
wAZ UP ChucKers?
1st off, Im lovin me some chuck. It so good for makin justly tuned scales. It also so good for makin joystick instruments. It just so good.
But I am stuck on one thing which I think should be easy.
How can I turn a float into an integer? either rounding or truncating, I don't care?
Basically, my joystick axis gives me a float range of -1.0 through 1.0. I want to use it's postion to index an array of frequencies to use as my instrument frequency. ie, I want it to convert -1.0 - 1.0 into 0-7
I have this formula that gives me 0.0-7.0 rounded, which as it turns out is not good enough.
Math.round(((e.Throttle + 1.0)/2.0 * 7.0)) => float note;
and then doing this:
scale[note] => m.freq;
gets me a big phat:
array index 1 must be of type 'int' or 'string', not 'float'
I have tried doing stuff I thought would work like using integer math like:
((e.Throttle+1.0)*7)/2
but to no avail.
What is the obvious thing I am missing here?
Thanks for all the rad chuck'n, and Ill post some stuff I have been makin at some point. |
|
Back to top
|
|
|
moudi
Joined: Oct 07, 2006 Posts: 63 Location: Bern Switzerland
|
Posted: Tue Sep 11, 2007 12:26 am Post subject:
|
|
|
Hi Robot and welcome in the world of chuck.
It seems that you're looking for a possibility to cast float to int.
Chuck automatically (implicit) casts int to float, but since
you lose information casting from float to int you need an
explicit cast:
Code: | 4.8 $ int => int foo; |
enjoy chucking
/moudi |
|
Back to top
|
|
|
Robot
Joined: Aug 18, 2007 Posts: 2 Location: Brooklyn
|
Posted: Tue Sep 11, 2007 4:32 am Post subject:
|
|
|
Heck yea, that works! That is the 1st I have heard of explicit type casting in ChucK. THanks so much.
Now I can use the throttle axis of my joystick to set a pitch in a seven note scale, brilliant! |
|
Back to top
|
|
|
robin746
Joined: Sep 02, 2007 Posts: 26 Location: Eire
|
Posted: Wed Sep 19, 2007 5:02 am Post subject:
|
|
|
Robot wrote: | Now I can use the throttle axis of my joystick to set a pitch in a seven note scale, brilliant! |
That's the sort of sentence I like to read. |
|
Back to top
|
|
|
|