Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
output formats for zcalc
- X-seq: zsh-workers 16352
- From: Oliver Kiddle <okiddle@xxxxxxxxxxx>
- To: zsh-workers@xxxxxxxxxx
- Subject: output formats for zcalc
- Date: Mon, 17 Dec 2001 11:18:48 +0000
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- Sender: kiddleo@xxxxxxxxxxxxxxxxx
I noticed that the to do section of zcalc mentions setting of output
formats. My own zcalc like function[1] already does this so it was easy
enough to move across. Note that this uses printf so won't work on
4.0.
I used syntax that would be familiar to anyone else who used Casio
calculators:
fix n - set to n fixed decimal places
sci n - set to n significant figures
eng n - use engineering notation
norm - return to normal
I'd actually prefer a `base' command to set the number base to the zsh
like [#16] stuff and hex, oct, dec, bin shortcuts.
I've also renamed $latest to $ans and removed the part that clears it.
The Ans variable on many calculators is a shortcut to retrieve the last
answer and is useful here as an alternative to looking what number the
last command was.
[1] - My zcalc like function differs from zcalc in that it doesn't have
it's own command-line. I just have a noglob alias to it and use it
directly from the zsh prompt. What would be nice was if zcalc was
adapted to also work in this way so if invoked with parameters, it
would just arithmetically evaluate its arguments and print the result.
$ans should then be left global. And I could then delete my function.
Oliver
Index: Functions/Misc/zcalc
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/zcalc,v
retrieving revision 1.6
diff -u -r1.6 zcalc
--- Functions/Misc/zcalc 2001/12/07 12:59:09 1.6
+++ Functions/Misc/zcalc 2001/12/17 11:13:57
@@ -82,8 +82,6 @@
# To do:
# - separate zcalc history from shell history using arrays --- or allow
# zsh to switch internally to and from array-based history.
-# - allow setting number of decimal places for display, scientific notation,
-# etc.
emulate -L zsh
setopt extendedglob
@@ -107,9 +105,11 @@
}
trap zcalc_restore HUP INT QUIT EXIT
-local line latest base defbase match mbegin mend psvar optlist opt arg
-integer num
+local line ans base defbase forms match mbegin mend psvar optlist opt arg
+integer num outdigits outform=1
+forms=( '%2$g' '%.*g' '%.*f' '%.*E' )
+
zmodload -i zsh/mathfunc 2>/dev/null
: ${ZCALCPROMPT="%1v> "}
@@ -178,30 +178,53 @@
else
base=$defbase
fi
- # Exit if `q' on its own.
- [[ $line = [[:blank:]]#q[[:blank:]]# ]] && return 0
print -s -- $line
- if [[ $line = [[:blank:]]#local([[:blank:]]##*|) ]]; then
- eval $line
- else
+
+ case ${${line##[[:blank:]]#}%%[[:blank:]]#} in
+ q) # Exit if `q' on its own.
+ return 0
+ ;;
+ norm) # restore output format to default
+ outform=1
+ ;;
+ sci[[:blank:]]#(#b)(<->)(#B))
+ outdigits=$match[1]
+ outform=2
+ ;;
+ fix[[:blank:]]#(#b)(<->)(#B))
+ outdigits=$match[1]
+ outform=3
+ ;;
+ eng[[:blank:]]#(#b)(<->)(#B))
+ outdigits=$match[1]
+ outform=4
+ ;;
+ local([[:blank:]]##*|))
+ eval $line
+ line=
+ continue
+ ;;
+ *)
# Latest value is stored as a string, because it might be floating
# point or integer --- we don't know till after the evaluation, and
# arrays always store scalars anyway.
#
# Since it's a string, we'd better make sure we know which
# base it's in, so don't change that until we actually print it.
- latest=
- eval "latest=\$(( $line ))"
- # on error $latest is not set; let user re-edit line
- [[ -n $latest ]] || continue
- argv[num++]=$latest
+ eval "ans=\$(( $line ))"
+ # on error $ans is not set; let user re-edit line
+ [[ -n $ans ]] || continue
+ argv[num++]=$ans
psvar[1]=$num
- if [[ -z $base ]]; then
- print -- $latest
- else
- print -- $(( $base $latest ))
- fi
+ ;;
+ esac
+ if [[ -n $base ]]; then
+ print -- $(( $base $ans ))
+ elif [[ $ans = *.* ]] || (( outdigits )); then
+ printf "$forms[outform]\n" $outdigits $ans
+ else
+ printf "%d\n" $ans
fi
line=
done
_____________________________________________________________________
This message has been checked for all known viruses by the
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp
Messages sorted by:
Reverse Date,
Date,
Thread,
Author