C++ abs() function

A

Aaron Gallimore

Hi,

I'm having trouble finding an absolute value of a float or double. my code
looks something like this:

for (i=0; i<num_delays ; i++){
double x = delay[i+1]-delay;
jitter_arr = abs(x);
jitter_calc = jitter_calc + jitter_arr;
}

i've stepped through and everytime I add a value to the jitter_arr[] array
it put a value of 0 in.

any ideas?
thanks
Aaron
 
P

P.J. Plauger

I'm having trouble finding an absolute value of a float or double. my code
looks something like this:

for (i=0; i<num_delays ; i++){
double x = delay[i+1]-delay;
jitter_arr = abs(x);
jitter_calc = jitter_calc + jitter_arr;
}

i've stepped through and everytime I add a value to the jitter_arr[] array
it put a value of 0 in.

any ideas?


Try calling fabs instead. The Standard C++ library is *supposed* to
overload abs the way you expect, but it's common for implementations
to omit the overloads, or put them in the wrong place (<cmath> instead
of <math.h>).

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
A

Aaron Gallimore

Try calling fabs instead. The Standard C++ library is *supposed* to
overload abs the way you expect, but it's common for implementations
to omit the overloads, or put them in the wrong place (<cmath> instead
of <math.h>).

ace! worked first time.

Thanks
Aaron
 
R

Rob Williscroft

Aaron Gallimore wrote in
Hi,

I'm having trouble finding an absolute value of a float or double. my
code looks something like this:

for (i=0; i<num_delays ; i++){

What is the type of delay[] if its an integer type you need to do:

double x = double( delay[i+1] ) - delay;

or if you don't like casts:

double x = delay[i + 1];
x -= delay;
double x = delay[i+1]-delay;

jitter_arr = abs(x);


Did you want to call fabs()

jitter_arr = std::fabs(x);

fabs() is declared as; double std::fabs( double ); in <cmath> and also
as: double ::fabs( double ); if you #include said:
jitter_calc = jitter_calc + jitter_arr;
}

i've stepped through and everytime I add a value to the jitter_arr[]
array it put a value of 0 in.


HTH.

Rob.
 
A

Aaron Gallimore

what's the difference between calling:
std::fabs(x);
than the way I have done it:
fabs(x)

as for the other question, *delay[] is a float (need precision)

Thanks
Aaron

Rob Williscroft said:
Aaron Gallimore wrote in
Hi,

I'm having trouble finding an absolute value of a float or double. my
code looks something like this:

for (i=0; i<num_delays ; i++){

What is the type of delay[] if its an integer type you need to do:

double x = double( delay[i+1] ) - delay;

or if you don't like casts:

double x = delay[i + 1];
x -= delay;
double x = delay[i+1]-delay;

jitter_arr = abs(x);


Did you want to call fabs()

jitter_arr = std::fabs(x);

fabs() is declared as; double std::fabs( double ); in <cmath> and also
as: double ::fabs( double ); if you #include said:
jitter_calc = jitter_calc + jitter_arr;
}

i've stepped through and everytime I add a value to the jitter_arr[]
array it put a value of 0 in.


HTH.

Rob.
 
R

Rob Williscroft

Aaron Gallimore wrote in
what's the difference between calling:
std::fabs(x);
than the way I have done it:
fabs(x)

If you include <cmath> then fabs() will be put into the std namespace
something like:

namespace std
{
double fabs( double );
}

To use this in your code you need to either do:

double f()
{
return std::fabs( -1.0 );
}

double g()
{
using std::fabs;
return fabs( -1.0 );
}

double h()
{
using namspace std;
return fabs( -1.0 );
}

Either of the using declarations can be at namspace scope i.e

using std::fabs;
double j()
{
return fabs( -1.0 );
}

if you include <math.h> then your compiler should efectivly
#include <cmath> and the do a

using std::fabs;

but for every function declared in <cmath>.

This applies to all the "C" headers <cstdio> vs <stdio.h>,
<cstdlib> vs <stdlib.h>, etc.

Rob.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top