zsh-users
 help / color / mirror / code / Atom feed
From: Erik Trulsson <ertr1013@student.uu.se>
To: Patrick Aussems <zyk@bugfactory.org>
Cc: zsh-users@sunsite.auc.dk
Subject: Re: Floating point calculus error...
Date: Sun, 25 Aug 2002 18:01:51 +0200	[thread overview]
Message-ID: <20020825160151.GA5679@falcon.midgard.homeip.net> (raw)
In-Reply-To: <1030288941.517.8.camel@Amok>

On Sun, Aug 25, 2002 at 05:22:20PM +0200, Patrick Aussems wrote:
> Hi all...
> 
> I was doing some shell scripts that were supposed to add CPU usages from
> the ps output... But I got puzzled when I got an answer with a 17th
> decimal while adding numbers with only 1 decimal... Strange isn't it?

Not strange at all.  That sort of things is to be expected when using
floating point.

> 
> This behaviour can be observed by doing the following command:
> 
> zyk@Amok:/home/zyk> echo $[0.4+0.4]
> 
> The value returned is: 0.80000000000000004 which is obviously not the
> correct answer... Okay, floating points numbers are inaccurate, but
> still...

Since neither 0.4 or 0.8 cannot be represented exactly in a binary
floating point format one shouldn't expect the ouput to be quite
correct.  
The internal form that "0.4" gets converted to is actually some other
number which has almost but not exactly the value 0.4 When you then add
two such numbers you will get a number that is almost but not quite
0.8.

This is similar to the behaviour of most pocket calculators where
trying to calculate 3*(1/3) usually results in 0.9999999999 instead of
the mathematically correct 1.


> 
> Patrick
> 
> PS: I don't have the time to read the mailing lists, so I don't know if
> this problem has already been reported...

It is not really a problem. It is just the way floating point is.

> 
> PS2: Please CC me on replies, I'm not subscribed to this mailing list...
> 
> 

-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se


  parent reply	other threads:[~2002-08-25 16:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-25 15:22 Patrick Aussems
2002-08-25 15:53 ` Vincent Lefevre
2002-08-25 16:32   ` David Kågedal
2002-08-25 18:07     ` Vincent Lefevre
2002-08-25 16:01 ` Erik Trulsson [this message]
2002-08-25 16:37 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020825160151.GA5679@falcon.midgard.homeip.net \
    --to=ertr1013@student.uu.se \
    --cc=zsh-users@sunsite.auc.dk \
    --cc=zyk@bugfactory.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).