mpq_get_d() rounding for different platforms?

Paul Zimmermann Paul.Zimmermann at loria.fr
Tue Mar 17 08:07:17 CET 2009


       Dear Kish,

> I am trying to port our system (the constraint logic programming 
> platform ECLiPSe) to 64 bit x86-64 Windows, using MinGW-W64. Our system 
> uses GMP to implement large integers and rationals, and I have run into 
> some issues with rounding of rationals to double (using mpq_get_d()).
> 
> I am using GMP 4.2.2, because I believe is the GMP version used by 
> MinGW-W64 itself. The issue I have ran into is that in some cases, the 
> MinGW-W64 version of GMP returns a slightly different double from the 32 
> bit Linux version of the same GMP. For example, for the rational 4/5,
> the MinGW version returns 0.79999999999999993, while the Linux version 
> returns 0.80000000000000004.

clearly the Linux version is wrong according to the GMP manual:

 -- Function: double mpq_get_d (mpq_t OP)
     Convert OP to a `double', truncating if necessary (ie. rounding
     towards zero).

> Looking at the manual, it seems that from GMP 4.2 onwards, mpq_get_d() 
> should round towards zero, and this suggest to me that 
> 0.79999999999999993 should be returned, for both platforms (they have 
> the same representation for double, I believe). Should the two platforms 
> return different values, and is there anyway I can get them to return 
> the same value on platforms with the same double representation?
> 
> Thanks in advance for any information!
> 
> Kish Shen

another possibility for you is to use the mpfr_set_q function from MPFR:

 -- Function: int mpfr_set_q (mpfr_t ROP, mpq_t OP, mp_rnd_t RND)

With rounding to zero, you will get 0.79999999999999993 on any platform,
and with rounding to +Infinity, you will get 0.80000000000000004.

Paul Zimmermann


More information about the gmp-discuss mailing list