Problem with Fabs

Jan 30, 2009 at 4:26pm
I dont understand why a simple function 'fabs' is not working when I use a double argument d_bend!!! It works only when I typecast it with 'long' as done below.

dF = pow( yieldstresses[i] * contour_widths[i] * thicknesses[i], 2.0 / ( 4.0* (1.0 / fabs(long(d_bend)) + thicknesses[i]/2.0) ) );

But in the cplusplus documentation its mentioned that fabs is available for 'double'

Btw, I am using Visual C++ 6.0

PLEASE HELP.
Jan 30, 2009 at 8:26pm
VC++ 6.0 is s**t. It's already over ten years old and its standard compliance is illusory. It's not surprising that it doesn't have a function like fabs().
I recommend changing your compiler, but if that's not an option (for whatever reason) try using abs() instead, which has the same overloads as abs() (which makes me wonder why they even bothered to include fabs() in the standard).
Feb 3, 2009 at 4:04pm
So strange!! It does not work even with 'abs'.
In the beginning it worked when I used 'abs' along with 'long' type-casting. But now its not working, I dont understand it all.

Can somebody please help me? Btw, all the variables in the code-snippet I put above are of type 'double'.
Feb 3, 2009 at 8:34pm
Why don't you try this, then?
#define ABS(x) ((x)<0?-(x):(x))
Feb 4, 2009 at 2:22pm
have you included the header where it is defined?

IIRC

#include <cmath>

or was it in math.h in VS6.0? don't remember.

There are several versions of fabs so it could be u accidentally picked up the wrong one.
Last edited on Feb 4, 2009 at 2:25pm
Feb 4, 2009 at 6:19pm
@Helios

There can be many workarounds, but I want to know the 'real' reason ;). You see, its got to do with some basic concept of typecasting, which I want to learn. Moreover I write this function and later I would like to invoke this function from Python, which in itself is a big headache. I managed to find a solution for calling a function from C using SWIG, so now I dont want to break my head further on trying to find out what do with 'define macros' if it should also be visible in Python.

@Anders43

I used math.h
Feb 5, 2009 at 1:18am
The function is a black box when called from Python. All you'll see are the parameters you pass, and the results it returns. Python will only see the compiled file (either a lib or an .o, I can't remember) and the definition (in Python) you provide. How you perform the operations will remain invisible inside the black box.

There must be something wrong with your installation. I just wrote and compiled with without problems:
1
2
3
4
5
6
7
8
#include <iostream.h>
#include <math.h>

int main(){
	long a=-14;
	cout <<fabs(a)<<endl;
	return 0;
}
Feb 5, 2009 at 9:44am
@Helios

All the variables I used are of type 'double'.

It could also be possible that 'fabs' is working fine, but the return type is something different :|.

If you see my very first post, the variable 'dF' doesnt get the exact calculated value.

Feb 5, 2009 at 10:18am
Guys,

I am really sorry. Have found my mistake, it was more down to the formula than due to fabs/abs.

The only reason why I doubted fabs was because the output seems correct on few occassions which actually was not the case, but actually a wrong interpretation of values from my side.

Thank you everybody, especially Helios for sparing the time.

Cheers
Topic archived. No new replies allowed.