Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: inf and nan in arithmetic expansions



Oliver Kiddle wrote on Fri, 16 Feb 2018 17:51 +0100:
> > 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...
> 
> I'm not sure about making them readonly simply because not doing so is
> less likely to break an existing script.

After 42356 I am not sure whether I would prefer predefined variables
(readonly or not) or recognising 'inf' and 'nan' (putting aside the
question of case for a moment) as special constants in math contexts
as 42356 suggests.

> @@ -791,6 +822,21 @@ zzlex(void)
>  	    break;
>  	/* Fall through! */

Unrelated to the patch: that comment is incorrect.

>  	default:
> +	    if (strcmp(ptr-1, "NaN") == 0) {
> +		yyval.type = MN_FLOAT;
> +		yyval.u.d = 0.0;
> +		yyval.u.d /= yyval.u.d;
> +		ptr += 2;
> +		return NUM;
> +	    }
> +	    else if (strcmp(ptr-1, "Inf") == 0) {
> +		yyval.type = MN_FLOAT;
> +		yyval.u.d = 0.0;
> +		yyval.u.d = 1.0 / yyval.u.d;
> +		ptr += 2;
> +		return NUM;
> +	    }
> +
>  	    if (idigit(*--ptr) || *ptr == '.')
>  		return lexconstant();
>  	    if (*ptr == '#') {



Messages sorted by: Reverse Date, Date, Thread, Author