Skip to content

Calculate power for int64 exponent precisely#289

Open
hcirellu wants to merge 5 commits intomainfrom
power64
Open

Calculate power for int64 exponent precisely#289
hcirellu wants to merge 5 commits intomainfrom
power64

Conversation

@hcirellu
Copy link
Copy Markdown
Collaborator

I added a precise calculation for POW64 with an overflow detection.

The example from #288 now returns the correct values.

x = as.integer64(c("94906267", "2147483650"))
> x^2L
integer64
[1] 9007199515875289    4611686027017322500

The results for double exponent are unchanged, i.e.:

x = as.integer64(c("94906267", "2147483650"))
> x^2
integer64
[1] 9007199515875288    4611686027017322496

In addition, the new implementation is faster, too.

x = as.integer64(sample(-100:100,rep=T,1000000))
y32 = 0:9
yd = as.double(y32)
microbenchmark::microbenchmark(x^y32, x^yd)
# Unit: milliseconds
#   expr       min        lq     mean    median        uq      max neval
#  x^y32  6.262101  6.566502 13.84232  6.816252  7.256201 178.3225   100
#   x^yd 27.294501 27.965501 30.46499 28.709051 31.592151  66.1929   100

Closes #288

@hcirellu hcirellu marked this pull request as ready for review March 11, 2026 11:49
@MichaelChirico MichaelChirico added this to the 4.8.0 milestone Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Power of integer64 is not precise

2 participants