public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "paul at inet dot co dot za" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sources.redhat.com
Subject: [Bug libc/4943] Inconsistent rounding behaviour for sprintf and IEEE doubles
Date: Sat, 22 Sep 2007 20:20:00 -0000	[thread overview]
Message-ID: <20070922201957.5067.qmail@sourceware.org> (raw)
In-Reply-To: <20070820142205.4943.paul@inet.co.za>


------- Additional Comments From paul at inet dot co dot za  2007-09-22 20:19 -------
(In reply to comment #24)

> And again, I suggest that you read
>   http://www2.hursley.ibm.com/decimal/
> (in particular the FAQ).

I have read this document, thanks.  FP is accurate enough for my purposes in all
calculations.  Once the result needs to be displayed though, I have an entirely
different situation.  Yes, of course I have work-arounds, as shown in my
original example.  Why are these work-arounds necessary though?

> Paul, you really make every one (including me) think that you do not 
> understand floating-point arithmetic. 

This is the strangest thing.  I probably understand it better than most, which
is really scary when you think about it.  Since the late 80's I could quote the
IEEE storage format without a second thought.

Perhaps it is not common to have people query these things.  Everyone just seems
to accept that "floating point is inaccurate" and are happy to leave it at that.
 When someone comes along and rocks the boat people get all upset.

> Double-precision numbers are binary numbers, not decimal numbers. Saying 
> "15-digit precision" is misleading; 

I thought I was using the correct terminology when I said "15 decimals of
precision" or phrased differently, "15 decimal digits of precision"...

> you should say "53-bit precision" since their representation is in binary.

well, not quite 53 bits, rather 52.  The extra bit is implied simply because the
FPU does not store the value 0.  When you break it down, though, it's not even
52 bits, as you cannot ignore calculation and storage error.  The largest
mantissa is (decimal) 9007199254740991 which is 16 digits.  We know that there
is always an error associated with storage, and therefore we cannot consider
more than 15 decimals (decimal digits) to be accurate.

> Now, if you have so much confidence in Microsoft's C library, I suggest that 
> you try the following program:

Erg.  Confidence?  I think my words were "Microsoft does not seem to have this
problem"...

> #include <stdio.h>
> int main (void)
> {
>   volatile double x = 2597.525;
>   volatile double y = 5000.525;
>   printf ("%.2f\n%.2f\n", x, y);
>   return 0;
> }
> 
> (please do not change the numbers), and see if you get "consistent" results for
> x and for y.

Ok, Vincent, you are right.  Well done.  In this case, even my "work-around" fails.

MS stores the value
  5000.525
as
  5000.524999999999600

Ignoring the error, the value 
  5000.52499999999 
being less than
  5000.525
rounds to
  5000.52
(closest)

Linux produces the identical result.

Should the FPU be ignoring the error when promoting 5000.5249999999996 to
5000.52499999999 to account for precision?  Or should it not instead have
promoted the value to 5000.52500000000?  I cannot answer that. Looks like a true
case of FP inaccuracy to me.

This is very different from the case of 2597.625 where we had an exactly stored
value being rounded towards the even value simply because it was exactly stored
and on a boundary.  

Seems no-one can get this right.  I will see what Microsoft have to say about
this.  They don't have the "round to nearest even on boundaries" argument to
throw my way.  I will certainly inform you as to the outcome.

Their result is certainly inconsistent with your earlier example:

#include <stdio.h>
int main (void)
{
  double x;
  for (x = -8.5; x <= 8.5; x += 1.0)
    printf ("%4g %.0f\n", x, x);
  return 0;
}

[Microsoft output]

-8.5 -9
-7.5 -8
-6.5 -7
-5.5 -6
-4.5 -5
-3.5 -4
-2.5 -3
-1.5 -2
-0.5 -1
 0.5 1
 1.5 2
 2.5 3
 3.5 4
 4.5 5
 5.5 6
 6.5 7
 7.5 8
 8.5 9


-- 


http://sourceware.org/bugzilla/show_bug.cgi?id=4943

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


  parent reply	other threads:[~2007-09-22 20:20 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-20 14:22 [Bug libc/4943] New: " paul at inet dot co dot za
2007-09-19 13:29 ` [Bug libc/4943] " vincent+libc at vinc17 dot org
2007-09-19 17:48 ` paul at inet dot co dot za
2007-09-19 21:22 ` vincent+libc at vinc17 dot org
2007-09-20  0:01 ` paul at inet dot co dot za
2007-09-20  1:02 ` vincent+libc at vinc17 dot org
2007-09-20  7:04 ` paul at inet dot co dot za
2007-09-20 13:57 ` drepper at redhat dot com
2007-09-20 14:33 ` paul at inet dot co dot za
2007-09-20 15:05 ` vincent+libc at vinc17 dot org
2007-09-20 15:56 ` paul at inet dot co dot za
2007-09-21 15:14 ` vincent+libc at vinc17 dot org
2007-09-21 22:31 ` paul at inet dot co dot za
2007-09-21 23:43 ` vincent+libc at vinc17 dot org
2007-09-22  7:51 ` paul at inet dot co dot za
2007-09-22  9:11   ` Pierre Habouzit
2007-09-22  9:11 ` madcoder at debian dot org
2007-09-22  9:39 ` paul at inet dot co dot za
2007-09-22  9:58 ` schwab at suse dot de
2007-09-22 10:09 ` paul at inet dot co dot za
2007-09-22 10:39 ` madcoder at debian dot org
2007-09-22 10:55 ` paul at inet dot co dot za
2007-09-22 11:05 ` paul at inet dot co dot za
2007-09-22 11:15 ` madcoder at debian dot org
2007-09-22 11:40 ` paul at inet dot co dot za
2007-09-22 13:02 ` vincent+libc at vinc17 dot org
2007-09-22 20:20 ` paul at inet dot co dot za [this message]
2007-09-22 23:09 ` vincent+libc at vinc17 dot org
2007-09-23  7:19 ` paul at inet dot co dot za
2007-09-23  9:33 ` vincent+libc at vinc17 dot org
2007-09-23 10:21 ` paul at inet dot co dot za
2007-09-23 16:41 ` paul at inet dot co dot za
2007-09-23 18:54 ` paul at inet dot co dot za
2007-09-23 19:31 ` vincent+libc at vinc17 dot org
2007-09-23 19:54 ` paul at inet dot co dot za
2007-09-25  6:51 ` paul at inet dot co dot za
2007-09-25 10:43 ` vincent+libc at vinc17 dot org
2007-09-25 11:01 ` paul at inet dot co dot za
2007-10-03 10:04 ` paul at inet dot co dot za
2007-10-03 10:37 ` madcoder at debian dot org
2007-10-03 11:48 ` paul at inet dot co dot za
2007-10-03 12:32 ` vincent+libc at vinc17 dot org
2007-10-03 15:07 ` paul at inet dot co dot za
2007-10-03 15:26 ` vincent+libc at vinc17 dot org
2007-10-03 17:21 ` paul at inet dot co dot za
2007-10-04  0:47 ` vincent+libc at vinc17 dot org
2007-10-04  7:52 ` paul at inet dot co dot za
2007-10-05 19:20 ` redhat dot com+Bugzilla at edp dot org
2007-10-05 19:53 ` paul at inet dot co dot za
     [not found] <bug-4943-131@http.sourceware.org/bugzilla/>
2014-02-16 19:41 ` jackie.rosen at hushmail dot com
2014-05-28 19:46 ` schwab at sourceware dot org
2014-05-28 19:47 ` schwab at sourceware dot org

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=20070922201957.5067.qmail@sourceware.org \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sources.redhat.com \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).