c++でのfloor関数の使い方は何ですか。
C++において、floor関数は、指定された浮動小数点数以下の最大の整数値を返す数学関数です。
floor関数の使い方は以下のとおりです:
#include <iostream>
#include <cmath>
int main() {
double x = 2.7;
double y = -3.4;
// 返回小于或等于x的最大整数值,结果为2
std::cout << "floor(" << x << ") = " << floor(x) << std::endl;
// 返回小于或等于y的最大整数值,结果为-4
std::cout << "floor(" << y << ") = " << floor(y) << std::endl;
return 0;
}
結果の出力:
floor(2.7) = 2
floor(-3.4) = -4
floor関数を使用するには、ヘッダーファイルを含める必要があります。浮動小数点数を関数の引数に渡すと、floor関数はその浮動小数点数以下の最大整数値を返します。