develooper Front page | perl.perl5.porters | Postings from October 2003

Re: [perl #24092] sprintf problem

Thread Previous | Thread Next
John Peacock
October 6, 2003 12:23
Re: [perl #24092] sprintf problem
Message ID:
Nick Ing-Simmons wrote:
> But:
> perl -e "print sprintf('%.20f', '1.045')"
> 1.04499999999999992895
> The boundary is drawn an 5 in next digit down so that falls under it.

I don't know if you should necessarily draw that conclusion.  There no generic 
way to know when the actual binary representation is inexact (like it is in this 
case).  In any case, your code is subtly different, since you are passing a 
string, which Perl then converts to a number.  There is frequently a difference 
between an asserted and a calculated floating point number.

The moral of the story is when using floating point numbers and requiring 
significant accuracy, Math:::BigFloat is a much better choice than any general 
floating point library.


John Peacock
Director of Information Research and Technology
Rowman & Littlefield Publishing Group
4720 Boston Way
Lanham, MD 20706
301-459-3366 x.5010
fax 301-429-5747

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About