Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sources DOT redhat DOT com Delivered-To: mailing list cygwin AT sources DOT redhat DOT com Message-ID: <3B9D2BF5.5060100@ece.gatech.edu> Date: Mon, 10 Sep 2001 17:09:09 -0400 From: Charles Wilson User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2) Gecko/20010713 X-Accept-Language: en-us MIME-Version: 1.0 To: Marco Craveiro CC: cygwin AT cygwin DOT com Subject: Re: atof returns a value that is slightly greater than the original string References: <1000153353 DOT 1101 DOT 8 DOT camel AT darkroom> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Marco Craveiro wrote: > Hello Cygwinners, > > I'm having some strange problems with atof. basically, it returns a > converted value that is 0.000024 bigger than the one on the string. Welcome to the wonderful world of floating point representation. This type of error is quite common with floats -- because the float format is intrinsically incapable of representing every real number with infinite precision. Unfortunately, that leads to "roundoff" errors that are a bit non-intuitive, like your 0.000024. (It's actually rounding off to a nice "round" number -- in the bitshifted, base-2, fixed-point notation used for the mantissa in the float data format. It just doesn't look like a roundoff error when printed out in "normal" base-10 notation). Workarounds: use a bigger float. (double? long double?) You'll still have this sort of problem, but you'll only be off by 0.00000000000xx instead of 0.000024. Or used fixed-point math (e.g. what the previous poster suggested). --Chuck -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/