Commit cc86575c authored by Martin Maechler's avatar Martin Maechler
Browse files

Arithmeti <bigz> o <bigq> now mostly works (old TODO!)

parent e41670bf
......@@ -18,28 +18,62 @@ setGeneric("asNumeric", useAsDefault = function(x) {
#
#----------------------------------------------------------
"+.bigz" <- add.bigz <- function(e1, e2) .Call(biginteger_add, e1, e2)
"+.bigz" <- add.bigz <- function(e1, e2) {
if(inherits(e2, "bigq"))
.Call(bigrational_add, e1, e2)
else .Call(biginteger_add, e1, e2)
}
"-.bigz" <- sub.bigz <- function(e1, e2=NULL)
{
if(is.null(e2))
.Call(biginteger_sub, 0, e1)
else
.Call(biginteger_sub, e1, e2)
else if(inherits(e2, "bigq"))
.Call(bigrational_sub, e1, e2)
else .Call(biginteger_sub, e1, e2)
}
"*.bigz" <- mul.bigz <- function(e1, e2) .Call(biginteger_mul, e1, e2)
"*.bigz" <- mul.bigz <- function(e1, e2) {
if(inherits(e2, "bigq"))
.Call(bigrational_mul, e1, e2)
else .Call(biginteger_mul, e1, e2)
}
## divq : integer division
"%/%.bigz" <- divq.bigz <- function(e1, e2) .Call(biginteger_divq, e1, e2)
"%/%.bigz" <- divq.bigz <- function(e1, e2) {
if(inherits(e2, "bigq")) {
if(is.whole(e2))
e2 <- as.bigz(e2)
else
stop("In 'n %/% d', d must be integer")
}
.Call(biginteger_divq, e1, e2)
}
## div : division of integers -> either rational or (mod) integer division
"/.bigz" <- div.bigz <- function(e1, e2) .Call(biginteger_div, e1, e2)
"/.bigz" <- div.bigz <- function(e1, e2) {
if(inherits(e2, "bigq"))
.Call(bigrational_div, e1, e2)
else .Call(biginteger_div, e1, e2)
}
"%%.bigz" <- mod.bigz <- function(e1, e2) .Call(biginteger_mod,e1, e2)
"%%.bigz" <- mod.bigz <- function(e1, e2) {
if(inherits(e2, "bigq")) {
if(is.whole(e2))
e2 <- as.bigz(e2)
else
stop("In 'n %% d', d must be integer")
}
.Call(biginteger_mod, e1, e2)
}
"^.bigz" <- pow.bigz <- function(e1, e2,...) .Call(biginteger_pow,e1, e2)
"^.bigz" <- pow.bigz <- function(e1, e2,...) {
if(inherits(e2, "bigq"))
pow.bigq(e1, e2)
else .Call(biginteger_pow, e1, e2)
}
##' Inverse: inv(a,b) := (1 / a) (modulo b)
inv.bigz <- function(a,b,...) .Call(biginteger_inv,a,b)
gcd <- function(a,b)
......
......@@ -7,19 +7,7 @@
** TODO completely *wrong* pmin() and pmax(<bigq>, <number>): uses Numerator in comparison ???
*** TODO: Find the *underlying* reason where pmin() goes wrong w/o warning!
** TODO Not working Arithmetic
*** TODO as.bigz(1:4) + as.bigq(7) fails, so does '*', '/', etc
--> use S4 setMethod() on *group* 'Arith' as in ~/R/Pkgs/Rmpfr/R/Arith.R
--> "done" in R/mixed-ops.R.~experi~
BUT tried and gave up <bigz> o <bigq> for now:
To use S4 method, would need S4 (or setOldClass) class.
See ~/R/MM/MISC/S4-dispatch-on-S3.R :
Also the setOldClass(.) only correctly dispatches when objects are created by
new("big*") ---> would need to do this not only in as.bigz(), as.bigq(),
but more importantly from C++ create_SEXP()
--> create_SEXP(const bigvec& v) in ./src/bigintegerR.cc ,
where currently simply the class attribute is set
*** DONE as.bigz(1:4) + as.bigq(7) failed, so do '*', '/', ... Fixed, using the +.bigz, etc _S3_ methods
*** TODO bigq %% and %/% are not even defined (but are for 'numeric')!
*** DONE more checking: q <- 2.3; Fn <- gmp:::`^.bigq`; Fn(q,q) silently gave wrong result; now a good error message
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment