Bioconductor / GenomicRanges

Representation and manipulation of genomic intervals
https://bioconductor.org/packages/GenomicRanges
43 stars 18 forks source link

Strange integer overflow issue #63

Closed gevro closed 2 years ago

gevro commented 2 years ago

Hi, I'm getting the below strange overflow issue.

> sum(width(fwd.granges[include==FALSE]))
[1] 1427398577
> sum(width(rev.granges[include==FALSE]))
[1] 1427397578
> sum(width(fwd.granges[include==FALSE])) + sum(width(rev.granges[include==FALSE]))
[1] NA
Warning message:
In sum(width(fwd.granges[include == FALSE])) + sum(width(rev.granges[include ==  :
  NAs produced by integer overflow
> 1427398577 + 1427397578
[1] 2854796155

As you can see, the value of each of the two terms is coming out fine. And when summing those numbers manually. But for some reason, when I sum the terms, I get an error.

This seems like a deep bug in GenomicRanges and/or R?

Also, this seems to work:

> sum(width(fwd.granges[include==FALSE]),width(rev.granges[include==FALSE]))
[1] 2854796155
mtmorgan commented 2 years ago

1427398577 is a 'double' value, whereas 1427398577L (trailing L) is an 'integer'.

If you add the two numbers as integers, they overflow

> 1427398577L + 1427397578L
[1] NA
Warning message:
In 1427398577L + 1427397578L : NAs produced by integer overflow

but not as doubles, as you show, or when sum() is applied to an integer vector, as you also show.

width() of a GRanges returns an integer value, and sum() of integer values returns integer values if the inputs are integer and don't overflow. So the NA in your original code arises because you're adding two integer values

gevro commented 2 years ago

Interesting. I don’t quite understand why R doesn’t recognize an overflow and then convert to double automatically.