最終更新日時(UTC):
が更新

履歴 編集

function
<cmath>

std::tan

namespace std {
  float tan(float x);             // (1) C++03からC++20まで
  double tan(double x);           // (2) C++03からC++20まで
  long double tan(long double x); // (3) C++03からC++20まで

  floating-point-type
    tan(floating-point-type x);   // (4) C++23
  constexpr floating-point-type
    tan(floating-point-type x);   // (4) C++26

  double
    tan(Integral x);              // (5) C++11
  constexpr double
    tan(Integral x);              // (5) C++26

  float
    tanf(float x);                // (6) C++17
  constexpr float
    tanf(float x);                // (6) C++26

  long double
    tanl(long double x);          // (7) C++17
  constexpr long double
    tanl(long double x);          // (7) C++26
}

概要

算術型の正接(タンジェント)を求める。

戻り値

引数 x の正接を返す(xの単位はラジアン)。

備考

#include <cmath>
#include <iostream>

int main() {
  static const double pi = 3.141592653589793;
  std::cout << std::fixed;
  std::cout << "tan(0.0)  = " << std::tan(0.0) << std::endl;
  std::cout << "tan(pi/6) = " << std::tan(pi/6) << std::endl;
  std::cout << "tan(pi/4) = " << std::tan(pi/4) << std::endl;
  std::cout << "tan(pi/3) = " << std::tan(pi/3) << std::endl;
  std::cout << "tan(pi/2) = " << std::tan(pi/2) << std::endl;
}

出力例

tan(0.0)  = 0.000000
tan(pi/6) = 0.577350
tan(pi/4) = 1.000000
tan(pi/3) = 1.732051
tan(pi/2) = 16331239353195370.000000

バージョン

言語

  • C++03

処理系

  • Clang: 1.9 , 2.9 , 3.1
  • GCC: 3.4.6 , 4.2.4 , 4.3.5 , 4.4.5 , 4.5.1 , 4.5.2 , 4.6.1 , 4.7.0
  • ICC: 10.1 , 11.0 , 11.1 , 12.0
  • Visual C++: 2003 , 2005 , 2008 , 2010

備考

特定の環境では、早期に constexpr 対応されている場合がある:

  • GCC 4.6.1 以上

実装例

tan のマクローリン展開はベルヌーイ数が登場するため計算には向かない。

$$ \tan x = \sum_{n = 1}^{\infty} \frac{B_{2n}(-4)^n(1-4^n)}{(2n)!} x^{2n - 1} \quad \mathrm{for} \; |x| < \frac{\pi}{2} $$

以下の公式から求めることができる。

$$ \tan x = \frac{\sin x}{\cos x} $$

参照