[chuck-users] Conversion float to int using SndBuf

Jean Menezes da Rocha jean at menezesdarocha.info
Tue Jan 21 10:58:36 EST 2020


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 <ermina at studioplume.com> wrote:

> 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 at lists.cs.princeton.edu
> > https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
> >
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>


-- 
Jean Menezes da Rocha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20200121/b08c368e/attachment.html>


More information about the chuck-users mailing list