zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh-workers@zsh.org
Subject: Re: inf and nan in arithmetic expansions
Date: Fri, 9 Feb 2018 21:09:48 +0000	[thread overview]
Message-ID: <20180209210948.GA27566@chaz.gmail.com> (raw)
In-Reply-To: <1518190272.2338601.1265377152.605E16CD@webmail.messagingengine.com>

2018-02-09 15:31:12 +0000, Daniel Shahaf:
> Stephane Chazelas wrote on Thu, 08 Feb 2018 14:22 +0000:
> > 2018-02-08 12:46:35 +0000, Daniel Shahaf:
> > > And then we could add 'inf' and 'nan' as readonly variables initialised to
> > > those respective values (as Oliver also suggests in the 19597 thread).  There
> > > are compatibility implications for scripts that use these variable names, but
> > > there is no way around them if we want to allow explicitly doing (( x = inf ))
> > > in user code...
> > 
> > But what value would you use for those variables?
> 
> 'inf' would be positive infinity. 'nan' would be a NaN (whichever flavour
> thereof would be least surprising).
> 
> > Or do you mean that the shell arithmetic parser would understand
> > "inf"/"nan" as the infinity and not-a-number numbers and not as the
> > variable (otherwise with inf=inf, that would do infinite recursion)
> 
> Why would it be recursive?

I mean as in:

$ inf=inf zsh -c 'echo $((inf))'
zsh:1: math recursion limit exceeded: inf

a variable name in arithmetic expression is expanded and its
value subject to arithmetic evaluation and so on.

There's always the option of defining them as:

$ nan='(-(1e9999/1e9999))' inf='(1e9999)' zsh -c 'echo $((nan)) $((inf))'
nan. inf.

(I don't know why I need the "-" there, it's the same in ksh93)

> 
> > but just make inf/nan readonly so users not be tempted to use them as
> > variables?
> 
> That would be the simplest implementation, yes.  It might suffice, or we might
> prefer something more elaborate.

I'm not sure I see the benefit of preventing users using from
using variables called "inf" or "nan". It seems to me that
ksh93's approach at just treating "inf" and "nan" as literral
constants representing their value like in C would be enough
(which would in effect prevent users from using variables by the
same name inside but not outside arithmetic expression (though
they'd still be able to do $(($inf)))).

-- 
Stephane


  reply	other threads:[~2018-02-09 21:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-07 22:30 Stephane Chazelas
2018-02-07 23:25 ` Oliver Kiddle
2018-02-08  9:38   ` Peter Stephenson
2018-02-08 12:46   ` Daniel Shahaf
2018-02-08 14:22     ` Stephane Chazelas
2018-02-09 15:31       ` Daniel Shahaf
2018-02-09 21:09         ` Stephane Chazelas [this message]
2018-02-10  0:10           ` Bart Schaefer
2018-02-16 16:51     ` Oliver Kiddle
2018-02-17  0:38       ` Daniel Shahaf
2018-02-19 14:19         ` Stephane Chazelas
2018-02-27 13:02       ` Vincent Lefevre
2018-02-27 15:25         ` Oliver Kiddle
2018-02-27 16:56           ` Vincent Lefevre
2018-03-21 23:46       ` Oliver Kiddle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180209210948.GA27566@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).