zsh-users
 help / color / mirror / code / Atom feed
* a calculator for zsh - zcalc and pcal conflict
@ 2003-01-30 17:08 kwong
  2003-01-30 17:34 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: kwong @ 2003-01-30 17:08 UTC (permalink / raw)
  To: zsh-users

All,

Earlier I posted a calculator "pcal" written in perl as
a followup to Clifford's post of "zcalc" written in zsh.

Andy Spiegl noticed a problem and his email is attached
below.

After some discussion, we figured out that this is due to
a conflict between the way "zcalc" and "pcal" uses the
variable "ans".

When "zcalc" is called, it implicitly typeset the variable
"ans" to -E (float). Once that is done, it is irreversible
until you "unset ans".  And if you then use "pcal", it will
bomb if "ans" is set to a non-numerical value.

Some ways to work around it (none is ideal):

   * refrain from using "zcalc" and then "pcal".
   
   * "ans" for "zcalc" and "an" for "pcal" (you have to
     modify the definition of pcal.s2 to change all "ans" to
     "an".

mk

********************************************************************

Andy wrote:

> 
> Hi Kwong,
> 
> great stuff too!
> 
> > Let me know if you find any bugs.
> I'm not sure if it's a bug, but it's strange and I don't understand why.
> It seems to have to do with the calculator of Clifford, because this
> happens:
> 
>  condor:~>pcal h abc * d
>  ans = 8b8c
> 
>  condor:~>zcalc '4 * 4'
>  zcalc: bad math expression: operator expected at `b8c'
>  zsh: exit 1
> 
>  condor:~>unset ans
> 
>  condor:~>zcalc '4 * 4'
>  16
> 
>  condor:~>pcal h abc * d
>  pcal-ans:4: bad math expression: operator expected at `b8c'
>  zsh: exit 1
> 
> Can you see why the two interfere???  $ans only holds a number, right?
> Besides I'm not even using ans in this example.  Strange.
> 
> So I turned on debugging (see below) and get this:
>  condor:~>print $ans
>  3
> 
>  condor:~>pcal h abc * d
>  trace pcal-ans:2(if)> [[ -n h abc * d ]]
>  trace pcal-ans:4(then)> _x=trace pcal-ans:4(then cmdsubst)> echo h abc * d
>  trace pcal-ans:4(then)> _x=trace pcal-ans:4(then cmdsubst pipe)> sed s/ans/3/g
>  trace pcal-ans:4(then)> _x=h abc * d 
>  trace pcal-ans:5(then)> ans=trace pcal-ans:5(then cmdsubst)> pcal.pl h abc * d
>  trace pcal-ans:5(then)> ans=8b8c pcal-ans:5: bad math expression: operator expected at `b8c'
> 
> I don't get it, sorry.
> 
> Okay, I've saved your script as pcal.pl and put this into my .zsh/alias:
> 
>  pcal-ans() {
>  set -x
>    if [[ -n "$*" ]]
>    then
>      _x=`echo $* | sed "s/ans/$ans/g"`
>      ans=`noglob pcal.pl $_x` 
>      print ans = $ans
>      unset _x
>    else
>       pcal.pl
>    fi
>  set +x
>  }   
>  alias pcal='noglob pcal-ans'
> 
>  # ----------------------------------------------------------------------
>  # Here are some quick calculators that output in integer
>  # hexadecimal, decimal, and binary.
>  zcalc ()  { print $(( ans = ${@:-ans} )) }
>  zcalch () { print $(( [#16] ans = ${@:-ans} )) }   
>  zcalcd () { print $(( [#10] ans = ${@:-ans} )) }
>  zcalco () { print $(( [#8] ans = ${@:-ans} )) }
>  zcalcb () { print $(( [#2] ans = ${@:-ans} )) }
> 
>  # A key binding that will allow you to quickly get into zcalc
>  bindkey -s '\C-xd' "zcalc "
> 
>  # this last one lets you calculate the ascii value of a single character
>  zcalcasc () { print $(( [#16] ans = ##${@:-ans} )) }
>  # -------------------------------------------------------------------end
> 
> 
> Any help would be very much appreciated!
> Thanks,
>  Andy.
> 
> -- 
>            http://peru.spiegl.de  Our project
>       http://radiomaranon.org.pe  Radio Marañón, Jaén, Perú
>                               o      _     _         _
>   ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
>   ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
>   ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: a calculator for zsh - zcalc and pcal conflict
  2003-01-30 17:08 a calculator for zsh - zcalc and pcal conflict kwong
@ 2003-01-30 17:34 ` Peter Stephenson
  2003-01-31 12:03   ` Clifford Caoile
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2003-01-30 17:34 UTC (permalink / raw)
  To: zsh-users

kwong@nwsgpa.ih.lucent.com wrote:
> All,
> 
> Earlier I posted a calculator "pcal" written in perl as
> a followup to Clifford's post of "zcalc" written in zsh.
> 
> Andy Spiegl noticed a problem and his email is attached
> below.

If you're worrying about conflicts, you might note there is already a
`zcalc' supplied with the 4.1 zsh distribution.  It's a command-line
interface rather than one-shot.  See the zshcontrib manual page.

Yes, we still need to release 4.1, so you wouldn't know about this
unless you're tracking the development version.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: a calculator for zsh - zcalc and pcal conflict
  2003-01-30 17:34 ` Peter Stephenson
@ 2003-01-31 12:03   ` Clifford Caoile
  0 siblings, 0 replies; 3+ messages in thread
From: Clifford Caoile @ 2003-01-31 12:03 UTC (permalink / raw)
  To: zsh-users

Everyone:

Thanks to kwong and Phil for the zcalc suggestions. I have implemented the
$@ in the input string and also the noglob/realias trick in my own .zshrc.
I'm pretty sure the settings are obvious so I won't post them here.

I don't know how "elegant" my "zcalc" is, but I get some round-off errors
with some float computations. For example:

+2403> zcalc 23.0 * 1.05                         ~ 31Fri203104
24.150000000000002
+2404> zsh --version                             ~ 31Fri203106
zsh 4.0.6 (i686-pc-cygwin)

+2403> echo $(( 23.0 * 1.05))                  ~ 31Fri205300 1
24.150000000000002
+2404> zsh --version                             ~ 31Fri205302
zsh 4.0.4 (i386-redhat-linux-gnu)

I have seen it where there isn't any round off error on my other machines at
work and at home. What is this? Anyway, not a big deal, just a little
annoying.

Also, thanks to Peter S. for the heads up on the zsh-4.1.x dev branch. I
guess for my own sake I should rename this "zcalc" so I'll be forward
compatible. As far as I'm concerned, only 4.0.4 and 4.0.6 exist, because
those are the zsh versions on my personal and work computers (RedHat 7.3,
8.0, Cygwin 2003/1), so I didn't know about that. I look forward to a
release, but frankly, I think zsh-4.0.6 is pretty cool as is.

Anyway, I've been thinking more and more that zsh is becoming more and more
emacs-y, as in fully featured for the interactive use. GNU Emacs is an
"advanced, self-documenting, customizable, extensible real-time display
editor." zsh is an "advanced,
self-documenting, customizable, extensible real-time display CLI".

  ,-~-.
 < ^ ; ~,  Clifford Escobar CAOILE  (aka "Piyo-kun")
  (_  _,
   J~~>  _.___...:.__..__.: __.:_. .:_.__::_..:._::._...  _____ p(^_^)q


| -----Original Message-----
| From: Peter Stephenson [mailto:pws@csr.com]
| Sent: Friday, January 31, 2003 02:34
| To: zsh-users@sunsite.dk
| Subject: Re: a calculator for zsh - zcalc and pcal conflict
|
|
| kwong@nwsgpa.ih.lucent.com wrote:
| > All,
| >
| > Earlier I posted a calculator "pcal" written in perl as
| > a followup to Clifford's post of "zcalc" written in zsh.
| >
| > Andy Spiegl noticed a problem and his email is attached
| > below.
|
| If you're worrying about conflicts, you might note there is already a
| `zcalc' supplied with the 4.1 zsh distribution.  It's a command-line
| interface rather than one-shot.  See the zshcontrib manual page.
|
| Yes, we still need to release 4.1, so you wouldn't know about this
| unless you're tracking the development version.
|
| --
| Peter Stephenson <pws@csr.com>                  Software Engineer
| CSR Ltd., Science Park, Milton Road,
| Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070
|
|
| **********************************************************************
| The information transmitted is intended only for the person or
| entity to which it is addressed and may contain confidential
| and/or privileged material.
| Any review, retransmission, dissemination or other use of, or
| taking of any action in reliance upon, this information by
| persons or entities other than the intended recipient is
| prohibited.
| If you received this in error, please contact the sender and
| delete the material from any computer.
| **********************************************************************



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-01-31 12:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-30 17:08 a calculator for zsh - zcalc and pcal conflict kwong
2003-01-30 17:34 ` Peter Stephenson
2003-01-31 12:03   ` Clifford Caoile

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).