Re: floating points



yep, john is right 85.3 has no exactly binary representation and this is
a problem in every software/hardware you use. Beside, if what you're
trying to do is to get an exact internal representation of the float try
using two integer variables: one for the integer part and other for the
fractional and parse the user entry looking for the decimal separator
(point or comma) or, easier but not elegant, use two GtkEntry to get
each part of it.

SALUDOS!!!
rtriay
:)


El 01 Mar 2002 12:25:20 +0000, John Cupitt ng-london org uk escribió:
> 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/
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
> 





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