P
Phil Carmody
Ken Brody said:[...]I've got a pdf file with some pseudo code that seems to have been written
by someone familiar with C++. I'm implementing the code in C, but need
help translating one particular line:
const int imax = 2*std::numeric_limits<Real>::max_exponent;
Can someone explain this and give me an equivalent C line of code? It
then iterates through a loop imax times.
You might get a better answer in comp.lang.c++ as to what that statement
means. However, a quick search for
std::numeric_limits said:The value of std::numeric_limits<T>::max_exponent is the largest positive
number n such that 10^(n-1) is a representable finite value of the
floating-point type T.
which I shall call reference 1.
And this page gives a chart of #defines:
http://en.cppreference.com/w/cpp/types/numeric_limits/max_exponent
My guess is that it is the equivalent of:
const int imax = 2*(LDBL_MAX_EXP);
(I guess it depends on what type "Real" is.)
Strangely, your logic doesn't hold, yet you've got to the right answer!
Reference 1 is *surely* in error. I believe[*] that the quote
describes something closer (but an out-by-one out) to
std::numeric_limits<T>::max_exponent10
where the "10" part is key. Were reference 1 to be absolutely
correct, then the answer would be (modulo out-by-ones):
imax = 2 * {FLT,DBL,LDBL}_MAX_10_EXP depending on what "Real" is.
Fortunately, you blasted past that presumed copy-pasto, and reached
what is almost certainly the right answer.
However, given that C++ is C++[*], it's possible that imax is used
in ways that aren't C-compatible (being as it is a const variable,
rather than a constant), and something bizarre like:
enum { imax = 2*DBL_MAX_EXP };
might be necessary to achieve all the constanty goodness that C++
may[*] provide.
Phil
[* I know bugger all about C++, or at least that's what I claim in public.]