making a duration into a string?
How can I cast a duration into a string (e.g. for passing as a string argument)? Consider: // ============= fun void log(string message) { // do something to log the message } 1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // ============= Same question for time objects. TIA. - Rob
Are you looking for a way to generate something that would ultimately
generate a string that contains "1::second"? Because if that's the
case, you probably can't do it. Durations ultimately get stored as
floats that represent the number of samples in the duration. And they
don't, as far as I know, retain any information about the units that
were used when they were created. So 6::second and 0.1::minute will
both be stored as the number of samples that are contained in a 6
second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d;
d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you
probably want is a duration in terms of time rather than samples, so
you could also get the number of samples per second and divide dSamp
by that number, and then store your duration as a string
representation of a floating point number represented in terms of
seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Sure, can do, but this is only for debugging -- I'd welcome a means to
get the same effect as
<<< "duration is", d >>>;
which obviously works, but not as a string. Same comments apply for
stringifying a time value.
- Rob
On Sat, Jan 9, 2010 at 13:08, Andrew Turley
Are you looking for a way to generate something that would ultimately generate a string that contains "1::second"? Because if that's the case, you probably can't do it. Durations ultimately get stored as floats that represent the number of samples in the duration. And they don't, as far as I know, retain any information about the units that were used when they were created. So 6::second and 0.1::minute will both be stored as the number of samples that are contained in a 6 second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d; d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you probably want is a duration in terms of time rather than samples, so you could also get the number of samples per second and divide dSamp by that number, and then store your duration as a string representation of a floating point number represented in terms of seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
wrote: How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
So are you looking for something like this?
fun void log(string s) {
<<<s>>>;
}
fun string stringify(dur d) {
return "" + ((d / (1::second))) + " seconds";
}
fun string stringify(time t) {
return "" + ((t / (1::second))) + " seconds";
}
1::second => dur d1;
log("d1 is " + stringify(d1)); // "d1 is 1 seconds"
300::samp => dur d2;
log("d2 is " + stringify(d2)); // "d2 is 0.0068 seconds"
now + 60::second => time t1;
log("t1 is " + stringify(t1)); // "t1 is 60 seconds"
now + 1::minute => time t2;
log("t2 is " + stringify(t2)); // "t2 is 60 seconds"
Or are you trying to do something else?
Understand that times are represented as a delta of the number of
samples since the VM (or maybe it's the shred) started executing.
Again, the time itself doesn't carry any information about how it was
created, so these two times would have the same representation if they
were created at the same time:
now + 1::minute => time t1;
now + 60::second => time t2;
And these two times have different representations since they are not
created at the same time:
now + 1::minute => time t1;
5::second => now;
now + 60::second => time t2;
andy
On Sat, Jan 9, 2010 at 1:16 PM, Robert Poor
Sure, can do, but this is only for debugging -- I'd welcome a means to get the same effect as <<< "duration is", d >>>; which obviously works, but not as a string. Same comments apply for stringifying a time value.
- Rob
On Sat, Jan 9, 2010 at 13:08, Andrew Turley
wrote: Are you looking for a way to generate something that would ultimately generate a string that contains "1::second"? Because if that's the case, you probably can't do it. Durations ultimately get stored as floats that represent the number of samples in the duration. And they don't, as far as I know, retain any information about the units that were used when they were created. So 6::second and 0.1::minute will both be stored as the number of samples that are contained in a 6 second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d; d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you probably want is a duration in terms of time rather than samples, so you could also get the number of samples per second and divide dSamp by that number, and then store your duration as a string representation of a floating point number represented in terms of seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
wrote: How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Andrew: Yep, I'd come up with the same thing: dedicated functions to
convert times and durations into strings. That works fine, but my
question was actually simpler, and I apologize for not making it
clear. What I should have said:
"It's clear that there are internal functions for converting times and
durations into strings, used in constructs such as:
<<< now, 1::second >>>;
Are those internal functions available?"
... and I'm guessing the answer is simply "no". Your suggested
solution will do the job just fine (though I'd follow ChucK's lead and
print everything in terms of samples). Thanks!
- Rob
On Sat, Jan 9, 2010 at 14:11, Andrew Turley
So are you looking for something like this?
fun void log(string s) { <<<s>>>; }
fun string stringify(dur d) { return "" + ((d / (1::second))) + " seconds"; }
fun string stringify(time t) { return "" + ((t / (1::second))) + " seconds"; }
1::second => dur d1; log("d1 is " + stringify(d1)); // "d1 is 1 seconds" 300::samp => dur d2; log("d2 is " + stringify(d2)); // "d2 is 0.0068 seconds"
now + 60::second => time t1; log("t1 is " + stringify(t1)); // "t1 is 60 seconds" now + 1::minute => time t2; log("t2 is " + stringify(t2)); // "t2 is 60 seconds"
Or are you trying to do something else?
Understand that times are represented as a delta of the number of samples since the VM (or maybe it's the shred) started executing. Again, the time itself doesn't carry any information about how it was created, so these two times would have the same representation if they were created at the same time:
now + 1::minute => time t1; now + 60::second => time t2;
And these two times have different representations since they are not created at the same time:
now + 1::minute => time t1; 5::second => now; now + 60::second => time t2;
andy
On Sat, Jan 9, 2010 at 1:16 PM, Robert Poor
wrote: Sure, can do, but this is only for debugging -- I'd welcome a means to get the same effect as <<< "duration is", d >>>; which obviously works, but not as a string. Same comments apply for stringifying a time value.
- Rob
On Sat, Jan 9, 2010 at 13:08, Andrew Turley
wrote: Are you looking for a way to generate something that would ultimately generate a string that contains "1::second"? Because if that's the case, you probably can't do it. Durations ultimately get stored as floats that represent the number of samples in the duration. And they don't, as far as I know, retain any information about the units that were used when they were created. So 6::second and 0.1::minute will both be stored as the number of samples that are contained in a 6 second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d; d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you probably want is a duration in terms of time rather than samples, so you could also get the number of samples per second and divide dSamp by that number, and then store your duration as a string representation of a floating point number represented in terms of seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
wrote: How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Sorry, yeah, the short answer is that there doesn't seem to be a good
way to do what you're trying to do.
If you want to see what Chuck does with "<<<" and ">>>", take a look
at Chuck_Instr_Hack::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
in chuck_instr.cpp for the details. There really isn't much in the way
of conversion, it just uses fprintf to print a double.
andy
On Sat, Jan 9, 2010 at 2:27 PM, Robert Poor
Andrew: Yep, I'd come up with the same thing: dedicated functions to convert times and durations into strings. That works fine, but my question was actually simpler, and I apologize for not making it clear. What I should have said:
"It's clear that there are internal functions for converting times and durations into strings, used in constructs such as: <<< now, 1::second >>>; Are those internal functions available?"
... and I'm guessing the answer is simply "no". Your suggested solution will do the job just fine (though I'd follow ChucK's lead and print everything in terms of samples). Thanks!
- Rob
On Sat, Jan 9, 2010 at 14:11, Andrew Turley
wrote: So are you looking for something like this?
fun void log(string s) { <<<s>>>; }
fun string stringify(dur d) { return "" + ((d / (1::second))) + " seconds"; }
fun string stringify(time t) { return "" + ((t / (1::second))) + " seconds"; }
1::second => dur d1; log("d1 is " + stringify(d1)); // "d1 is 1 seconds" 300::samp => dur d2; log("d2 is " + stringify(d2)); // "d2 is 0.0068 seconds"
now + 60::second => time t1; log("t1 is " + stringify(t1)); // "t1 is 60 seconds" now + 1::minute => time t2; log("t2 is " + stringify(t2)); // "t2 is 60 seconds"
Or are you trying to do something else?
Understand that times are represented as a delta of the number of samples since the VM (or maybe it's the shred) started executing. Again, the time itself doesn't carry any information about how it was created, so these two times would have the same representation if they were created at the same time:
now + 1::minute => time t1; now + 60::second => time t2;
And these two times have different representations since they are not created at the same time:
now + 1::minute => time t1; 5::second => now; now + 60::second => time t2;
andy
On Sat, Jan 9, 2010 at 1:16 PM, Robert Poor
wrote: Sure, can do, but this is only for debugging -- I'd welcome a means to get the same effect as <<< "duration is", d >>>; which obviously works, but not as a string. Same comments apply for stringifying a time value.
- Rob
On Sat, Jan 9, 2010 at 13:08, Andrew Turley
wrote: Are you looking for a way to generate something that would ultimately generate a string that contains "1::second"? Because if that's the case, you probably can't do it. Durations ultimately get stored as floats that represent the number of samples in the duration. And they don't, as far as I know, retain any information about the units that were used when they were created. So 6::second and 0.1::minute will both be stored as the number of samples that are contained in a 6 second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d; d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you probably want is a duration in terms of time rather than samples, so you could also get the number of samples per second and divide dSamp by that number, and then store your duration as a string representation of a floating point number represented in terms of seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
wrote: How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
.toString() method works on objects only. printing time and duration is
always a little more work.
fun void printTime(time t)
{
<<< t/second, "::second" >>>;
}
printTime(now);
or for durations, you can do the same kind of thing:
fun void printTime(dur t)
{
<<< t/second, "::second" >>>;
}
printTime(400::ms);
this is generally what i do when i need to know print this kind of
information.
-mike
On Sat, Jan 9, 2010 at 11:01 PM, Andrew Turley
Sorry, yeah, the short answer is that there doesn't seem to be a good way to do what you're trying to do.
If you want to see what Chuck does with "<<<" and ">>>", take a look at Chuck_Instr_Hack::execute( Chuck_VM * vm, Chuck_VM_Shred * shred ) in chuck_instr.cpp for the details. There really isn't much in the way of conversion, it just uses fprintf to print a double.
andy
On Sat, Jan 9, 2010 at 2:27 PM, Robert Poor
wrote: Andrew: Yep, I'd come up with the same thing: dedicated functions to convert times and durations into strings. That works fine, but my question was actually simpler, and I apologize for not making it clear. What I should have said:
"It's clear that there are internal functions for converting times and durations into strings, used in constructs such as: <<< now, 1::second >>>; Are those internal functions available?"
... and I'm guessing the answer is simply "no". Your suggested solution will do the job just fine (though I'd follow ChucK's lead and print everything in terms of samples). Thanks!
- Rob
On Sat, Jan 9, 2010 at 14:11, Andrew Turley
wrote: So are you looking for something like this?
fun void log(string s) { <<<s>>>; }
fun string stringify(dur d) { return "" + ((d / (1::second))) + " seconds"; }
fun string stringify(time t) { return "" + ((t / (1::second))) + " seconds"; }
1::second => dur d1; log("d1 is " + stringify(d1)); // "d1 is 1 seconds" 300::samp => dur d2; log("d2 is " + stringify(d2)); // "d2 is 0.0068 seconds"
now + 60::second => time t1; log("t1 is " + stringify(t1)); // "t1 is 60 seconds" now + 1::minute => time t2; log("t2 is " + stringify(t2)); // "t2 is 60 seconds"
Or are you trying to do something else?
Understand that times are represented as a delta of the number of samples since the VM (or maybe it's the shred) started executing. Again, the time itself doesn't carry any information about how it was created, so these two times would have the same representation if they were created at the same time:
now + 1::minute => time t1; now + 60::second => time t2;
And these two times have different representations since they are not created at the same time:
now + 1::minute => time t1; 5::second => now; now + 60::second => time t2;
andy
On Sat, Jan 9, 2010 at 1:16 PM, Robert Poor
wrote: Sure, can do, but this is only for debugging -- I'd welcome a means to get the same effect as <<< "duration is", d >>>; which obviously works, but not as a string. Same comments apply for stringifying a time value.
- Rob
On Sat, Jan 9, 2010 at 13:08, Andrew Turley
wrote: Are you looking for a way to generate something that would ultimately generate a string that contains "1::second"? Because if that's the case, you probably can't do it. Durations ultimately get stored as floats that represent the number of samples in the duration. And they don't, as far as I know, retain any information about the units that were used when they were created. So 6::second and 0.1::minute will both be stored as the number of samples that are contained in a 6 second interval.
Now, what you can do is convert the duration to a float by doing this:
1::second => dur d; d / (1::samp) => float dSamp;
And then you could convert that float to a string. Now what you probably want is a duration in terms of time rather than samples, so you could also get the number of samples per second and divide dSamp by that number, and then store your duration as a string representation of a floating point number represented in terms of seconds (or maybe you want milliseconds).
andy
On Sat, Jan 9, 2010 at 12:50 PM, Robert Poor
wrote: How can I cast a duration into a string (e.g. for passing as a string argument)? Consider:
// ============= fun void log(string message) { // do something to log the message }
1::second => dur d; log("the duration is " + d); // fails with "cannot perform '+' on object references" log("the duration is " + d.toString()); // fails with "type 'dur' does not have members" // =============
Same question for time objects.
TIA.
- Rob _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Andrew; Understand that times are represented as a delta of the number of
samples since the VM (or maybe it's the shred) started executing. Again, the time itself doesn't carry any information about how it was created, []
Sorry for the late response; I haven't been at home over the weekend and my hosts were dealing with a LAN issue. Just catching up on mails here. You are quite right here, and I'd like to add that time is always expressed as samples since the origin of the VM. Instantiating a new variable of type time will have it refer to "time 0" being the origin of the VM. This is the same for all Shreds, no matter when they were sporked or added. "now" is a special case (of type time) and always refers to the amount of time that has passed since the start of the VM. It's also interesting to note that while time and now are quite universal things they are expressed in samps yet obviously the duration of a samp depends on the settings used. It's probably not a good idea to perform any calculations (but dsp) expressed in samps as your code won't be portable from machine to machine. BPM and/or tuning may change if you do. Hope that clarifies. Likely this was already well known to many but it's such a important point that I didn't want to gloss over it. Yours, Kas.
participants (4)
-
Andrew Turley
-
Kassen
-
mike clemow
-
Robert Poor