Results 1 to 5 of 5

Thread: Absolute value without sign *Resolved*

  1. #1

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    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--
    Last edited by swatty; Nov 15th, 2002 at 08:19 AM.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4

    Thread Starter
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Thank should have tried it first.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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
  •  



Click Here to Expand Forum to Full Width