名前空間
変種
操作

「cpp/numeric/math/sqrt」の版間の差分

提供: cppreference.com
< cpp‎ | numeric‎ | math
(Translated from the English version using Google Translate)
 
(3人の利用者による、間の7版が非表示)
1行: 1行:
{{tr_note}}
+
{{cpp/title|sqrt}}
{{cpp/title|sqrt}}
+
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
{{ddcl list begin}}
+
{{begin}}
{{ddcl list header | cmath}}
+
{{header | cmath}}
{{ddcl list item |
+
{{|
float      sqrt( float arg );
+
 +
float      sqrt
 +
 +
( float arg );
 
}}
 
}}
{{ddcl list item |
+
{{|
double      sqrt( double arg );
+
double      sqrt ( double arg );
 
}}
 
}}
{{ddcl list item |
+
{{|
long double sqrt( long double arg );
+
 +
long double sqrt
 +
 +
( long double arg );
 
}}
 
}}
{{ddcl list item | notes={{mark since c++11}} |
+
{{|=sincec++11 |
double      sqrt( Integral arg );
+
double      sqrt ( arg );
 
}}
 
}}
{{ddcl list end}}
+
{{end}}
  
{{tr|{{tt|arg}}の平方根を計算.|Computes square root of {{tt|arg}}.}}
+
{{tt|arg}}
 +
|{{|}}
  
===パラメータ===
+
======
{{param list begin}}
+
{{begin}}
{{param list item | arg |{{tr| 浮動小数点または整数値| floating point or integer value}}}}
+
{{| arg | |}}
{{param list end}}
+
{{end}}
  
===値を返します===
+
======
{{tr|{{tt|arg}}の平方根.|square root of {{tt|arg}}.}}
+
{{tt|arg}} の平方根 |{{|arg}}}}
  
{{tr|{{tt|arg}}が負の場合はドメイン·エラーが発生します。 {{c|NAN}}はその場合に返されます.|Domain error occurs if {{tt|arg}} is negative. {{c|NAN}} is returned in that case.}}
+
  
===も参照してください===
+
{{dcl list begin}}
+
 
{{dcl list template | cpp/numeric/math/dcl list cbrt}}
+
======
{{dcl list template | cpp/numeric/math/dcl list pow}}
+
{{
{{dcl list template | cpp/numeric/complex/dcl list sqrt}}
+
 
{{dcl list template | cpp/numeric/valarray/dcl list sqrt}}
+
{{dcl list end}}
+
 
 +
 +
 +
 +
 +
 
 +
 +
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
 +
begin}}
 +
{{| cpp/numeric/math/
 +
cbrt}}
 +
{{| cpp/numeric/math/}}
 +
{{| cpp/numeric/complex/sqrt}}
 +
{{| cpp/numeric/valarray/sqrt}}
 +
{{
 +
end
 +
 
 +
}}

2018年12月5日 (水) 15:01時点における最新版

 
 
 
一般的な数学関数
関数
基本的な演算
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数関数
(C++11)
(C++11)
(C++11)
(C++11)
冪関数
sqrt
(C++11)
(C++11)
三角関数と双曲線関数
(C++11)
(C++11)
(C++11)
誤差関数とガンマ関数
(C++11)
(C++11)
(C++11)
(C++11)
最も近い整数
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮動小数点操作関数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類および比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
マクロ定数
(C++11)(C++11)(C++11)(C++11)(C++11)
 
ヘッダ <cmath> で定義
(1)
float       sqrt ( float arg );
float       sqrtf( float arg );
(C++11以上)
double      sqrt ( double arg );
(2)
(3)
long double sqrt ( long double arg );
long double sqrtl( long double arg );
(C++11以上)
double      sqrt ( 整数型 arg );
(4) (C++11以上)
1-3) arg の平方根を計算します。
4) 任意の整数型の引数を受理するオーバーロード集合または関数テンプレート。 2) と同等です (引数は double にキャストされます)。

目次

[編集] 引数

arg - 浮動小数点または整数型の値

[編集] 戻り値

エラーが発生なければ、 arg の平方根 (arg) が返されます。

定義域エラーが発生した場合、処理系定義の値 (サポートされていれば NaN) が返されます。

アンダーフローによる値域エラーが発生した場合、 (丸めた後の) 正しい結果が返されます。

[編集] エラー処理

math_errhandling で規定されている通りにエラーが報告されます。

arg がゼロより小さい場合、定義域エラーが発生します。

処理系が IEEE 浮動小数点算術 (IEC 60559) をサポートしている場合、

  • 引数が -0 より小さければ、 FE_INVALID が発生し、 NaN が返されます。
  • 引数が +∞ または ±0 であれば、それが変更されずに返されます。
  • 引数が NaN であれば、 NaN が返されます。

[編集] ノート

std::sqrt は IEEE 標準によって正確であることが要求されています。 他に正確であることが要求されている演算は、算術演算子と関数 std::fma だけです。 戻り値型に (デフォルトの丸めモードを使用して) 丸めた後、 std::sqrt の結果は無限に正確な結果から区別できません。 別の言い方をすると、誤差は 0.5 ulp 未満です。 他の関数 (std::pow など) には、そのような制約はありません。

[編集]

#include <iostream>
#include <cmath>
#include <cerrno>
#include <cfenv>
#include <cstring>
 
#pragma STDC FENV_ACCESS ON
 
int main()
{
    // 通常の使用
    std::cout << "sqrt(100) = " << std::sqrt(100) << '\n'
              << "sqrt(2) = " << std::sqrt(2) << '\n'
              << "golden ratio = " << (1+std::sqrt(5))/2 << '\n';
    // 特殊な値
    std::cout << "sqrt(-0) = " << std::sqrt(-0.0) << '\n';
    // エラー処理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "sqrt(-1.0) = " << std::sqrt(-1) << '\n';
    if(errno == EDOM)
        std::cout << "    errno = EDOM " << std::strerror(errno) << '\n';
    if(std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

出力例:

sqrt(100) = 10
sqrt(2) = 1.41421
golden ratio = 1.61803
sqrt(-0) = -0
sqrt(-1.0) = -nan
    errno = EDOM Numerical argument out of domain
    FE_INVALID raised

[編集] 関連項目

(C++11)(C++11)
xy 乗 (xy) を計算します
(関数) [edit]
(C++11)(C++11)(C++11)
立方根 (3x) を計算します
(関数) [edit]
(C++11)(C++11)(C++11)
2つまたは3つの値の二乗和の平方根 (x2
+y2
または x2
+y2
+z2
) を計算します
(関数) [edit]
右半平面の範囲の複素数の平方根
(関数テンプレート) [edit]
valarray の各要素に関数 std::sqrt を適用します
(関数テンプレート) [edit]