abs

提供:cppreference.com
移動: 案内, 検索


文法:

    #include <cstdlib>
    int abs( int 数値 );

abs()関数は、数値の絶対値を返します。

サンプル:

     int magic_number = 10;
     cout << "推測してください: ";
     cin >> x;
     cout << "答えから " << abs( magic_number - x ) << " 離れています。" << endl;

C++では以下のようなオーバーロード版も提供されます:

    #include <cstdlib>
    long abs( long 数値 ); // labs()と同じです
 
    #include <cmath>
    // 以下の関数はfabs()の別名です
    float abs( float 数値 );
    double abs( double 数値 );
    long double abs( long double 数値 );

関連トピック: fabs, labs

個人用ツール
名前空間
変種
操作
案内
ツールボックス