Re: floating points



Lourdes Maldonado wrote:
> I have an entry field where the user enters a string that should 
> correspond to a floating point number (an FM frequency).  When I 
> retrieve the string from the entry field and convert it (using strtod) 
> to a floating point, the number does not come out exact (i.e. if I enter 
> the string 85.3 the floating point number I get is 85.299995).

Hi Lourdes, this is because 85.3 does not have an exact binary 
representation ... it's a recurring fraction. There's no bug, you really 
are getting the most accurate double representation of 85.3, it just 
looks a bit ugly.

You can make it prettier by reducing the number of digits you display 
when you send the result back to the user. For example:

printf( "%.4g\n", 85.3 );

will print "85.3".

John



========================================================== 
Aelbert Cuyp 13 February - 12 May 2002 

For information and tickets: 
http://www.nationalgallery.org.uk/exhibitions/cuyp/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]