# Exact division and mpf

Elias P. TSIGARIDAS et at di.uoa.gr
Mon Nov 21 13:35:53 CET 2005

```Torbjorn Granlund wrote:
> Paul.Zimmermann at loria.fr (Paul Zimmermann) writes:
>
>      From: "Elias P. TSIGARIDAS" <et at di.uoa.gr>
>
>      Can anyone tell me if I can assume exact division for mpf_class?
>
>      For example can I assume that
>      mpf_class( 6) / mpf_class( 3)
>      will return mpf_class( 2) ?
>
>   I don't know for mpf, but mpfr guarantees correct rounding, which implies
>   that if a division is exact, you get the exact quotient. See mpfr.org for
>   further details.
>
> Let me add that it also requires that that exact quotient fits
> into the declared mantissa precision.  When dividing 6 by 3, you
> will always get 2, but dividing 6000000000 by 3 needs 31 bits,
> which will not be the case if the quotient variable was
> initialized using mpfr_init2(q,30);
>
> (Under similar conditions, also mpf does what you want.)

Thank you Torbjorn,
Unfortunately I learned this the hard way!
It took me the whole Sunday to figure out why I didn't get the correct
result for a division like the one you discribed.
So the first thing to do in order to use a library is to read the manual
first :-))

Actually what I am doing is this:
I have some repeated operations with integers.
I figured out that if I use mpf instead of mpz
then my program is 2 times faster (at least),
of course I have to adopt the precision at every step, but you can not
have it all :-)

My operations involve exact division.
When I eliminate these divisions then my program is 4 times faster.
Thus it seems that the division is really slow compare to multiplication
at least when you use mpf.
Currently I am trying to make my program work with mpfr.

I will post my results in this maillist, if anyone is interesting

Best regards

--Elias

```

More information about the gmp-discuss mailing list