#### Archived

This topic is now archived and is closed to further replies.

# Absolute value???

This topic is 5908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Newb question, how do you get the absolute value of a variable? Thanks in advance!!!

##### Share on other sites
square root of the square of the value.

Timkin

##### Share on other sites
Or use abs ().

How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

##### Share on other sites
Cool thanks again!

##### Share on other sites
#define ABS( p ) { if(p < 0) p = -p }

use that macro

##### Share on other sites
Under no circumstances use the above macro!

a) It changes the actual value of the variable, that''s a no-no
b) Can''t be used as an expression e.g. can''t do
if( ABS ( x ) < y )
c) ABS( x + 1 ) doesn''t compile
d) It''s called the same as a macro from a standard header!

If you must use a macro, use the one in math.h (or is it stddef.h), defined as

#define ABS ( x_ ) ( ( ( x_ ) < 0 ) ? ( -( x_ ) ) : ( x_ ) )

but that''s still a bit dodgy, ABS ( x++ ) can incrememnt x twice.

Use the abs template function from the C++ standard library for the best option... defined as

template< class T > T abs( T arg )
{
return( arg < 0 ? -arg : arg );
}

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
Hehe... it''s funny how a person''s background and beliefs affect their answers! I had assumed that since this was a Maths and Physics forum, that Noods wanted the mathematical definition. Alternatively, Paradigm Shifter provided a programming definition, which might have been the first response if the question were asked in the General Progamming forum... both answers are of course valid... it''s just interesting to ponder why we provide the particular answers we do and what influences our thinking!

Cheers,

Timkin

##### Share on other sites
While this is a Math and Physics board, it''s surrounded by game development and programming boards, and when you mix math, programming, and something as simple as absolute value, you get code So the first reaction is a code solution. That would at least have been my response

##### Share on other sites
I just had to moan about the AP''s dodgy macro I''m afraid!

All programming languages I have heard of have an abs function or equivalent, I would never recommend using sqrt( x*x ), despite my degree in maths! It''s like I wouldn''t recommend

if( a * b != 0 )
over
if ( a != 0 && b != 0 )

Timkin''s advice is normally very good though...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

##### Share on other sites
quote:
Original post by Timkin
Hehe... it''s funny how a person''s background and beliefs affect their answers!

Yeah... this is something that annoyed me in Statistics classes, because the formulae would often square all the data, and later take the square root of it, just to get rid of the sign. As a programmer I would have preferred to use abs(x) to make it shorter and more obvious, but my calculator doesn''t have an abs button

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633325
• Total Posts
3011371
• ### Who's Online (See full list)

There are no registered users currently online

×