sqrt、sqrtf、sqrtl を使ってみる (C++)
C++
Published: 2020-03-28

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

3の平方根を計算してみます。

今回は下記の関数を使ってみます。

  • sqrtf (float)
  • sqrt (double)
  • sqrtl (long double)

test.cpp

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    printf("%.80f\n", sqrtf((float)3));
    printf("%.80f\n", sqrt(3));
    printf("%.80Lf\n", sqrtl((long double)3));
}

出力結果

1.73205077648162841796875000000000000000000000000000000000000000000000000000000000
1.73205080756887719317660412343684583902359008789062500000000000000000000000000000
1.73205080756887729357372529559455642811371944844722747802734375000000000000000000

参考