|
-
Nov 15th, 2002, 07:41 AM
#1
Thread Starter
Frenzied Member
Absolute value without sign *Resolved*
Is there a way in C++ to get the absolute value of an integer ??
if after a calculation i get a value returned -2 how can i use some function to get 2
I don't want to write
PHP Code:
if(c<0) c = c -c -c ;
Last edited by swatty; Nov 15th, 2002 at 08:19 AM.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Nov 15th, 2002, 08:12 AM
#2
Addicted Member
abs(value)
Don't know if there are any float/double etc methods for this - probably, but abs is the right one for ints.
Need math.h I think for this.
HD
-
Nov 15th, 2002, 08:17 AM
#3
Monday Morning Lunatic
In C++, it's the <cmath> header 
You can use fabs() for floating-point numbers.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 15th, 2002, 08:18 AM
#4
Thread Starter
Frenzied Member
Thank should have tried it first.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Nov 15th, 2002, 10:10 AM
#5
Frenzied Member
There are a lot of 'abs' functions - cabs(), labs(), etc.
The generally accepted dogma is that you should use a macro for functions like sgn(), abs(), etc because they are faster - a lot faster.
Kinda like this:
Code:
#define abs(c) ( (c<0) ? (-1)*c : c )
You can also force 'inlining' by using macros -
This does a complex reciprocal for instance.
arg & out are type Complex (a struct)
Code:
#define LCMPLXrecip(arg,out) \
{ long denom; denom = lsqr((arg).x) + lsqr((arg).y);\
if(denom==0L) overflow=1; else {(out).x = divide((arg).x,denom,bitshift);\
(out).y = -divide((arg).y,denom,bitshift);}}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|