[PD] converting float to int

Mathieu Bouchard matju at sympatico.ca
Fri Aug 16 00:16:01 CEST 2002

On Thu, 15 Aug 2002, Carlo E. Prelz wrote:
> Maybe I did not get your question right. This is very elementary C.
> If you want to just truncate the decimal part of a floating point
> variable you just cast it to int:
> int firstint=(int)firstfloat;
> int secondint=(int)secondfloat;

> If you want to have the nearest integer, this is what you need
> int firstint=(int)(firstfloat+0.5);
> int secondint=(int)(secondfloat+0.5);

This does not round to the nearest, because for the +0.5 trick to work,
the rounding that occurs after must be downwards, like floor(), instead of
towards zero, like a (int) cast.

(This is very elementary C too ;-)

Mathieu Bouchard                       http://artengine.ca/matju

More information about the Pd-list mailing list