Skip to content

Commit

Permalink
BUG FIX: Using "AS_INTEGER" coerces the value to int and nlopt_srand(…
Browse files Browse the repository at this point in the history
…) needs a long. Instead, pass as a real and cast to long in C.
  • Loading branch information
aadler committed Jul 2, 2024
1 parent 8e046be commit 0a67e26
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is a patch (pre) release. It includes
* Correcting some of the unit tests in `test-global-wrapper`.
* Update the code in `nloptr.c` for compatibility with the `R` API, efficiency,
and formatting.
* Bugfix: `ranseed` expects an unsigned long but was passed as an integer, thus
reducing the range of random seeds. It is now passed as a double and converted
to a long.

# nloptr 2.1.1

Expand Down
5 changes: 3 additions & 2 deletions src/nloptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "nloptr.h"

#define AS_INTEGER(x) Rf_coerceVector(x, INTSXP)
#define AS_REAL(x) Rf_coerceVector(x, REALSXP)

// Extracts element with name 'str' from R object 'list' & returns that element.
SEXP getListElement (SEXP list, char *str) {
Expand Down Expand Up @@ -745,8 +746,8 @@ nlopt_opt getOptions(SEXP R_options, int num_controls, int *flag_encountered_err
Rprintf("Error: nlopt_set_vector_storage returned NLOPT_INVALID_ARGS.\n");
}

SEXP R_opts_ranseed = PROTECT(AS_INTEGER(getListElement(R_options, "ranseed")));
unsigned long ranseed = asInteger(R_opts_ranseed);
SEXP R_opts_ranseed = PROTECT(AS_REAL(getListElement(R_options, "ranseed")));
unsigned long ranseed = REAL(R_opts_ranseed)[0];
// Set random seed if ranseed > 0. By default a random seed is generated from
// system time.
if (ranseed > 0) {
Expand Down

0 comments on commit 0a67e26

Please sign in to comment.