Hi,

as a complement to Torbjörn answer:

1) the decimal string 0.693...875 cannot be represented exactly within 3000
   bits, thus the mpf_set_str() necessarily performs some rounding
2) the mpf_get_str() also has to perform some rounding, since you convert
   a 3000-bit number into a 99-digit decimal string
3) mpf functions do not specify any rounding, see the GMP reference manual:

   Note that the 'mpf' functions are _not_ intended as a smooth
extension to IEEE P754 arithmetic.  In particular results obtained on
one computer often differ from the results on a computer with a
different word size.

   New projects should consider using the GMP extension library MPFR
(<https://www.mpfr.org/>) instead.  MPFR provides well-defined precision
and accurate rounding, and thereby naturally extends IEEE P754.

4) with MPFR, if you tell mpfr_set_str to round up and mpfr_get_str to
   round to nearest, you get:

$ cat f.c
#include <stdio.h>
#include <mpfr.h>

int main()
{
  mpfr_t x;
 mpfr_init2(x,3000);
 
mpfr_set_str(x,"0.6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875",10,MPFR_RNDU);
 char str[300];
 mpfr_exp_t myexp;
 mpfr_get_str(str, &myexp, 10, 99, x, MPFR_RNDN);
 mpfr_clear(x);


printf("%s\n",str);
}

$ gcc f.c -lmpfr
$ ./a.out
693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418688

Best regards,
Paul Zimmermann

PS: followups about MPFR should go on the MPFR list

> From: 赵世忠 <szz...@sei.ecnu.edu.cn>
> Date: Sun, 30 Jul 2023 08:41:43 +0800 (GMT+08:00)
> 
> mpf_t x;
> mpf_init2(x,3000);
> mpf_set_str(x,"0.6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875",10);
> char str[300];
> mp_exp_t myexp;
> mpf_get_str(str, &myexp, 10, 99, x); 
> mpf_clear(x);
> 
> 
> printf("%s\n",str);
> 
> 
> The last digit should be '8', but mpf_get_str outputs '7'.
> 
> 
> 
> 
> 
> 
> _______________________________________________
> gmp-bugs mailing list
> gmp-bugs@gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-bugs
> 
_______________________________________________
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs
  • bug 赵世忠
    • Re: bug Torbjörn Granlund
    • Re: bug Paul Zimmermann

Reply via email to