Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max of empty IntegerVector gives large negative number #883

Closed
jackwasey opened this Issue Jul 20, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@jackwasey
Copy link
Contributor

jackwasey commented Jul 20, 2018

Not sure if this is designed to be somewhat consistent with R (which gives -Inf and a warning for max(integer(0)).

Rcpp::cppFunction('int getiv() { IntegerVector IV = IntegerVector::create(); return max(IV); }')
getiv()
# [1] -368909088

Don't know where the number came from, other than perhaps a random memory location on my machine. If it is picking random memory locations, I guess it could cause some kind of memory access violation? I don't see an obviously correct choice of output in this case.

@eddelbuettel

This comment has been minimized.

Copy link
Member

eddelbuettel commented Jul 20, 2018

Agreed -- max() and min() do not seem to check correctly for empty vectors right now, and the returned value appears to be a random value each time.

edd@rob:~/git/rcpp(master)$ Rscript -e 'Rcpp::cppFunction("int getiv() { IntegerVector IV = IntegerVector::create(); return max(IV); }"); getiv()'
[1] 955757120
edd@rob:~/git/rcpp(master)$ Rscript -e 'Rcpp::cppFunction("int getiv() { IntegerVector IV = IntegerVector::create(); return max(IV); }"); getiv()'
[1] -376245696
edd@rob:~/git/rcpp(master)$ 

eddelbuettel added a commit that referenced this issue Jul 21, 2018

Merge pull request #884 from RcppCore/bugfix/min_max_zero
consider empty vectors in min() and max() (fixes #883)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.