From owner-freebsd-questions Thu Jan 16 18:35:14 2003 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3754037B401 for ; Thu, 16 Jan 2003 18:35:13 -0800 (PST) Received: from sccrmhc03.attbi.com (sccrmhc03.attbi.com [204.127.202.63]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8095443F13 for ; Thu, 16 Jan 2003 18:35:12 -0800 (PST) (envelope-from swear@attbi.com) Received: from localhost.localdomain ([12.242.158.67]) by sccrmhc03.attbi.com (sccrmhc03) with ESMTP id <20030117023511003005ubcde>; Fri, 17 Jan 2003 02:35:11 +0000 Received: from localhost.localdomain (localhost [127.0.0.1]) by localhost.localdomain (8.12.6/8.12.5) with ESMTP id h0H2Y5m9031833; Thu, 16 Jan 2003 18:34:05 -0800 (PST) (envelope-from swear@attbi.com) Received: (from jojo@localhost) by localhost.localdomain (8.12.6/8.12.5/Submit) id h0H2XsGh031828; Thu, 16 Jan 2003 18:33:54 -0800 (PST) (envelope-from swear@attbi.com) X-Authentication-Warning: localhost.localdomain: jojo set sender to swear@attbi.com using -f To: Ugen Cc: freebsd-questions@FreeBSD.ORG Subject: Re: printf prints WRONG double precision values References: <3E272874.8060706@xonix.com> From: swear@attbi.com (Gary W. Swearingen) Date: 16 Jan 2003 18:33:54 -0800 In-Reply-To: <3E272874.8060706@xonix.com> Message-ID: <7cel7csdwt.l7c@localhost.localdomain> Lines: 23 User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Cuyahoga Valley) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Ugen writes: > double pi = 3.14159265358979323846 ; > printf("%20.19f\n", pi); > and here is what you get: > host: [16:45] [127] /tmp>./test > 3.1415926535897931160 Function "printf" is only working with the 64 bits of info in variable "pi" not the more-than-64 bits of data represented by the string on the first line. The function might be giving the value of the variable to as much precision as you ask for, but it probably just gives randomish numbers after about 16 (?) digits. I did a little test which didn't mean much since I don't know the details of the floating point bits, but you might find the "bc" interesting. (I changed your printf's format.) $ ./a.out 3.14159265358979311599796346854 $ echo "scale=29; 4*a(1)" | bc -l 3.14159265358979323846264338324 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message