zsh-users
 help / color / mirror / code / Atom feed
* How to set signifcant figures of $((..)) ?
@ 2014-09-02 13:22 Han Pingtian
  2014-09-02 14:08 ` Peter Stephenson
  2014-09-02 14:09 ` Kurtis Rader
  0 siblings, 2 replies; 3+ messages in thread
From: Han Pingtian @ 2014-09-02 13:22 UTC (permalink / raw)
  To: zsh-users

Hi,

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?

Thanks in advance!


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to set signifcant figures of $((..)) ?
  2014-09-02 13:22 How to set signifcant figures of $((..)) ? Han Pingtian
@ 2014-09-02 14:08 ` Peter Stephenson
  2014-09-02 14:09 ` Kurtis Rader
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2014-09-02 14:08 UTC (permalink / raw)
  To: zsh-users

On Tue, 02 Sep 2014 21:22:35 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to set signifcant figures of $((..)) ?
  2014-09-02 13:22 How to set signifcant figures of $((..)) ? Han Pingtian
  2014-09-02 14:08 ` Peter Stephenson
@ 2014-09-02 14:09 ` Kurtis Rader
  1 sibling, 0 replies; 3+ messages in thread
From: Kurtis Rader @ 2014-09-02 14:09 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 882 bytes --]

It can't produce that output. The IEEE 754 standard for double precision
floating point values doesn't contain enough bits for more than 17 digits
of precision. See
http://en.wikipedia.org/wiki/Double-precision_floating-point_format

The reason 2**63 outputs a negative number is that integer arithmetic is
causing overflow into the sign bit of the 64 bit integer.

Also, you're using the wrong tool if you're trying to use zsh for high
precision math.


On Tue, Sep 2, 2014 at 6:22 AM, Han Pingtian <hanpt@linux.vnet.ibm.com>
wrote:

> Hi,
>
> 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?
>
> Thanks in advance!
>
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-09-02 14:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-02 13:22 How to set signifcant figures of $((..)) ? Han Pingtian
2014-09-02 14:08 ` Peter Stephenson
2014-09-02 14:09 ` Kurtis Rader

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).