[chuck-users] Math.trunc

Kassen signal.automatique at gmail.com
Mon Oct 8 06:54:14 EDT 2007


That was a good question, I think sorting where to use "modular synth style"
signal flow and where to us "plain code" is a important part of ChucKing.
Many things can be done both ways but if you do it the "wrong" way you'll
end up with ugly code and high cpu costs.

If there is still a need to round to a multiple of some number in a
style it could be done using something like this;

fun float trunc(float input, float multiple)
  return (input - input%multiple);

which will always round down. If we really need the closest value we can use
something like;

fun float trunc(float input, float multiple)
  if ( input%multiple < ( .5 * multiple)  ) return (input - input%multiple);
  else return (input - input%multiple + multiple);

This would be overkill if we just want a series of tones that hits every
multiple of 400Hz (in that case we'd be much better off with simply
generating that series directly like Ge said) but it's still a valid
question in itself.

Hope that helps.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20071008/2ce4281c/attachment-0001.htm 

More information about the chuck-users mailing list