Re: [R] gmp::bigq vs. MASS::fractions

2023-01-11 Thread Sigbert Klinke

Hello,

Am 07.01.23 um 17:55 schrieb Ivan Krylov:
> What do you need the fractions for?
>

When writing tasks for statistics exams, I try to avoid non-terminal 
intermediate results. Students might round them and arrive at a final 
result that might be slightly different from the true value. Although 
the exam package supports a tolerance for checking the result, this 
could be problematic. To check if a number is non-terminal, I calculate 
a fraction representation and check if the denominator can be expressed 
by 2^p 5^q.


@Enrico: When I started developing assignments a few years ago, I was 
not familiar with gmp. :(


Thanks Sigbert

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] gmp::bigq vs. MASS::fractions

2023-01-09 Thread Enrico Schumann
On Sat, 07 Jan 2023, Sigbert Klinke writes:

> Hi,
>
> has someone experience which routine should be used for
> creating fractional numbers? The two conversion
> routines deliver different results
>
>> x <- (0:7)/7
>
>> MASS::fractions(x)
>
> [1]   0 1/7 2/7 3/7 4/7 5/7 6/7   1
>
>> gmp::as.bigq(x)
>
> Big Rational ('bigq') object of length 8:
>
> [1] 0 2573485501354569/18014398509481984
> 2573485501354569/9007199254740992
>
> [4] 7720456504063707/18014398509481984
> 2573485501354569/4503599627370496
> 6433713753386423/9007199254740992
>
> [7] 7720456504063707/9007199254740992  1
>
> Following the example I would compute my fractional
> numbers with MASS::fractions and store them for further
> processing as Big Rational.
>
> Thanks Sigbert
>

'gmp' allows you to create the fractions directly:

gmp::as.bigq(n = 0:7, d = 7)
## Big Rational ('bigq') object of length 8:
## [1] 0   1/7 2/7 3/7 4/7 5/7 6/7 1  



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] gmp::bigq vs. MASS::fractions

2023-01-07 Thread Ivan Krylov
On Sat, 7 Jan 2023 17:29:35 +0100
Sigbert Klinke  wrote:

>  > x <- (0:7)/7  
> 
>  > MASS::fractions(x)  
> 
> [1]   0 1/7 2/7 3/7 4/7 5/7 6/7   1
> 
>  > gmp::as.bigq(x)  
> 
> Big Rational ('bigq') object of length 8:
> 
> [1] 0 
> 2573485501354569/18014398509481984 2573485501354569/9007199254740992
> 
> [4] 7720456504063707/18014398509481984
> 2573485501354569/4503599627370496 6433713753386423/9007199254740992
> 
> [7] 7720456504063707/9007199254740992  1

That's a consequence of the way fractions are internally represented
inside most modern processors. In short, gmp::as.bigq takes the red
pill and shows you the reality as it is (all fractions have a limited
number of significant digits -- 52 binary digits -- and are required to
have a denominator that's a power of two), while MASS::fractions tries
to guess what the original number might have been before an imprecise
approximation of k/7 had been computed. (Since 7 is not a power of 2,
only 0 and 1 have an exact representation in your example.)

For more information, see R FAQ 7.31:
https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

What do you need the fractions for?

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] gmp::bigq vs. MASS::fractions

2023-01-07 Thread Sigbert Klinke

Hi,

has someone experience which routine should be used for creating 
fractional numbers? The two conversion routines deliver different results


> x <- (0:7)/7

> MASS::fractions(x)

[1]   0 1/7 2/7 3/7 4/7 5/7 6/7   1

> gmp::as.bigq(x)

Big Rational ('bigq') object of length 8:

[1] 0 
2573485501354569/18014398509481984 2573485501354569/9007199254740992


[4] 7720456504063707/18014398509481984 2573485501354569/4503599627370496 
 6433713753386423/9007199254740992


[7] 7720456504063707/9007199254740992  1

Following the example I would compute my fractional numbers with 
MASS::fractions and store them for further processing as Big Rational.


Thanks Sigbert

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.