abs [C++ Reference]
Keywords: c,math,abs
Date: 2009-03-20T00:00:22-0700

[[abs]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## abs

Syntax:

        #include <cstdlib>
        int abs( int num );

The abs() function returns the absolute value of num. For example:

         int magic_number = 10;
         cout << "Enter a guess: ";
         cin >> x;
         cout << "Your guess was " << abs( magic_number - x ) << " away from the
      magic number." << endl;

C++ also provides the following overloaded forms:

        #include <cstdlib>
        long abs( long num ); // same as labs()

        #include <cmath>
        // the following are synonyms for fabs()
        float abs( float num );
        double abs( double num );
        long double abs( long double num );

Related Topics: fabs, labs

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •