Conversion float to int using SndBuf
Hi What is the best way to convert a float to int in the following please? //I want to set myBuf.pos to a percentage of myBuf.samples()... //Start playing at say 40% through the total number of samples. 40.0 => float pctPosInit; pctPosInit/100*myBuf.samples() => myBuf.pos; This throws the error argument types don't match. I'm not seeing how to convert the float to an int here. Thanks for your help! Micheal
Hello Mícheál, The ChucK language has a cast operator '$' which might come in handy in this situation (pctPosInit/100*myBuf.samples()) $ int => myBuf.pos; Cheers, michael
On Jan 21, 2020, at 7:08 AM, Mícheál Ó Catháin
wrote: Hi What is the best way to convert a float to int in the following please?
//I want to set myBuf.pos to a percentage of myBuf.samples()... //Start playing at say 40% through the total number of samples.
40.0 => float pctPosInit; pctPosInit/100*myBuf.samples() => myBuf.pos;
This throws the error argument types don't match. I'm not seeing how to convert the float to an int here. Thanks for your help!
Micheal _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Hi, to convert a float to an int, you need to explicitly cast the value with 4.8 $ int => int foo; // foo == 4 (as written here: https://chuck.cs.princeton.edu/doc/language/oper.html) So you would do something like pctPosInit $ int => int pctPosInitInt pctPosInitInt/100*myBuf.samples() => myBuf.pos; . e On 01/21/2020 02:08 PM, Mícheál Ó Catháin wrote:
Hi What is the best way to convert a float to int in the following please?
//I want to set myBuf.pos to a percentage of myBuf.samples()... //Start playing at say 40% through the total number of samples.
40.0 => float pctPosInit; pctPosInit/100*myBuf.samples() => myBuf.pos;
This throws the error argument types don't match. I'm not seeing how to convert the float to an int here. Thanks for your help!
Micheal
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
I would suggest that you call Math.round() before casting, as to prevent
undesirable effects: notice that in the example sent by ermina, 4.8 rounds
down to 4 when cast to int, which could cause miscalculations *depending on
your use case*.
The previous example would look something like this if we used Math.round():
Math.round(pctPosInit) $ int => int pctPosInitInt;
pctPosInitInt/100*myBuf.samples() => myBuf.pos;
Best regards!
On Tue, Jan 21, 2020 at 12:49 PM ermina
Hi,
to convert a float to an int, you need to explicitly cast the value with 4.8 $ int => int foo; // foo == 4 (as written here: https://chuck.cs.princeton.edu/doc/language/oper.html)
So you would do something like pctPosInit $ int => int pctPosInitInt pctPosInitInt/100*myBuf.samples() => myBuf.pos;
. e On 01/21/2020 02:08 PM, Mícheál Ó Catháin wrote:
Hi What is the best way to convert a float to int in the following please?
//I want to set myBuf.pos to a percentage of myBuf.samples()... //Start playing at say 40% through the total number of samples.
40.0 => float pctPosInit; pctPosInit/100*myBuf.samples() => myBuf.pos;
This throws the error argument types don't match. I'm not seeing how to convert the float to an int here. Thanks for your help!
Micheal
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
-- Jean Menezes da Rocha
participants (4)
-
ermina
-
Jean Menezes da Rocha
-
Michael Heuer
-
Mícheál Ó Catháin