4 数学関数

いつの間にか cbrt() という関数が C にも入っているんだ (cbrt($ x$) $ \sqrt[3]{x}$ を返す)。

hypot() が3つの引数を許すとか (hypot($ x$,$ y$,$ z$) $ \sqrt{x^2+y^2+z^2}$ を返す)。

[
l]test-cmath-func.cpp
// test-cmath-func.cpp
// c++ -std=c++20 -O -o test-cmath-func test-cmath-func.cpp

#include <iostream>
#include <iomanip>  // setprecision()
#include <cmath>
#include <numbers>
using namespace std;

int main(void)
{
  double x=-1.234;
  double integral_part;
  int exponent;
  double pi=std::numbers::pi;
  cout << setprecision(16);
  cout << "x=" << x << endl;
  cout << "abs(x)=" << abs(x) << endl;
  cout << "fabs(x)=" << fabs(x) << endl;
  cout << "sqrt(x)=" << sqrt(x) << endl;
  cout << "sqrt(abs(x))=" << sqrt(abs(x)) << endl;
  cout << "exp(1.0)=" << exp(1.0) << endl;
  cout << "log(exp(x))=" << log(exp(x)) << endl;
  cout << "log10(exp(1.0))=" << log10(exp(1.0)) << endl;
  cout << "cos(std::numbers::pi / 3.0)=" << cos(std::numbers::pi / 3.0) << endl;
  cout << "4*acos(cos(pi/4))=" << 4*acos(cos(pi/4)) << endl;
  cout << "2*(acos(x/2)+asin(x/2))=" << 2*(acos(x/2)+asin(x/2)) << endl;
  cout << "tan(pi/4)=" << tan(pi/4) << endl;
  cout << "4*atan(1.0)=" << 4*atan(1.0) << endl;
  cout << "atan2(-1.0,-1.0)/pi=" << atan2(-1.0,-1.0)/pi << endl;
  cout << "cosh(x)*cosh(x)-sinh(x)*sinh(x)=" << cosh(x)*cosh(x)-sinh(x)*sinh(x)
       << endl;
  cout << "tanh(10.0) =" << tanh(10.0) << endl;
  cout << "modf(12.345,&integral_part)="
       << modf(12.345,&integral_part) << endl;
  cout << "integral_part=" << integral_part << endl;
  cout << "modf(-12.345,&integral_part)="
       << modf(-12.345,&integral_part) << endl;
  cout << "integral_part=" << integral_part << endl;
  cout << "frexp(1024*0.567890123456789,&exponent)="
       <<  frexp(1024*0.567890123456789,&exponent) << endl;
  cout << "exponent=" << exponent << endl;
  cout << "frexp(ldexp(0.567890123456789,10),&exponent)="
       <<  frexp(ldexp(0.567890123456789,10),&exponent) << endl;
  cout << "exponent=" << exponent << endl;
  // C++
  cout << "cbrt((-1.2)*(-1.2)*(-1.2))=" << cbrt((-1.2)*(-1.2)*(-1.2)) << endl;
  cout << "ceil(1.5)=" << ceil(1.5) << ", ceil(-1.5)=" << ceil(-1.5) << endl;
  cout << "floor(1.5)=" << floor(1.5) << ", floor(-1.5)=" << floor(-1.5) << endl;
  cout << "exp2(10.0)=" << exp2(10.0) << endl;
  cout << "expm1(1.23456789012345e-8)=" << expm1(1.23456789012345e-8) << endl;
  cout << "exp(1.23456789012345e-8)-1=" << exp(1.23456789012345e-8)-1 << endl;
  cout << "hypot(3e200,4e200)=" << hypot(3e200,4e200) << endl;
  cout << "sqrt(3e200*3e200+4e200*4e200)=" << sqrt(3e200*3e200+4e200*4e200) << endl;
  cout << "hypot(3.0,4.0,12.0)=" << hypot(3.0,4.0,12.0) << " (3^2+4^2=5^2, 5^2+12^2=13^2)" << endl;
}
コンパイル&実行結果
% c++ -std=c++20 -O -o test-cmath-func test-cmath-func.cpp
% ./test-cmath-func
x=-1.234
abs(x)=1.234
fabs(x)=1.234
sqrt(x)=nan
sqrt(abs(x))=1.110855526159905
exp(1.0)=2.718281828459045
log(exp(x))=-1.234
log10(exp(1.0))=0.4342944819032518
cos(std::numbers::pi / 3.0)=0.5000000000000001
4*acos(cos(pi/4))=3.141592653589793
2*(acos(x/2)+asin(x/2))=3.141592653589794
tan(pi/4)=0.9999999999999999
4*atan(1.0)=3.141592653589793
atan2(-1.0,-1.0)/pi=-0.75
cosh(x)*cosh(x)-sinh(x)*sinh(x)=1
tanh(10.0) =0.9999999958776927
modf(12.345,&integral_part)=0.3450000000000006
integral_part=12
modf(-12.345,&integral_part)=-0.3450000000000006
integral_part=-12
frexp(1024*0.567890123456789,&exponent)=0.567890123456789
exponent=10
frexp(ldexp(0.567890123456789,10),&exponent)=0.567890123456789
exponent=10
cbrt((-1.2)*(-1.2)*(-1.2))=-1.2
ceil(1.5)=2, ceil(-1.5)=-1
floor(1.5)=1, floor(-1.5)=-2
exp2(10.0)=1024
expm1(1.23456789012345e-8)=1.234567897744239e-08
exp(1.23456789012345e-8)-1=1.234567892360872e-08
hypot(3e200,4e200)=5e+200
sqrt(3e200*3e200+4e200*4e200)=inf
hypot(3.0,4.0,12.0)=13 (3^2+4^2=5^2, 5^2+12^2=13^2)
%


桂田 祐史