Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Templates and typeconversion - question

0 views
Skip to first unread message

Markus S

unread,
Jul 28, 2009, 12:45:29 PM7/28/09
to
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;
}


Alf P. Steinbach

unread,
Jul 28, 2009, 12:51:14 PM7/28/09
to
* 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

Markus S

unread,
Jul 28, 2009, 2:05:37 PM7/28/09
to
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.

Victor Bazarov

unread,
Jul 28, 2009, 2:54:17 PM7/28/09
to
Alf P. Steinbach wrote:
> * 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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Alf P. Steinbach

unread,
Jul 28, 2009, 8:25:11 PM7/28/09
to
* Victor Bazarov:

> Alf P. Steinbach wrote:
>> * 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.
>
> You don't know about the templated copy-conversion constructor? Please
> see [lib.pairs]/1.

Thanks. Well nothing surprises me anymore. :-)


[snip]


>>
>> But anyway, express the arguments as 1.0 and 100.0.
>>
>> You need the same numeric type as for the result values.


Cheers,

- Alf

0 new messages