Beware of the difference between ::abs and std::abs

By , last updated November 25, 2015

When you need to use the absolute value from a variable, use std::abs (usually residing in ). If you’re certain you’ll be dealing with floating point variables, use std::fabs.

If you only use ::abs with floating point variables, you’ll risk an implicit cast to an integral value (short, int, long). This won’t be converted to std::abs even if you include using namespace std;

Be safe, use ::fabs, std::abs or std::fabs, but not ::abs.


Be the first to comment.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>