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: Sun, 23 Sep 2007 18:54:00 -0000 [thread overview] Message-ID: <20070923185354.17871.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-23 18:53 ------- ... and this version deals correctly with the outside cases... #include <math.h> #include <string.h> #include <stdio.h> //______________________________________________________________________ // Utility function converts an IEEE double precision number to a // fixed precision decimal format stored in a buffer. void tobuf(size_t max, int *len, char *buf, double x, int precision, double max_prec, double carry) { int sign = x < 0; // remember the sign double q = pow10(-precision); // current mask double y = x==0?0:fmod(fabs(x), q); // modulus double l_div = round(y*max_prec)/max_prec+carry; // significant digit int l_dec = (int)round(l_div*10/q); // round to decimal carry = l_dec>=10?l_div:0; // carry forward? l_dec = l_dec % 10; // this decimal x = x>0?x-y:x+y; // subtract modulus if (fabs(x) > 0) // recurse while |x| > 0 tobuf(max, len, buf, x, precision-1, max_prec, carry); else { // x == 0 - first digit if (*len >= max) return; if (sign) { buf[*len] = '-'; *len = *len + 1; } if (*len+1 <= max && precision >= 0) { buf[*len] = '0'; *len = *len + 1; buf[*len] = '.'; *len = *len + 1; } while (precision-- > 0) { buf[*len] = '0'; *len = *len + 1; if (*len >= max) return; } precision = -1; // don't place another period } if (*len <= max && precision == 0) { buf[*len] = '.'; *len = *len + 1; } // for first and subsequent digits, add the digit to the buffer if (*len >= max) return; if (l_dec < 0) l_dec = 0; buf[*len] = '0' + l_dec; *len = *len + 1; } //______________________________________________________________________ // Convert the value x to a decimal representation stored in a buffer int dbl2buf(size_t max, char *buf, double x, int precision) { const int DECIMALS=15; // max significant digits int max_dec = DECIMALS-(int)(trunc(log10(fabs(x)))+1); double max_prec = pow10(max_dec); // magnitude for precision loss int len = 0; // buffer length init double y = x==0?0:fmod(fabs(x), 1/max_prec); // determine error double l_carry = round(y*max_prec)/max_prec; // error is carried forward if (x != x) { strncpy(buf, "NAN", max); return 0; } if ((x-x) != (x-x)) { strncpy(buf, "INF", max); return 0; } tobuf(max, &len, buf, x, precision-1, max_prec, l_carry); // fill in buffer buf[len] = 0; // terminate buffer return len; // return buffer length used } -- 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.
next prev parent reply other threads:[~2007-09-23 18:54 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 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 [this message] 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=20070923185354.17871.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: linkBe 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).