Templates and typeconversion - question

M

Markus S

Hi,

I guess there is a good reason for this, a function from the Boost
library (which finds the minima of given function) has the following
synopsis:

template <class F, class T>
std::pair<T, T> brent_find_minima(F f, T min, T max, int bits);

Calling it the following way works (inside main, with a top-level
function TrickyFunc):

double min = 1;
double max = 100;
int bits = 50;

std::pair<double, double> result = brent_find_minima(TrickyFunc, min,
max, bits);

Calling it with the arguments directly given, compiles and runs 'fine'
but fails to iterate and just returns the upper boundary (max).

std::pair<double, double> result = brent_find_minima(TrickyFunc, 1, 100, bits);

Below is a compilable example (found on the net).

Thanks,

Markus


#include <boost/math/tools/minima.hpp>

#include <iostream>
#include <cmath>
using namespace std;


// Standard C function
double TrickyFunction(double x)
{
return exp(x) + 0.01/x;
}


int main()
{

// Tricky function
double min = 0.0001;
double max = 1.0;
int bits = 50;
boost::uintmax_t maxIter = 1000;

std::pair<double, double> result =
boost::math::tools::brent_find_minima(TrickyFunction, min, max,
bits);//, maxIter);
cout << "Abscissa, value f(x) Tricky function: " << result.first << ",
" << result.second << endl;

return 0;
}
 
A

Alf P. Steinbach

* Markus S:
Hi,

I guess there is a good reason for this, a function from the Boost
library (which finds the minima of given function) has the following
synopsis:

template <class F, class T>
std::pair<T, T> brent_find_minima(F f, T min, T max, int bits);

Calling it the following way works (inside main, with a top-level
function TrickyFunc):

double min = 1;
double max = 100;
int bits = 50;

std::pair<double, double> result = brent_find_minima(TrickyFunc, min,
max, bits);

Calling it with the arguments directly given, compiles and runs 'fine'
but fails to iterate and just returns the upper boundary (max).

std::pair<double, double> result = brent_find_minima(TrickyFunc, 1, 100,
bits);

Shouldn't compile really, unless there's something I don't know about std::pair.

But anyway, express the arguments as 1.0 and 100.0.

You need the same numeric type as for the result values.


Cheers & hth.,

- Alf
 
M

Markus S

Shouldn't compile really, unless there's something I don't know about
std::pair.

But anyway, express the arguments as 1.0 and 100.0.

You need the same numeric type as for the result values.
You are right about 1.0 and 100.0. I guess I just have to get used to
being pedantic about this. The best premise is probably to assume that
type conversion will not work unless you have verified that it does
work in your particular case.
Oddly enough, it really does compile and run without warnings or errors.
 
V

Victor Bazarov

Alf said:
* Markus S:

Shouldn't compile really, unless there's something I don't know about
std::pair.

You don't know about the templated copy-conversion constructor? Please
see [lib.pairs]/1. Now, why it would "just" return "the upper
boundary", I am not sure (don't know what 'brent...' function's effects
are supposed to be).

I created this:

#include <utility>
#include <iostream>

template <class F, class T>
std::pair<T, T> brent_find_minima(F f, T min, T max, int bits)
{
return std::make_pair(min, max);
}

void TrickyFunc()
{
}

int main()
{
int bits = 0;
std::pair<double, double> result =
brent_find_minima(TrickyFunc, 1, 100, bits);
std::cout << "Result is < " << result.first << " , ";
std::cout << result.second << " >\n";
}

, try it.
But anyway, express the arguments as 1.0 and 100.0.

You need the same numeric type as for the result values.


Cheers & hth.,

- Alf

V
 
A

Alf P. Steinbach

* Victor Bazarov:
Alf said:
* Markus S:

Shouldn't compile really, unless there's something I don't know about
std::pair.

You don't know about the templated copy-conversion constructor? Please
see [lib.pairs]/1.

Thanks. Well nothing surprises me anymore. :)


[snip]

Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top