0

The help documentation of the Rmpfr R package claims that the .bigq2mpfr() function uses the minimal precision necessary for correct representation when the precB argument is NULL :

Description:

     Coerce from and to big integers (‘bigz’) and ‘mpfr’ numbers.

     Further, coerce from big rationals (‘bigq’) to ‘mpfr’ numbers.

Usage:

     .bigz2mpfr(x, precB = NULL)
     .bigq2mpfr(x, precB = NULL)
     .mpfr2bigz(x, mod = NA)

Arguments:

       x: an R object of class ‘bigz’, ‘bigq’ or ‘mpfr’ respectively.

   precB: precision in bits for the result.  The default, ‘NULL’, means
          to use the _minimal_ precision necessary for correct
          representation.

However when converting 31/3 one gets a bad approximation:

> x <- as.bigq(31,3)
> .bigq2mpfr(x)
1 'mpfr' number of precision  8   bits 
[1] 10.31 

By looking inside the .bigq2mpfr() function we see the detailed procedure:

N <- numerator(x)
D <- denominator(x)
if (is.null(precB)) {
    eN <- frexpZ(N)$exp
    eD <- frexpZ(D)$exp
    precB <- eN + eD + 1L
}
.bigz2mpfr(N, precB)/.bigz2mpfr(D, precB)

Firstly I do not understand why precB is taken as follows. The exp output of the frexpZ() is the exponent in binary decomposition:

> frexpZ(N)
$d
[1] 0.96875

$exp
[1] 5

> 0.96875*2^5
[1] 31

Here we get precB=8 and the result is then identical to:

> mpfr(31, precBits=8)/mpfr(3, precBits=8)
1 'mpfr' number of precision  8   bits 
[1] 10.31

I am under the impression one should rather replace precB with 2^precB but I'd like to get some advices about that:

> mpfr(31, precBits=8)/mpfr(3, precBits=2^8)
1 'mpfr' number of precision  256   bits 
[1] 10.33333333333333333333333333333333333333333333333333333333333333333333333333329
> mpfr(31, precBits=8)/mpfr(3, precBits=2^9)
1 'mpfr' number of precision  512   bits 
[1] 10.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333329
> mpfr(31, precBits=8)/mpfr(3, precBits=2^7)
1 'mpfr' number of precision  128   bits 
[1] 10.33333333333333333333333333333333333332
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • What is the precision produced by your first line ( `x <- as.bigq(31,3)` ) ? I ask because the converter is designed not to increase the precision over that given to it. – Carl Witthoft Jan 13 '13 at 14:28
  • @CarlWitthoft I don't understand your question. `as.bigq(31,3)` is the rational number with numerator 31 and denominator 3. There's no precision involved. – Stéphane Laurent Jan 13 '13 at 14:58
  • Well, that's probably the problem. I would start by making sure you get the expected results for the examples on the help page and then follow that sequence with 31/3 – Carl Witthoft Jan 13 '13 at 18:48

2 Answers2

1

I get (note the difference in my initial creation):

Rgames> fooq<-as.bigq(31/3)
Rgames> fooq
Big Rational ('bigq') :
[1] 5817149518686891/562949953421312
Rgames> .bigq2mpfr(fooq)
1 'mpfr' number of precision  104   bits 
[1] 10.3333333333333339254522798000835

All this strongly suggest to me that the precision in your bigq number is in fact zero decimal places, i.e. each of "31" and "3" has that precision. As such, your mpfr conversion is quite correct in giving you a result with one decimal place precision.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Ok but `fooq` is not the rational number 31/3 here. I set your answer as accepted but now I wonder what are the good practices with the combination of `gmp` and `Rmpfr`. Thanks ! – Stéphane Laurent Jan 14 '13 at 20:01
  • **(Update 22/02/2014)** @Carl As I said this is not correct because your `fooq` is not the rational number 31/3. See my new answer, this works with the latest version of the package. – Stéphane Laurent Feb 22 '14 at 15:08
1

This has been corrected in a newer version of the package:

> x <- as.bigq(31,3)
> .bigq2mpfr(x)
1 'mpfr' number of precision  128   bits 
[1] 10.33333333333333333333333333333333333332
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Confirmed here. THanks for letting me know. But be aware that `as.bigq(31,3)` is NOT the same as `as.bigq(31/3) #5817149518686891/562949953421312` . So be cautious in your code. – Carl Witthoft Feb 22 '14 at 15:34
  • @CarlWitthoft Yes, this is what I said: I **want** the rational number with numerator 31 and denominator 3, so it is `as.bigq(31,3)` and *not* `as.bigq(31/3)` – Stéphane Laurent Feb 22 '14 at 16:23