Hey all,
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!!!
See attached patch.
Have I lost my mind? Is my machine going crazy? or is expr actually doing what I see here?
I cringe at the idea of doing these calculations with separate [*] [/] [+] [-] objects...
Thanks, B. Bogart
#N canvas 0 0 450 300 10; #X obj 74 90 expr 8 / 20; #X obj 142 24 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1; #X floatatom 93 128 5 0 0 0 - - -; #X floatatom 213 133 5 0 0 0 - - -; #X msg 191 47 1; #X obj 194 95 expr $f1 * 8 / 20; #X connect 0 0 2 0; #X connect 1 0 0 0; #X connect 1 0 4 0; #X connect 4 0 5 0; #X connect 5 0 3 0;
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
Mathieu, are you really able to put 8.0 in an expr argument? In my PD (.39 ubuntu package) the 8.0 gets turned into 8 and remains an int.
Hmm?
B. Bogart
Mathieu Bouchard wrote:
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
PD-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Ah! [expr float(8) / 6] does the trick, I'm damn happy there is float() in there!!!
Thanks all,
B. Bogart
B. Bogart wrote:
Mathieu, are you really able to put 8.0 in an expr argument? In my PD (.39 ubuntu package) the 8.0 gets turned into 8 and remains an int.
Hmm?
B. Bogart
Mathieu Bouchard wrote:
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
PD-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
PD-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
On Mon, 23 Jul 2007, B. Bogart wrote:
Mathieu, are you really able to put 8.0 in an expr argument? In my PD (.39 ubuntu package) the 8.0 gets turned into 8 and remains an int.
Ah! [expr float(8) / 6] does the trick, I'm damn happy there is float() in there!!!
if you write any character next to the number that causes pd to read it as a symbol, then pd won't rewrite it before expr reads it.
[expr (8.0) / (6.0)] = 1.25 [expr 8.0 / 6.0] = 1 [expr 8.0 /6.0] = 1.25 [expr 8.0/ 6.0] = 1.25 [expr 8.0/6.0] = 1.25
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
Ah, I see now.
I could not use that method, since 8/6 was an example, and I wanted to do $8 / $6, so treating "$8" as a symbol would not have helped.
.b.
Mathieu Bouchard wrote:
On Mon, 23 Jul 2007, B. Bogart wrote:
Mathieu, are you really able to put 8.0 in an expr argument? In my PD (.39 ubuntu package) the 8.0 gets turned into 8 and remains an int.
Ah! [expr float(8) / 6] does the trick, I'm damn happy there is float() in there!!!
if you write any character next to the number that causes pd to read it as a symbol, then pd won't rewrite it before expr reads it.
[expr (8.0) / (6.0)] = 1.25 [expr 8.0 / 6.0] = 1 [expr 8.0 /6.0] = 1.25 [expr 8.0/ 6.0] = 1.25 [expr 8.0/6.0] = 1.25
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
Hallo, B. Bogart hat gesagt: // B. Bogart wrote:
Ah, I see now.
I could not use that method, since 8/6 was an example, and I wanted to do $8 / $6, so treating "$8" as a symbol would not have helped.
It does work, unless you're using an old, pre-0.40 version of Pd.
Frank Barknecht _ ______footils.org_ __goto10.org__
Hallo, Frank Barknecht hat gesagt: // Frank Barknecht wrote:
Hallo, B. Bogart hat gesagt: // B. Bogart wrote:
Ah, I see now.
I could not use that method, since 8/6 was an example, and I wanted to do $8 / $6, so treating "$8" as a symbol would not have helped.
It does work, unless you're using an old, pre-0.40 version of Pd.
Ah, sorry, I should have tested it: [expr $1/$2] doesn't work. :(
Frank Barknecht _ ______footils.org_ __goto10.org__
On Wed, 25 Jul 2007, Frank Barknecht wrote:
Frank Barknecht hat gesagt: // Frank Barknecht wrote:
B. Bogart hat gesagt: // B. Bogart wrote: It does work, unless you're using an old, pre-0.40 version of Pd.
Ah, sorry, I should have tested it: [expr $1/$2] doesn't work. :(
Huh? [expr $1/$2] works here on Miller's 0.40.
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
Hallo, Mathieu Bouchard hat gesagt: // Mathieu Bouchard wrote:
On Wed, 25 Jul 2007, Frank Barknecht wrote:
Frank Barknecht hat gesagt: // Frank Barknecht wrote:
B. Bogart hat gesagt: // B. Bogart wrote: It does work, unless you're using an old, pre-0.40 version of Pd.
Ah, sorry, I should have tested it: [expr $1/$2] doesn't work. :(
Huh? [expr $1/$2] works here on Miller's 0.40.
I mean, it doesn't create all the time, see attached.
Frank Barknecht _ ______footils.org_ __goto10.org__
On Wed, 25 Jul 2007, Frank Barknecht wrote:
Mathieu Bouchard hat gesagt: // Mathieu Bouchard wrote:
Huh? [expr $1/$2] works here on Miller's 0.40.
I mean, it doesn't create all the time, see attached.
It won't expand $1/$2 to 0/0 or anything else that [expr] could make sense of, because it's a DOLLSYM. If you want 0.0/0.0 you need to use spaces because then each becomes a DOLLAR. That is something that I explain in a previous mail of this thread.
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada
On Tue, 24 Jul 2007, B. Bogart wrote:
Ah, I see now. I could not use that method, since 8/6 was an example, and I wanted to do $8 / $6, so treating "$8" as a symbol would not have helped.
Ah, caution with that: literals like 8 6 8.0 6.0 and pd's substitutors ($1 $2 $3) work in the same way in this case, but [expr]'s own substitutors ($f1 $f2 $f3) don't have that int-vs-float confusion problem because then [expr] does not attempt to read those floats as being text.
_ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - tél:+1.514.383.3801, Montréal QC Canada