Float error or a Format error ??

Paul Zimmermann Paul.Zimmermann at loria.fr
Wed Oct 4 13:28:55 CEST 2006

>   x=1.2
> mpf_pow_ui(result,x,22)
> mp_sprintf(stringVar,"%.30Ff",result) 
> the displayed result will be:
> 55.206143891243641727800000000000
> while the true result must be :
> 55.2061438912436417593344
> using gmp-v4.2
> please explain

A possible explanation: whatever precision you use, 1.2 is not exactly
representable in radix 2, which is what mpf uses.

If you used mpf_set_d to set x to 1.2, what you entered in x is not 12/10,
but the closest 53-bit number, i.e. 5404319552844595/2^52.

Use mpf_set_str to get a more accurate string -> mpf conversion.

Together with the roundings done by mpf_pow_ui, this might explain what you

Paul Zimmermann

More information about the gmp-discuss mailing list