Archived

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

Absolute value???

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 ]

Share this post


Link to post
Share on other sites
If you want a real fast abs function, just turn off the sign bit in the internal representation of a double. It''s not portable, it''s obscured, it''s a hack. But this site is about programming games, where speed is a concern most of the time.

Share this post


Link to post
Share on other sites
quote:
Original post by Timkin
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


timkin, yeah.. but think about it. other peoples assume its a general math and physics forum and asks questions for example about homework or so. they get closed with the answer if its not a game related math question don''t ask it in here, we''re gamedev, its about math and physicquestions of games.

i''ve seen this a lot of times:D so he bether got a gamedev answer:D else he would have got closed:D

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites