Hi I expected the ++ (and --) operator to work as this: If ++ precedes the variable, e.g. ++i, the value returned is the value in counter after it has been incremented. If ++ follows the variable, e.g. i++, the value returned is the value in counter before it has been incremented. This doesn't seem to be the case (chuck always returns the value after it has been incremented): int i; <<<i>>>; <<>>; <<<++i>>>; [atte@aarhus tests]$ chuck increment.ck 0 :(int) 1 :(int) 2 :(int) Maybe this could be changed? -- peace, love & harmony Atte http://www.atte.dk | quartet: http://www.anagrammer.dk http://www.atte.dk/gps | compositions: http://www.atte.dk/compositions
Hi Atte! The ++ and -- (post and pre) are a bit fishy right now. We plan to fix this. In the meantime, it safest to NOT used them inline with expressions. Thanks for the reporting this and sorry for the inconvenience. Best, Ge!
I expected the ++ (and --) operator to work as this:
If ++ precedes the variable, e.g. ++i, the value returned is the value in counter after it has been incremented. If ++ follows the variable, e.g. i++, the value returned is the value in counter before it has been incremented.
This doesn't seem to be the case (chuck always returns the value after it has been incremented):
int i; <<<i>>>; <<>>; <<<++i>>>;
[atte@aarhus tests]$ chuck increment.ck 0 :(int) 1 :(int) 2 :(int)
Maybe this could be changed?
-- peace, love & harmony Atte
http://www.atte.dk | quartet: http://www.anagrammer.dk http://www.atte.dk/gps | compositions: http://www.atte.dk/compositions _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Hi, Ge!
The ++ and -- (post and pre) are a bit fishy right now. We plan to fix this. In the meantime, it safest to NOT used them inline with expressions.
I wondered about those. Why would you use those instead of "+=>" or "-=>"? I don't realy see why we need those at all, personally I avoid them beause to me using the ChucK operator feels more consistend. Sorry if I'm missing something; clearly this is another thing borowed from C which I don't speak but I'd still like to know WHY we borowed it. Kas.
The advantage of using the ++ and -- operators is that you can change
the value of one variable and then further use it in an expression.
Consider this:
++i +=> x;
This command will increment i, then take the incremented value and add
to x. In many situations, the ++/-- operators lets you write compact
yet readable code. Consider these C statements:
Statement 1:
i=i+1; x=x+i;
Statement 2:
i=i+1; x+=i;
Statement 3:
x+=(i=i+1);
Statement 4:
x+= ++i;
They all do the exact same thing, but at least in my experienced eyes,
I know which one reveals most clearly what is going on.
/nitro2k01
On 6/28/06, Kassen
Hi, Ge!
The ++ and -- (post and pre) are a bit fishy right now. We plan to fix this. In the meantime, it safest to NOT used them inline with expressions.
I wondered about those.
Why would you use those instead of "+=>" or "-=>"? I don't realy see why we need those at all, personally I avoid them beause to me using the ChucK operator feels more consistend.
Sorry if I'm missing something; clearly this is another thing borowed from C which I don't speak but I'd still like to know WHY we borowed it.
Kas. _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
-- -----BEGIN 2ROT13 MESSAGE----- The blog of nitro2k01: http://soundandcomplete.wordpress.com/ Sätt på ett par flipflops, vippa på rumpan och gör det här till en minnesvärd sommar! -----END 2ROT13 MESSAGE-----
On 6/28/06, nitro2k01
They all do the exact same thing, but at least in my experienced eyes, I know which one reveals most clearly what is going on.
That's very clear, thanks for taking the time to explain that, I had only considdered more simple cases. Now I also see why it would be usefull to have both i++ and ++i. Kas.
nitro2k01 wrote:
The advantage of using the ++ and -- operators is that you can change the value of one variable and then further use it in an expression. Consider this: ++i +=> x;
Wow, you like to live on the edge! :) Unless I'm vastly mistaken, the code is compiled into exactly the same assembly commands as i++; x+i=>x; In other words, there's no computational benefit to writing that as a single statement.
In many situations, the ++/-- operators lets you write compact yet readable code.
I encountered this quote a week ago, and it's already become my favorite saying: "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan I'm all for using ++ and --, but only if they're the only thing happening on that line. If anything else is happening, I need to actually think about what's happening, and that's dangerous if you're trying to write maintainable code. :) Cheers, - Graham Percival
On 6/28/06, Graham Percival
In other words, there's no computational benefit to writing that as a single statement.
Well, agreed, but then it could be argued that there is no computational benefit to running ChucK at all *cough*. There are however benefits in using ChucK because it's expressive and I could imagine there are situations where for some people structures like that would be a good solution from that perspective... sometimes. I still like the odd glass of wine with my music making. That being said; out of pure curiocity, would "now++;" compile? Kas.
That being said; out of pure curiocity, would "now++;" compile? Wild guess: No, I think now (as well as other time expressions) are objects and not numbers. Also, such expression would not be very useful, since it's ambiguous depending on sample rate. (Or is the unit of now independent of the sample rate?)
Wow, you like to live on the edge! :) Maybe a bad example, I just wrote down the first thing that crossed my mind. But still that's most probably the way I'd write that piece of code in a real situation.
Unless I'm vastly mistaken, the code is compiled into exactly the same assembly commands as i++; x+i=>x; In other words, there's no computational benefit to writing that as a single statement. I haven't yet looked into the kernel of ChucK, but that's possible. But depending on the CPU architecture (Including both virtual and physical machines) and the effectiveness of the compiler having a statement with a unary operator will make sense from a computational point of view. On the other hand most C compilers have good optimization egines and don't need the statements to be nested by a human brain.
/nitro2k01 -- -----BEGIN 2ROT13 MESSAGE----- The blog of nitro2k01: http://soundandcomplete.wordpress.com/ Sätt på ett par flipflops, vippa på rumpan och gör det här till en minnesvärd sommar! -----END 2ROT13 MESSAGE-----
On 6/28/06, nitro2k01
Wild guess: No, I think now (as well as other time expressions) are objects and not numbers. Also, such expression would not be very useful, since it's ambiguous depending on sample rate. (Or is the unit of now independent of the sample rate?)
I agree it's ambiguous since it's not realy all that clear what we want to add to now (I would asume one sample would seem natural) but forwarding time by one sample is quite usefull in DSP. This looks usefull to me; While(true) { //simple interpolation filter (.5 * dac(last) ) + (.5 * my_signal(last) ) => dac; 1::samp => now; } Admittedly the effect of this will depend on the sample rate and I also admit that in this speciffic case cpu could be saved by using "step" (just taking a simple exaple) but advancing now by one sample even if it will make the code less universal is often usefull. Ok, even in a case like that I don't see any advantage in going "now++;" The situations where you'd use "now++" in a larger expression would probably not be the ones where you'd advance time by one sample. As far as I'm concerned we can forget about "now++". Kas.
participants (5)
-
Atte André Jensen
-
Ge Wang
-
Graham Percival
-
Kassen
-
nitro2k01