名前空間
変種
操作

std::cbrt, std::cbrtf, std::cbrtl

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
ヘッダー <cmath> で定義
(1)
float       cbrt ( float num );

double      cbrt ( double num );

long double cbrt ( long double num );
(C++23まで)
/*浮動小数点数型*/
            cbrt ( /*浮動小数点型*/ num );
(C++23から)
(C++26 以降 constexpr)
float       cbrtf( float num );
(2) (C++11以降)
(C++26 以降 constexpr)
long double cbrtl( long double num );
(3) (C++11以降)
(C++26 以降 constexpr)
ヘッダー <simd> で定義
template< /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/<V>

            cbrt ( const V& v_num );
(S) (C++26以降)
ヘッダー <cmath> で定義
template< class Integer >
double      cbrt ( Integer num );
(A) (C++26 以降 constexpr)
1-3) num の立方根を計算します。ライブラリは、パラメータの型としてすべての cv 修飾されていない浮動小数点型に対する std::cbrt のオーバーロードを提供します。(since C++23)
S) SIMD オーバーロードは、v_num に対して要素ごとの std::cbrt を実行します。
(定義については、math-floating-point および deduced-simd-t を参照してください。)
(C++26以降)
A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
(C++11以降)

目次

[編集] パラメータ

num - 浮動小数点数または整数値

[編集] 戻り値

エラーが発生しなかった場合、num の立方根(3num)を返します。

アンダーフローによる範囲エラーが発生した場合、正確な結果 (丸め後) が返される。

[編集] エラー処理

エラーは math_errhandling で指定された通りに報告される。

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

  • 引数が ±0 または ±∞ の場合、変更されずに返されます。
  • 引数が NaN の場合、NaN が返されます。

[編集] 注記

std::cbrt(num)std::pow(num, 1.0 / 3) と等価ではありません。なぜなら、有理数
1
3
は通常 1.0 / 3 と等しくなく、std::pow は負の基数を分数指数で累乗することはできないためです。さらに、std::cbrt(num) は、通常 std::pow(num, 1.0 / 3) よりも精度の高い結果を返します(例を参照)。

追加のオーバーロードは、(A) とまったく同じように提供される必要はありません。整数型の引数 num に対して、std::cbrt(num)std::cbrt(static_cast<double>(num)) と同じ効果を持つことを保証するのに十分であればよいのです。

[編集]

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
 
int main()
{
    std::cout
        << "Normal use:\n"
        << "cbrt(729)       = " << std::cbrt(729) << '\n'
        << "cbrt(-0.125)    = " << std::cbrt(-0.125) << '\n'
        << "Special values:\n"
        << "cbrt(-0)        = " << std::cbrt(-0.0) << '\n'
        << "cbrt(+inf)      = " << std::cbrt(INFINITY) << '\n'
        << "Accuracy and comparison with `pow`:\n"
        << std::setprecision(std::numeric_limits<double>::max_digits10)
        << "cbrt(343)       = " << std::cbrt(343) << '\n'
        << "pow(343,1.0/3)  = " << std::pow(343, 1.0 / 3) << '\n'
        << "cbrt(-343)      = " << std::cbrt(-343) << '\n'
        << "pow(-343,1.0/3) = " << std::pow(-343, 1.0 / 3) << '\n';
}

実行結果の例

Normal use:
cbrt(729)       = 9
cbrt(-0.125)    = -0.5
Special values:
cbrt(-0)        = -0
cbrt(+inf)      = inf
Accuracy and comparison with `pow`:
cbrt(343)       = 7
pow(343,1.0/3)  = 6.9999999999999991
cbrt(-343)      = -7
pow(-343,1.0/3) = -nan

[編集] 関連項目

(C++11)(C++11)
数値を指定されたべき乗に累乗する (xy)
(関数) [編集]
(C++11)(C++11)
平方根を計算する (x)
(関数) [編集]
(C++11)(C++11)(C++11)
斜辺 x2
+y2
を計算する および x2
+y2
+z2
(C++17 から)

(関数) [編集]
English 日本語 中文(简体) 中文(繁體)