Re: integer or float?

Ray Andrews <[email protected]> Thu, 2 Oct 2025 06:10:02 -0700
Newsgroups gmane.comp.shells.zsh.user
Message-ID <[email protected]>

On 2025-10-01 21:31, Lawrence Velázquez wrote:
> 	% x=$((1.0))
> 	% typeset -p x
> 	typeset x=1.
> 	% print $((x / 2))
> 	0.5
>
> you would get this:
>
> 	% x=$((1.0))
> 	% typeset -p x
> 	typeset x=1
> 	% print $((x / 2))
> 	0
>
>
Ah, but that is an actual decimal value which of course must be 
preserved.  BTW I note the mathematically conventional *leading* zero.  
By exactly the same convention a trailing zero is correct: '5.0', 
'0.5'.  '5.'  is as mathematically dirty as '.5'  (with no leading zero) 
would be.  Anyway I take it that this is now so deeply established that 
it would be unwise to tinker with it given that there's no real problem 
once you understand what's going on. However the above:

% unsetopt FORCE_FLOAT
	% echo $((1.0))
	1.

... really does seem like a mistake:

% setopt forcefloat; echo $((1.0))
1.

% unsetopt forcefloat; echo $((1.0))
1.

% unsetopt forcefloat; echo $((1))
1

% setopt forcefloat; echo $((1))
1.

contrast the uniform an correct output here:

% setopt forcefloat; echo $((.5))
0.5

% unsetopt forcefloat; echo $((.5))
0.5

% setopt forcefloat; echo $((0.5))
0.5

% unsetopt forcefloat; echo $((0.5))
0.5

... note that above zsh actually 'fixes it for you': '.5' becomes 
'0.5'.  So we have rigor with leading zeros but not trailing zeros.

BTW another question:

Playing with Benford's Law, I had zsh creating my data for me, say 1000 
random floats, then each of them multiplied by another random float and 
the whole thing repeated some number of iterations.  Then graph it in 
GeoGebra.  Anyway I finally got the math done within GeoGebra (vastly 
more obtuse than zsh!) and it will recompute the whole show in less than 
a second whereas the same data from zsh took about a minute -- and 
that's just the data, not including graphing. Now, zsh is not 
graphing/mathematical software so I'm not whining, still I'm curious, 
I'd expect that under the hood a math call is a math call at the system 
level.  I'm guessing that it's because zsh is interpreted whereas GG can 
probably ... I'll say 'compile' for lack of a better word.  Yes?

https://www.geogebra.org/m/aeafk6bs