question about mpf_mul_ui

Paul Zimmermann Paul.Zimmermann at loria.fr
Tue Feb 23 09:09:23 CET 2010


       Hi,

> Date: Mon, 22 Feb 2010 18:21:51 +0800
> From: "ppmsn2005" <ppmsn2005 at gmail.com>
> 
> hi all
>     i had wrote some simple code, i found mpf lost data.
> 
>   code like this
> 
>         mpf_t   t6, t7, t8,t9 ;  
>  
>         mpf_init(t6);
>         mpf_init(t7);
>         mpf_init(t8);
>         mpf_init(t9);
> 
> 
> 
>         mpf_set_str(t6,"6378137", 10);
>         mpf_set_str(t7,"3.1415926", 10);
> 
>         mpf_mul(t8, t6, t7);
>         gmp_printf("1 result:  %40.25Ff\n", t8);
>         mpf_set(t6, t8);
>         gmp_printf("2 result:  %40.25Ff\n", t6);
>         //mpf_set_str(t9,"2", 10);
>         //mpf_mul(t8, t6, t9);
>         mpf_mul_ui(t8, t8, 2);
>         gmp_printf("3 result:  %40.25Ff\n", t8);
> 
> 
> but the result is
> 
> 1 result:        20037508.0009862000000000000000000
> 2 result:        20037508.0009862000000000000000000
> 3 result:        40075016.0019723999999000000000000
> 
> i think 3 result is wrong , it should be 40075016.001972400000000000000. if i use double type. i also can get 
> the right result. so how can i fix this bug? thanks

this is not a bug: mpf as most libraries works in radix 2, and t7 is not
exactly representable in radix 2 (like 0.1 for example). See also
http://www.mpfr.org/faq.html#accuracy for a related issue.

Paul Zimmermann


More information about the gmp-discuss mailing list