Date: Wed, 20 Jan 2010 22:13:25 GMT From: Paul Guyot <paulguyot@ieee.org> To: freebsd-gnats-submit@FreeBSD.org Subject: kern/143037: strtod doesn't always round to the nearest Message-ID: <201001202213.o0KMDPY5030953@www.freebsd.org> Resent-Message-ID: <201001202220.o0KMK6hg077066@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 143037
>Category: kern
>Synopsis: strtod doesn't always round to the nearest
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: sw-bug
>Submitter-Id: current-users
>Arrival-Date: Wed Jan 20 22:20:06 UTC 2010
>Closed-Date:
>Last-Modified:
>Originator: Paul Guyot
>Release: 7.1
>Organization:
>Environment:
FreeBSD papaya.local 7.1-RELEASE-p4 FreeBSD 7.1-RELEASE-p4 #0: Fri Apr 17 05:48:23 UTC 2009 root@papaya.local:/usr/obj/usr/src/sys/GENERIC amd64
>Description:
Calling strtod with "2.225073858507201e-308" yields 2^-1022 (0,16,0,0,0,0,0,0), while it should yield 2^-1022 - 2^-1074 (0,15,255,255,255,255,255,255), which is the nearest representable value.
gcc on the very same FreeBSD box rounds this to the proper value (see sample code below).
>How-To-Repeat:
#include <stdio.h>
#include <stdlib.h>
#include <fenv.h>
int main() {
if (fegetround() == FE_TONEAREST) {
printf("rounding to nearest\n");
} else {
printf("not rounding to nearest\n");
}
const char* fp_str = "2.225073858507201e-308";
printf("string = %s\n", fp_str);
double fp_gcc = 2.225073858507201e-308;
printf("gcc = %.20g\n", fp_gcc);
double parsed = strtod(fp_str, NULL);
printf("parsed = %.20g\n", parsed);
double fp = 0.0;
char* fp_p = (char*) &fp;
fp_p[0] = 255;
fp_p[1] = 255;
fp_p[2] = 255;
fp_p[3] = 255;
fp_p[4] = 255;
fp_p[5] = 255;
fp_p[6] = 15;
fp_p[7] = 0;
printf("smaller = %.20g\n", fp);
if (fp == parsed) {
printf("actually rounded to nearest (and towards zero)\n");
}
fp_p[0] = 0;
fp_p[1] = 0;
fp_p[2] = 0;
fp_p[3] = 0;
fp_p[4] = 0;
fp_p[5] = 0;
fp_p[6] = 16;
fp_p[7] = 0;
printf("larger = %.20g\n", fp);
if (fp == parsed) {
printf("actually rounded towards +infinity\n");
}
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001202213.o0KMDPY5030953>
