How do I translate the following two Matlab expressions into C++?

L

Luna Moon

Hi all,

I am using Matlab 2007a and MSVS.NET2003 on Windows XP.

I used Matlab for prototyping and now I converted the code line by
line into C++.

However, there was some precision issue with my C++ results.

I suspected that I didn't understand the numerical difference between
Matlab and C++ correctly.

Specifically, I don't really know how to translate the following two
Matlab expressions, and what are their equivalent statements in C++?
Here I attached a few examples and their Matlab documents. Could you
please help me? Thanks!

ans =

1.1755e-038
ans =
2.2251e-308
REALMIN Smallest positive floating point number.
x = realmin is the smallest positive normalized double precision
floating
point number on this computer. Anything smaller underflows or is
an IEEE
"denormal".

REALMIN('double') is the same as REALMIN with no arguments.

REALMIN('single') is the smallest positive normalized single
precision
floating point number on this computer.

See also eps, realmax, intmin.
Reference page in Help browser
doc realmin

------------------------
ans =
2.2204e-016
ans =
1.1369e-013
EPS Spacing of floating point numbers.
D = EPS(X), is the positive distance from ABS(X) to the next
larger in
magnitude floating point number of the same precision as X.
X may be either double precision or single precision.
For all X, EPS(X) is equal to EPS(ABS(X)).

EPS, with no arguments, is the distance from 1.0 to the next
larger double
precision number, that is EPS with no arguments returns 2^(-52).

EPS('double') is the same as EPS, or EPS(1.0).
EPS('single') is the same as EPS(single(1.0)), or single(2^-23).

Except for numbers whose absolute value is smaller than REALMIN,
if 2^E <= ABS(X) < 2^(E+1), then
EPS(X) returns 2^(E-23) if ISA(X,'single')
EPS(X) returns 2^(E-52) if ISA(X,'double')

For all X of class double such that ABS(X) <= REALMIN, EPS(X)
returns 2^(-1074). Similarly, for all X of class single such
that
ABS(X) <= REALMIN('single'), EPS(X) returns 2^(-149).

Replace expressions of the form
if Y < EPS * ABS(X)
with
if Y < EPS(X)

Example return values from calling EPS with various inputs are
presented in the table below:

Expression Return Value
===========================================
eps(1/2) 2^(-53)
eps(1) 2^(-52)
eps(2) 2^(-51)
eps(realmax) 2^971
eps(0) 2^(-1074)
eps(realmin/2) 2^(-1074)
eps(realmin/16) 2^(-1074)
eps(Inf) NaN
eps(NaN) NaN
-------------------------------------------
eps(single(1/2)) 2^(-24)
eps(single(1)) 2^(-23)
eps(single(2)) 2^(-22)
eps(realmax('single')) 2^104
eps(single(0)) 2^(-149)
eps(realmin('single')/2) 2^(-149)
eps(realmin('single')/16) 2^(-149)
eps(single(Inf)) single(NaN)
eps(single(NaN)) single(NaN)

See also realmax, realmin.
Reference page in Help browser
doc eps
 
V

Victor Bazarov

Luna said:
I used Matlab for prototyping and now I converted the code line by
line into C++.

However, there was some precision issue with my C++ results.

I suspected that I didn't understand the numerical difference between
Matlab and C++ correctly.

Specifically, I don't really know how to translate the following two
Matlab expressions, and what are their equivalent statements in C++?
Here I attached a few examples and their Matlab documents. Could you
please help me? Thanks!


ans =

1.1755e-038

If I understand correctly, you want 'std::numeric_limits<float>::min()'
and 'std::numeric_limits<double>::min()' functions. #include <limits>.

V
 
B

Bart van Ingen Schenau

Luna said:
Hi all,

I am using Matlab 2007a and MSVS.NET2003 on Windows XP.

I used Matlab for prototyping and now I converted the code line by
line into C++.

However, there was some precision issue with my C++ results.

I suspected that I didn't understand the numerical difference between
Matlab and C++ correctly.

Specifically, I don't really know how to translate the following two
Matlab expressions, and what are their equivalent statements in C++?
Here I attached a few examples and their Matlab documents. Could you
please help me? Thanks!


ans =

1.1755e-038

FLT_MIN (from <float.h>) or std::numeric_limits<float>::min() (from
ans =
2.2251e-308

DBL_MIN (from <float.h>) or std::numeric_limits<double>::min() (from
ans =
2.2204e-016

ans =
1.1369e-013

EPS Spacing of floating point numbers.
D = EPS(X), is the positive distance from ABS(X) to the next
larger in
magnitude floating point number of the same precision as X.
X may be either double precision or single precision.
For all X, EPS(X) is equal to EPS(ABS(X)).

There is no proper equivalent for the Matlab eps function in C++.
The closes you can get is
- FLT_EPSILON or std::numeric_limits<float>::epsilon() (single
precision)
- DBL_EPSILON or std::numeric_limits<double>::epsilon() (double
precision)

These correspond to respectively eps(single(1.0)) and eps(1.0).
C++ does not have a facility to obtain the next higher floating point
number after an arbitrary value.

Bart v Ingen Schenau
 
L

Luna Moon

FLT_MIN (from <float.h>) or std::numeric_limits<float>::min() (from
<limits>)




DBL_MIN (from <float.h>) or std::numeric_limits<double>::min() (from
<limits>)









There is no proper equivalent for the Matlab eps function in C++.
The closes you can get is
- FLT_EPSILON or std::numeric_limits<float>::epsilon() (single
precision)
- DBL_EPSILON or std::numeric_limits<double>::epsilon() (double
precision)

These correspond to respectively eps(single(1.0)) and eps(1.0).
C++ does not have a facility to obtain the next higher floating point
number after an arbitrary value.

Bart v Ingen Schenau

Is there a way to program the Matlab "eps(1000-0)" using some more
atomic expressions in C++?

I really need that expression... I met with numerical problems and I
suspect it is this line that needs fix...
 
B

Bart van Ingen Schenau

Luna said:
Is there a way to program the Matlab "eps(1000-0)" using some more
atomic expressions in C++?

I really need that expression... I met with numerical problems and I
suspect it is this line that needs fix...

You could try to see if your implementation supports the C function
nextafter() in <math.h>.
Then you could implement your own eps function like this:

double eps(double x)
{
return nextafter(x, DBL_MAX) - x;
}

This function was introduced in the 1999 revision of the C standard,
which is not widely supported. But it never hurts to try this one.

Otherwise, the closest approximation is 'DBL_EPSILON*(1000-0)'.
If that is not good enough, you will have to rewrite your algorithm to
avoid the dependency on eps(1000-0).

Bart v Ingen Schenau
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top