5

I'm running into problems regarding the usage of the Brobdingnag package - after setting

a2 <- as.brob(0.1)^1000, 

a2 = exp(-2302.6)

a1 <- as.brob(0.1)^800, 

a1 = exp(-1842.1)

I get different results for using sum(a1,a2) and sum(a2,a1) - Each time the result equals the first argument given to the sum function. It seems that maybe sum is not overrided by the Brobdingang package even though its supposed to? Or maybe i'm doing something wrong?

I asked this question also as a reply to another question I wrote, see here

[EDIT: Answer from author of the package]

Hi Dan

This is definitely a bug in the package; thank you for the report! Unfortunately, correcting it will take me some considerable time.

In the meantime, please find below the usual R idiom for calculating the sum of two brobs:

> a1 <- as.brob(0.1)^800
> a2 <- as.brob(0.1)^1000
> a1+a2


> a1 <- as.brob(0.1)^800
> a2 <- as.brob(0.1)^1000


> a1+a2
[1] +exp(-1842.1)
> a2+a1
[1] +exp(-1842.1)

> sum(cbrob(a1,a2))
[1] +exp(-1842.1)
> sum(cbrob(a2,a1))
[1] +exp(-1842.1)
> 
Community
  • 1
  • 1
dan12345
  • 1,594
  • 4
  • 20
  • 30

1 Answers1

2

I can reproduce your issue with the following code. One answer might be to + instead of sum. Added: Another would be to have your data in a vector before doing the sum

> library(Brobdingnag)
> (a1 <- as.brob(0.1)^800) 
[1] +exp(-1842.1)
> (a2 <- as.brob(0.1)^1000)
[1] +exp(-2302.6)
>
> a1 + a2
[1] +exp(-1842.1)
> a2 + a1
[1] +exp(-1842.1)
>
> sum(a1, a2)
[1] +exp(-1842.1)
> sum(a2, a1)
[1] +exp(-2302.6)
> 
> sum(as.brob(0.1)^c(1000,800))
[1] +exp(-1842.1)
> sum(as.brob(0.1)^c(800,1000))
[1] +exp(-1842.1)

It seems to be that you cannot use sum(,) like that. Here are some similar strange results with more practical numbers

> as.brob(0.1) + as.brob(1)           # OK, gives exp(ln(1.1))
[1] +exp(0.09531)     
> as.brob(1) + as.brob(0.1)           # OK, gives exp(ln(1.1))
[1] +exp(0.09531)
> sum(as.brob(c(0.1, 1)))             # OK, gives exp(ln(1.1))
[1] +exp(0.09531)
> sum(as.brob(c(1, 0.1)))             # OK, gives exp(ln(1.1))
[1] +exp(0.09531)
>
> sum(as.brob(0.1), as.brob(1))       # not OK, gives first term exp(ln(0.1))
[1] +exp(-2.3026)
> sum(as.brob(1), as.brob(0.1))       # not OK, gives first term exp(ln(1))
[1] +exp(0)
Henry
  • 6,704
  • 2
  • 23
  • 39
  • Weird - I sent an email to the creator of the package, lets see if he answers. Using the + sign instead of a sum is a problem, as i have a matrix of brobs and i want to sum its rows ( many times ) , and this would mean i would have to add loops of '+' just to get the sum - not horrible, but kinda ugly – dan12345 Apr 29 '11 at 15:25
  • @user206903: A matrix should not be a problem. For example `sum(as.brob(0.1)^c(1000,800))` and `sum(as.brob(0.1)^c(800,1000))` both give the same (I think correct) result. I will add this above – Henry Apr 29 '11 at 15:38