gmp 4.2.4 - mpf_add two large negative floats - strange result

Sisyphus sisyphus1 at optusnet.com.au
Thu Jan 22 00:43:05 CET 2009


----- Original Message ----- 
From: "Bruce M. Axtens" <bruce.axtens at gmail.com>
To: <gmp-discuss at swox.com>
Sent: Wednesday, January 21, 2009 6:36 PM
Subject: gmp 4.2.4 - mpf_add two large negative floats - strange result


> G'day everyone,
>
> I'm summing two negative floats:
>
>    char * lhs = "-2234.6016114467412141";
>    char * rhs = "-4939600281397002.2812";
>
> According to Perl, using bignum and Math::BigFloat, the answer is
>
>    -4939600281399236.8828114467412141
>
> However, according to GMP, using the code below, the answer is
>
>    -4939600281399236.88281
>
> Where have I gone wrong? What happened to the remaining "14467412141"?
>

Math::BigFloat automatically extends its precision to whatever is needed, 
but not so with GMP (mpf) - if you want other than 53 bits of precision for 
the mantissa then you need to specify exactly how many you want.

In this instance, 104 bits seems to be sufficient:

C:\_32>perl -MMath::GMPf=":mpf" -le "print 
Math::GMPf->new('-2234.6016114467412141') + 
Math::GMPf->new('-4939600281397002.2812')"
-0.493960028139923688281e16

C:\_32>perl -MMath::GMPf=":mpf" -le "Rmpf_set_default_prec(104);print 
Math::GMPf->new('-2234.6016114467412141') + 
Math::GMPf->new('-4939600281397002.2812')"
-0.49396002813992368828114467412141e16

See http://gmplib.org/manual/Initializing-Floats.html#Initializing-Floats .

Cheers,
Rob 



More information about the gmp-discuss mailing list