0

I have written a bisection solver with a function name as argument and that function has only one argument:

double Bisection(double(*func)(double), double xLower, double xUpper, double xEPS = 1e-9, double yEPS = 1e-9);

Now I want to apply this solver in a Beta inverse cumulative probability function where the CDF is already known. But this CDF has three argument where the last two arguments are known:

double CDF(double x, double alpha, double beta)

How should I use this CDF as an argument of the solver by reducing the number of arguments?

0

1 Answer 1

1

The first step is to replace your C-style function pointer with a C++ std::function:

double Bisection(std::function<double(double)> func, double xLower, double xUpper, double xEPS = 1e-9, double yEPS = 1e-9);

The implementation will be the same as before. Now you can use std::bind() to call it:

double alpha, beta, xLower, xUpper; // TODO: assign
Bisection(std::bind(CDF, _1, alpha, beta), xLower, xUpper);

What std::bind() does there is to convert your three-argument CDF function into a one-argument (_1) function by binding the second and third arguments to variables.

You can't use std::bind() with your original C-style function pointer, as discussed here: convert std::bind to function pointer

Sign up to request clarification or add additional context in comments.

1 Comment

This is really a great solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.