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

Re: How to set signifcant figures of $((..)) ?



On Tue, 02 Sep 2014 21:22:35 +0800
Han Pingtian <hanpt@xxxxxxxxxxxxxxxxxx> wrote:
> I have this results:
>
>   % zsh  -f
>   localhost% print $((2**63))
>   -9223372036854775808
>   localhost% print $((2.0**63))
>   9.2233720368547758e+18
>   localhost%
>
> How can the second print puts 9.223372036854775808e+18, please?

To be able to specify digits you need to use a floating point variable
and substitute it directly, not via $((...)).  Obviously, all the
standard disclaimers about ranges and precisions of floating point
arithmetic apply --- this is with double precision on 32-bit Linux.

% typeset -E 20 f
% (( f = 2.0**63 ))
% print $f
9.2233720368547758080e+18

If you don't want to have to specify digits you need to avoid
exponential notation.

% typeset -F f
% (( f= 2.0**63 ))
% print $f
9223372036854775808.0000000000

Rather surprisingly,

printf %.18e\\n $((2.0**63))

does get you the "08" --- logically it shouldn't because you get the
default output, which you quote above, before the printf conversion.  I
suspect it's rounding internally then outputting the rounded value using
the additional precision, so it's just luck (but not unexpected luck in
the case of a power of two).

pws



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