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

Re: integer or float?



On Thu, Oct 2, 2025, at 10:11 AM, Ray Andrews wrote:
> % echo $((.5))
> 0.5
> ... zsh very politely fixes sloppy input.
>
> % echo $((1.0))
> 1.
> ... zsh very rudely breaks correct input.

It's not broken, no matter how many times you assert that it is.

> That can't be right.  If I go 
> to the trouble to add the zero, why remove it?

Evidently it's normalizing the value.  Perhaps someone else knows
why the normalized form is the way it is, but as long as it works
with the shell's own arithmetic, it's correct.

> % var=1.1
> % echo $((var))
> 1.1000000000000001  # I myself would prefer '1.1'
>
> % var=1.0
> % echo $((var))
> 1.                                  # I'd prefer '1.0'
>
> % echo $((1.001))
> 1.0009999999999999   # '1.001' please

https://0.30000000000000004.com/

Again, if you care about the visible representation of a number
(this includes inspecting variable expansions), you should be using
formatted output, not expecting the shell to read your mind.

-- 
vq




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