Skip site navigation (1)Skip section navigation (2)
Date:      16 Jan 2003 18:33:54 -0800
From:      swear@attbi.com (Gary W. Swearingen)
To:        Ugen <ugen@xonix.com>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: printf prints  WRONG double precision values
Message-ID:  <7cel7csdwt.l7c@localhost.localdomain>
In-Reply-To: <3E272874.8060706@xonix.com>
References:  <3E272874.8060706@xonix.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Ugen <ugen@xonix.com> 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?7cel7csdwt.l7c>