bug-gmp
[Top][All Lists]
Advanced

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

Re: mpf_cmp


From: Paul Zimmermann
Subject: Re: mpf_cmp
Date: Tue, 26 Nov 2002 12:33:34 +0100

Dear Toine,

   I disagree that the numbers are slightly different in value, the calculation
   is: truncate((1/12 * 1) * 100) / 100
   First part is: (1/12 * 1) * 100) = 8.3333333333333333333333333 .... this is
   a number that cannot be exactly represented with a binary representation.
   I then use the following function to truncate the 8.3333... number, this is
   the source of my truncate function:

[source deleted]

Yes the problem comes from 8/100 (from mpf_div_ui) and 0.08 (from mpf_set_str)
giving different values. But you can't avoid this since you can't control
the way mpf_div_ui and mpf_set_str behave when the input is not exactly
representable. 

   Then I divide the INTEGER value 8 by 100 -> the result is 0.8, I think this
   is a floating-point value that can be exactly represented by a
   floating-point representation like GNUmp uses, its not a rational number
   like 0.83333333333...

No: 8/100 is 2/25 and since the denominator is not a power of two, it is
not exactly representable in binary. Try the following program in C:

   main()
   {
     double x = 0.8;

     printf ("x=%1.30e\n", x);
   }

   lucrezia(zimmerma): ./a.out
   x=8.000000000000000444089209850063e-01

One solution to your problem would be to use the mpfr library (www.mpfr.org)
which enables you to specify rounding directions for each operation. Thus
if you compute 8/100 and "0.08" with the same rounding mode, mpfr *guarantees*
you that it will give you the same internal value (thus mpfr_cmp will give 0).

Paul






reply via email to

[Prev in Thread] Current Thread [Next in Thread]