[chuck-users] feeble time printing

Robert Poor rdpoor at gmail.com
Wed Oct 14 20:45:42 EDT 2009


I'm pretty sure there's a bug in the way ChucK prints time.  Or there may be
a bug in the way I think.  Bear with me...

You would expect that two time values that print identically would compare
as equal.  But I can show you a case where t0 and t1 print as "44100.000000"
yet they're not equal.  You'd also expect t1 to have a fractional part of
0.0.  But in this case:

   <<< (t1 % 1::second) >>> prints "1.000000"

and

   <<< t1 - (t1 % 1::second) >>> prints "44099.000000"

Mind you, I'm NOT complaining about roundoff error -- I understand that
converting between floats and time is subject to roundoff -- but it has
certainly cost me a LOT of debugging time discovering that 44100.000000 !=
44100.000000.  It's a printing bug.

Here's the code.  It's longer than it needs to be, so if anyone asks for it,
I'll strip it down to a three line example.

// In ChucK, two times can appear to be equal when printed,
// but compare as not equal, as demonstrated here.

72.511111111 => float tempo;    // intentionally subject to roundoff

now => time t0;            // base time
100.0 => float b0;        // beat at t0
1::second / tempo => dur i0;    // duration of one beat

// convert from system time to beat
fun float time_to_beat(time t) { return b0 + (t - t0)/i0; }

// convert from beat to system time
fun time beat_to_time(float b) { return t0 + (b - b0)::i0; }

// for debugging: extract fractional and whole parts from time
fun dur frac_time(time t) { return t % 1::samp; }
fun time whol_time(time t) { return t - (t % 1::samp); }

fun void test_time(time t0) {
    beat_to_time(time_to_beat(t0)) => time t1;
    if (t0 != t1) {
    <<< "t0:",t0,"is not equal to t1:",t1>>>;
    frac_time(t0) => dur f0;
    frac_time(t1) => dur f1;
    whol_time(t0) => time w0;
    whol_time(t1) => time w1;
    <<< f0, f1, f0==f1, w0, w1, w0==w1 >>>;
    }
}

test_time(now);
test_time(now+1::second);
test_time(now+1::samp);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20091014/daab6f15/attachment.htm>


More information about the chuck-users mailing list