Absolute value???
Newb question, how do you get the absolute value of a variable? Thanks in advance!!!
Or use abs ().
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
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
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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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
Cheers,
Timkin
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


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
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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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 ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement