Repository navigation
get_initial_step: explain the error when no step was set and no x is given - #651
Merged
Merged
Conversation
NLOPT_GETSET_VEC(initial_step) defined get_initial_step(v) and get_initial_step(), which called nlopt_get_initial_step with x = NULL. Unless a step had been set, nlopt_get_initial_step needs x to compute the default step, so these getters threw std::invalid_argument (stevengj#545). Define only the two setters, and remove the global nlopt_get_initial_step(opt, dx) overload that existed for these getters. get_initial_step(x, dx) and get_initial_step_(x) are unchanged; SWIG already ignored the removed getters. Add a test of the initial-step setters and getters.
Owner
Yes that's the point. If a step has been set, there should be a way to get it, no? |
As discussed in review, a step that was set should stay readable, so restore the getters of NLOPT_GETSET_VEC(initial_step). They already return a step set with set_initial_step; they failed only when no step was set, with "nlopt invalid argument". nlopt_get_initial_step now sets the error message "no initial step was set, and the default initial step depends on x" when x is NULL and no step was set; the C++ getter passes it on in std::invalid_argument. The test now checks that the getters return a set step and that the message explains the failure.
Contributor
Author
|
Yes, agreed. The getters already return a step that was set; they only fail when no step was set, and then with just "nlopt invalid argument". In 2280a63 I restored them, and |
stevengj
approved these changes
Oct 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #545.
get_initial_step(std::vector<double> &v)andget_initial_step()(fromNLOPT_GETSET_VEC(initial_step)) callnlopt_get_initial_stepwithx = NULL. If a step was set withset_initial_step, they return it. If no step was set, the default step depends onx, sonlopt_set_default_initial_step(opt, NULL)returnsNLOPT_INVALID_ARGSwithout a message, and the C++ getters throwstd::invalid_argument("nlopt invalid argument"), which does not say why.After the review, this PR keeps the getters (the first version removed them) and only makes the failure clear:
src/api/options.c: when no step was set andxis NULL,nlopt_get_initial_stepsets the error message "no initial step was set, and the default initial step depends on x". The C++ wrapper passes it on instd::invalid_argument.test/t_initial_step.cxx: the default step fromget_initial_step(x, dx)andget_initial_step_(x), the message ofget_initial_step()when no step was set,get_initial_step()andget_initial_step(v)after both setters, and the dimension check of the setter. On master only the message check fails.The C++ interface and the bindings are unchanged.
ctestwith MSVC 2022 on Windows: 78 of 78 tests pass.