Subject: Re: [ast-users] Discipline functions for float vs. $ printf "%f" s_t.varname # ... --------
> Hi! > > ---- > > The following testcase contains a simple "stopwatch" type which tracks > the time between the functions "start" and "stop" and maintains a > variable "diff" which contains the difference: > -- snip -- > typeset -T stopwatch_t=( > float starttime=nan > float stoptime=nan > float diff=nan > > function start > { > (( _.starttime=SECONDS )) > return 0 > } > function stop > { > (( _.stoptime=SECONDS )) > return 0 > } > function reset > { > (( _.starttime=nan , _.stoptime=nan )) > } > function diff.get > { > (( .sh.value=_.stoptime-_.starttime )) > print -u2 "#mark" > return 0 > } > ) > > stopwatch_t sw > > sw.start > sleep 2.1 > sw.stop > > printf "time=%f\n" sw.diff > -- snip -- > > However when I run this testcase with ast-ksh.2009-06-22 it only prints: > -- snip -- > time=nan > -- snip -- > ... while the expected output should AFAIK be: > -- snip -- > #mark > time=2.1000 > -- snip -- > > AFAIK two things go wrong there: > 1. Using $ printf "time=%f\n" sw.diff # to call the discipline method > stopwatch_t.diff.get does not work, however it works when I replace > "sw.diff" with "${sw.diff}" The get discipline is only called to get string values, $var. Internally, the shell uses a different discipline to get a numerical value. I will consider adding a getn and setn disciplines which will allow an assignment to .sh.value which will be type float to a future release. > 2. The somehow the value assigned to ".sh.value" does not make it to the > "printf" builtin... but I am not sure why this happens... The other problem is that in diff.get, The _ variable is set to sw.diff, not sw. Therefore, you can change function diff.get to function diff.get { nameref __=${.sh.name%.*} (( .sh.value=__.stoptime-__.starttime )) } Better you, when not just define diff() { (( _.stoptime - _.starttime )) } which would allow printf "time=%f\n" ${sw.diff} to print the time. > > ---- > > Bye, > Roland > David Korn d...@research.att.com _______________________________________________ ast-users mailing list ast-users@research.att.com https://mailman.research.att.com/mailman/listinfo/ast-users