# Pseudo code to C help needed

M

#### Mark Storkamp

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.

L

#### Lew Pitcher

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

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

We here in comp.lang.c can /guess/ what the line of code represents, but you
will never be certain of that guess' accuracy.
and give me an equivalent C line of code?

Tell us what the line represents, and we can give you the equivalent C code.

K

#### Ken Brody

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.

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

J

#### James Kuyper

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;

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.
Can someone explain this and give me an equivalent C line of code?

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

B

#### BartC

Mark Storkamp 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.

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.

J

#### James Kuyper

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

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.

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.

B

#### Ben Bacarisse

Mark Storkamp 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'd #include <float.h> and then use one of the three constants

FLT_MAX_EXP
DBL_MAX_EXP
LDBL_MAX_EXP

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++?

M

#### Mark Storkamp

Ben Bacarisse said:
Mark Storkamp 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'd #include <float.h> and then use one of the three constants

FLT_MAX_EXP
DBL_MAX_EXP
LDBL_MAX_EXP

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++?

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

M

#### Mark Storkamp

Mark Storkamp 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.

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.

Thanks to all who responded, 2*FLT_MAX_EXP is what it will be.

E

#### Eric Sosman

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.

Aside: This description differs from the one at

<http://www.cplusplus.com/reference/limits/numeric_limits/>

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

Just because a search found something doesn't mean the search
has found Truth. "On the Internet, nobody knows you're a dog."

G

#### glen herrmannsfeldt

Eric Sosman said:
Aside: This description differs from the one at

<http://www.cplusplus.com/reference/limits/numeric_limits/>
... 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

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.
Just because a search found something doesn't mean the search
has found Truth. "On the Internet, nobody knows you're a dog."

-- glen

G

#### glen herrmannsfeldt

(snip)
Thanks to all who responded, 2*FLT_MAX_EXP is what it will be.

Or maybe 2*FLT_MAX_10_EXP.

-- glen

J

#### James Kuyper

(snip)

Or maybe 2*FLT_MAX_10_EXP.

The original message specified
std::numeric_limits<Real>::max_exponent, not
std::numeric_limits<Real>::max_exponent10.

J

#### James Kuyper

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

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, \
doubleBL_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.

B

#### Ben Bacarisse

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, \
doubleBL_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.

clang likes it.

G

#### glen herrmannsfeldt

The original message specified
std::numeric_limits<Real>::max_exponent, not
std::numeric_limits<Real>::max_exponent10.

A previous post suggested that it was base 10.

-- glen

J

#### James Kuyper

A previous post suggested that it was base 10.

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."
(18.3.2.4p31)

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,". (5.2.4.2.2p11)

K

#### Keith Thompson

Mark Storkamp said:
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

Why are you using floats rather than doubles? double tends to be the
"default" floating-point type in C.

G

#### glen herrmannsfeldt

(snip, and previoulsy I wrote)
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."
(18.3.2.4p31)

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.
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,". (5.2.4.2.2p11)

and so usually should be used in a context using FLT_RADIX.

-- glen

J

#### James Kuyper

(snip, and previoulsy I wrote)

In that case, I suggest 7*FLT_MAX_10_EXP.

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