[PD] Expr does not play well with ints?

Mathieu Bouchard matju at artengine.ca
Mon Jul 23 23:49:03 CEST 2007


On Mon, 23 Jul 2007, B. Bogart wrote:

> So I've been doing lots of calculations with expr and creation
> arguments, and it seems to send an int when it should send a float way
> too often:
> [expr 8 / 20]
> returns 0!!!
> Have I lost my mind? Is my machine going crazy? or is expr actually
> doing what I see here?

expr supports the int type because [expr] comes from jMax/FTS/Max/etc. So 
if you put a number without a dot it's parsed as an int. Then the main 
difference between ints and floats is that an int divided by an int is 
rounded towards zero (many languages do it like that but some others round 
it always downwards instead; some others instead force the result to be a 
float or a rational)

What you can do is either [expr 8 / 20.0] or [expr 8.0 / 20] or obviously 
[expr 8.0 / 20.0]

You only need to make one of the two as float because float has priority 
on int, so when a float comes into contact with an int, the int is 
converted, even though float can't represent everything that int can, e.g. 
compare

   [expr 123456789%10] finds last digit of 123456789
   [expr int(123456789.0)%10] fails because float isn't that precise

do not put a space before % in that first example! if you do, pd will 
parse it as a number, turn it to float, then give it to [expr], which 
determines that float % int = syntax error.

cool, eh?

  _ _ __ ___ _____ ________ _____________ _____________________ ...
| Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada


More information about the Pd-list mailing list