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

Re: integer or float?



You missed the point. No pun intended.

The numeric value of "1." is distinct from the value "1", not just in zsh but many proglangs. "1." is a float where "1" is an int. Which means if you divide "1."  by 2, you get 0.5. But if you divide "1" by 2, you get 0.

So that period is conveying type information. A trailing 0 would also,work though it suggests a level of precision that may not be appropriate. But printing the float value 1. as "1" would destroy the ability to capture that value intact and recreate it from the output string - which is of course the only way you can "return" such a value in a shell script. 

Mark J. Reed <markjreed@xxxxxxxxx>


On Thu, Oct 2, 2025 at 09:10 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:


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




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