Pseudo code to C help needed

Discussion in 'C Programming' started by Mark Storkamp, Oct 22, 2013.

  1. 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.

    The purpose of the whole routine is to find the distance from a point to
    an ellipse. It's using a modified version of Newton's Method to solve
    for the distance, and that's the loop that's being iterated through.
    Mark Storkamp, Oct 22, 2013
    1. Advertisements

  2. Mark Storkamp

    Lew Pitcher Guest

    For an explanation of a C++ (or C++-like) line of code, you should ask in

    We here in comp.lang.c can /guess/ what the line of code represents, but you
    will never be certain of that guess' accuracy.
    Tell us what the line represents, and we can give you the equivalent C code.
    Lew Pitcher, Oct 22, 2013
    1. Advertisements

  3. Mark Storkamp

    Ken Brody Guest


    You might get a better answer in comp.lang.c++ as to what that statement
    means. However, a quick search for
    And this page gives a chart of #defines:

    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.)
    Ken Brody, Oct 22, 2013
  4. Mark Storkamp

    James Kuyper Guest

    C++ supports class templates. std::numeric_limits<T> is one such
    template, where T identifies the type for which the template is to be
    instantiated. This template has been specialize by the C++
    implementation of the C++ standard library, for every arithmetic type
    supported by that implementation of C++.. One element of
    std::numeric_limits<T> is named max_exponent - it's the maximum value
    for the exponent such that std::numeric_limits<T>::radix raised to that
    power, -1, is a value representable in type T.

    The identifier "Real" is a type name, presumably a typedef for a
    floating point type.
    There's no exact equivalent, since C does not have templates. However,
    if you know which C type "Real" corresponds with, you can find the
    following macros #defined in <float.h>:

    T max_exponent
    =========== ============
    float FLT_MAX_EXP
    double DBL_MAX_EXP
    long double LDBL_MAX_EXP
    James Kuyper, Oct 22, 2013
  5. Mark Storkamp

    BartC Guest

    The 'std::numeric_limits<Real>::' is just some stuff that tells it where to
    find 'max_exponent' (perhaps with some generics thrown in).

    This might be equivalent to FLT_MAX_EXP or DBL_MAX_EXP in C (which seems to
    return it as a binary count). I believe from float.h.

    It anyway sounds an unlikely requirement for an algorithm to do with
    ellipses. You can try setting imax=42 and see what happens.
    BartC, Oct 22, 2013
  6. Mark Storkamp

    James Kuyper Guest

    Answering his question requires a knowledge of both C++ and C; that
    combination can be found either in a C++ newsgroup or a C newsgroup - I
    think it's equally topical (or, if you insist that it's not topical,
    euqally untopical) in either newsgroup.
    James Kuyper, Oct 22, 2013
  7. You'd #include <float.h> and then use one of the three constants


    depending on what type 'Real' denotes (I'd guess it's a typedef):

    const int imax = 2 * DBL_MAX_EXP;

    I wonder what the 2 is there for. In case it matters, it's possible
    that 2 * DBL_MAX_EXP != DBL_MAX_EXP - DBL_MIN_EXP.

    [C++ explanation: std::numeric_limits is a class in the std namespace
    (the namespace used by the standard library) which includes a bunch of
    constants pertaining to numerical limits. It's what's call a templated
    class, in that there is, in some sense, a version of it for any suitable
    type written inside the <>s. So std::numeric_limits<float>::max_exponent
    is a constant equivalent to C's FLT_MAX_EXP.]

    If the C++ code is parameterised by the floating point type used, you
    might have to use some combination of macros, the type generic maths
    functions, and C11's new _Generic construct to get a faithful
    translation. Why not to use C++?
    Ben Bacarisse, Oct 22, 2013
  8. Thanks for the explanation. As to why I'm not doing it in C++: I'm doing
    a single specific implementation of the function using floats, and
    adding it to a large project that's already written in C. So I don't
    need to worry about combinations of macros and type generic routines.
    And because the pseudo code is not a complete C++ program, but pseudo
    code with some C++ constructs, and I only learned C++ back before
    templates and all the other modern enhancements, I'm not really up to
    the task.
    Mark Storkamp, Oct 22, 2013
  9. Thanks to all who responded, 2*FLT_MAX_EXP is what it will be.
    Mark Storkamp, Oct 22, 2013
  10. Mark Storkamp

    Eric Sosman Guest

    Aside: This description differs from the one at


    .... in that the former assumes radix ten while the latter allows
    for other radices. Since actual floating-point implementations
    almost never use radix ten, taking the first description at face
    value might be ill-advised.

    Just because a search found something doesn't mean the search
    has found Truth. "On the Internet, nobody knows you're a dog."
    Eric Sosman, Oct 22, 2013
  11. There is also FLT_MAX_10_EXP

    Also, since radix 10 floating point is now part of the IEEE standard,
    one might see more implementations supporting it.

    As far as I know, now only on IBM z/ machines.
    -- glen
    glen herrmannsfeldt, Oct 22, 2013
  12. (snip)
    Or maybe 2*FLT_MAX_10_EXP.

    -- glen
    glen herrmannsfeldt, Oct 22, 2013
  13. Mark Storkamp

    James Kuyper Guest

    The original message specified
    std::numeric_limits<Real>::max_exponent, not
    James Kuyper, Oct 22, 2013
  14. Mark Storkamp

    James Kuyper Guest

    I'd forgotten about _Generic. The OP has already indicated that he only
    needs to cover the case where Real is float, but here's my attempt at a
    more faithful equivalent:

    #include <float.h>
    #define MAX_EXPONENT(type) _Generic((type)0, float: FLT_MAX_EXP, \
    double:DBL_MAX_EXP, long double:LDBL_MAX_EXP)

    const int imax = 2*MAX_EXPONENT(Real);

    The only C compiler I have access to at the moment doesn't support
    C2011, so I can't test this code right, and this is my very first
    attempt to use this feature, so expect that it may contain errors.
    James Kuyper, Oct 22, 2013
  15. clang likes it.
    Ben Bacarisse, Oct 22, 2013
  16. A previous post suggested that it was base 10.

    -- glen
    glen herrmannsfeldt, Oct 22, 2013
  17. Mark Storkamp

    James Kuyper Guest

    True, but that was based upon an incorrect description of max_exponent;
    one that is inconsistent with the C++ standard. I just noticed that Eric
    Sosman only expressed skepticism about the incorrect description - he
    did seem to be sure that it was incorrect. The C++ standard describes
    max_exponent as:

    "Maximum positive integer such that radix raised to the power one less
    than that integer is a representable finite floating point number."

    The C standard describes the *_MAX_EXP macros as "maximum integer such
    that FLT_RADIX raised to one less than that power is a representable
    finite floating-point number,". (
    James Kuyper, Oct 22, 2013
  18. Why are you using floats rather than doubles? double tends to be the
    "default" floating-point type in C.
    Keith Thompson, Oct 22, 2013
  19. (snip, and previoulsy I wrote)
    In that case, I suggest 7*FLT_MAX_10_EXP.

    That is, with 7 an integer near 2*log(10)/log(2).

    As far as I know, (int)(FLT_MAX_EXP*log(2)/log(FLT_RADIX))

    Could be a little closer with 66*FLT_MAX_10_EXP/10.

    is not a constant expression, as one might want in this case.
    and so usually should be used in a context using FLT_RADIX.

    -- glen
    glen herrmannsfeldt, Oct 22, 2013
  20. Mark Storkamp

    James Kuyper Guest

    The description of std::numeric_limits<T>::max_exponent in the C++
    standard is almost an exact match for the description of the *_MAX_EXP
    macros in the C standard. If Real is a typedef for float, which appears
    to be the case, why would you use anything other than FLT_MAX_EXP as a C
    replacement for that C++ expression? What advantage do you see from the
    use of 7*FLT_MAX_10_EXP instead? I see one substantial disadvantage: if
    FLT_MAX_EXP is not an exact multiple of 7 (and in general, it won't be),
    then 7*FLT_MAX_10_EXP cannot have the same value as FLT_MAX_EXP. Can you
    come up with any advantage to make up for that disadvantage?
    FLT_RADIX in the description of C's *_MAX_EXP macros corresponds exactly
    to the C++ std::numeric_limits<T>::radix, which is simply referred to as
    "radix" in the description of std::numeric_limits<T>::max_exponent.
    James Kuyper, Oct 22, 2013
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.