Inaccurate/inconsistent output from mpf_out_str

paul zimmermann Paul.Zimmermann at inria.fr
Tue Oct 6 12:10:21 UTC 2015


       Dear Yihang,

note that if you use MPFR (mpfr.org) with the program below, you will get
on a 64-bit computer:

2.628978819e208987640249978733
2.6289788186792204674075064891600428077435502009263e208987640249978733
2.628978818679220467407506489160042807743550200926299545175582219560661239284074832295595964174891630e208987640249978733

where all digits agree (I added the mpfr_set_emax call otherwise one gets an
overflow with the default MPFR exponent range).

Best regards,
Paul Zimmermann

// gcc sample.c -lmpfr -lgmp
#include <stdio.h>
#include <gmp.h>
#include <mpfr.h>
#include <mpf2mpfr.h>                     /* added */
#define PRECISION 1024

int main() {
  unsigned long n = 1000000000000000000;
  mpf_t result, r, s, root5;

  mpfr_set_emax (mpfr_get_emax_max ());   /* added */
  mpf_init2(root5, PRECISION);
  mpf_sqrt_ui(root5, 5);

  mpf_init2(r, PRECISION);
  mpf_add_ui(r, root5, 1);
  mpf_div_2exp(r, r, 1);
  mpf_pow_ui(r, r, n);

  mpf_init2(s, PRECISION);
  mpf_ui_sub(s, 1, root5);
  mpf_div_2exp(s, s, 1);
  mpf_pow_ui(s, s, n);

  mpf_init2(result, PRECISION);
  mpf_sub(result, r, s);
  mpf_div(result, result, root5);

  mpf_out_str(stdout, 10, 10, result);

  putchar('\n');

  mpf_out_str(stdout, 10, 50, result);

  putchar('\n');

  mpf_out_str(stdout, 10, 100, result);

  return 0;
}


> From: Yihang Ho <hoyihang5 at gmail.com>
> Date: Sun, 13 Sep 2015 02:55:33 +0000
> 
> Dear all,
> 
> In one of my programs, the output of mpf_out_str is not as accurate as it
> should be.
> 
> Consider this piece of code: http://git.io/vZKCz
> 
> This code computes the n-th Fibonacci number using Binet's formula. The
> last few lines merely print the results to 10, 50, and 100 digits. However,
> the outputs (http://git.io/vZKCg) are not consistent with one another. As
> you can see, only the first 5 digits from the first line are the same as
> the second line while only the first 44 digits from the second line are the
> same the third line. I believe this is a bug as it is not documented
> anywhere.
> 
> I am able to replicate this problem on OS X and Ubuntu.
> 
> Details about my systems:
> 
> - OS X: http://git.io/vZKCr
> - Ubuntu: http://git.io/vZKCo
> 
> Do let me know if any additional info are needed.
> 
> Best,
> Yihang


More information about the gmp-bugs mailing list