- cmath[meta header]
- std[meta namespace]
- function[meta id-type]
- cpp11[meta cpp]
namespace std {
float fmax(float x, float y); // (1) C++11からC++20まで
double fmax(double x, double y); // (2) C++11からC++20まで
long double
fmax(long double x, long double y); // (3) C++11からC++20まで
constexpr floating-point-type
fmax(floating-point-type x,
floating-point-type y); // (4) C++23
Promoted
fmax(Arithmetic1 x,
Arithmetic2 y); // (5) C++11
constexpr Promoted
fmax(Arithmetic1 x,
Arithmetic2 y); // (5) C++23
float
fmaxf(float x, float y); // (6) C++17
constexpr float
fmaxf(float x, float y); // (6) C++23
long double
fmaxl(long double x, long double y); // (7) C++17
constexpr long double
fmaxl(long double x, long double y); // (7) C++23
}
- Promoted[italic]
- Arithmetic1[italic]
- Arithmetic2[italic]
算術型の最大値を求める。
- (1) :
float
に対するオーバーロード - (2) :
double
に対するオーバーロード - (3) :
long double
に対するオーバーロード - (4) : 浮動小数点数型に対するオーバーロード
- (5) : 算術型に対するオーバーロード (大きい精度にキャストして計算される。整数は
double
で計算される) - (6) :
float
型規定 - (7) :
long double
型規定
引数の最大値を返す。
- 戻り値は正確で、現在の丸めモードに依存しない。
- 処理系が IEC 60559 に準拠している場合(
std::numeric_limits
<T>::
is_iec559
() != false
)、以下の規定が追加される。- 引数の1つが NaN の場合 NaN でない方を返す。
- 引数が2つとも NaN の場合 NaN を返す。
- 理想的には
fmax(-0.0, +0.0)
は+0
を返す。 - C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
#include <cmath>
#include <iostream>
int main() {
std::cout << std::showpos;
std::cout << "fmax( 0.0, -1.0) = " << std::fmax(0.0, -1.0) << std::endl;
std::cout << "fmax(-0.0, +0.0) = " << std::fmax(-0.0, +0.0) << std::endl;
std::cout << "fmax( 0.0, +1.0) = " << std::fmax(0.0, +1.0) << std::endl;
std::cout << "fmax( 0.0, nan) = " << std::fmax(0.0, std::nan("")) << std::endl;
std::cout << "fmax( nan, nan) = " << std::fmax(std::nan(""), std::nan("")) << std::endl;
}
- std::fmax[color ff0000]
- std::showpos[link ../ios/showpos.md]
- std::nan[link ../cmath/nanf.md]
fmax( 0.0, -1.0) = +0
fmax(-0.0, +0.0) = +0
fmax( 0.0, +1.0) = +1
fmax( 0.0, nan) = +0
fmax( nan, nan) = +nan
- C++11
- Clang: 3.0 [mark verified]
- GCC: 4.3.6 [mark verified]
- ICC: ??
- Visual C++: ??
特定の環境では、早期に constexpr
対応されている場合がある:
- GCC 4.6.1 以上
namespace std {
float fmax(float x, float y) {
return (std::isgreaterequal(x, y) || std::isnan(y)) ? x : y;
}
double fmax(double x, double y) {
return (std::isgreaterequal(x, y) || std::isnan(y)) ? x : y;
}
long double fmax(long double x, long double y) {
return (std::isgreaterequal(x, y) || std::isnan(y)) ? x : y;
}
template <typename T, typename U>
auto fmax(T x, U y) -> typename std::enable_if<
std::is_arithmetic<T>::value && std::is_arithmetic<U>::value,
typename std::common_type<T, U, double>::type
>::type {
return (std::isgreaterequal(x, y) || std::isnan(y)) ? x : y;
}
}
- fmax[color ff0000]
- std::isgreaterequal[link ../cmath/isgreaterequal.md]
- std::isnan[link ../cmath/isnan.md]
- std::enable_if[link ../type_traits/enable_if.md]
- std::is_arithmetic[link ../type_traits/is_arithmetic.md]
- std::common_type[link ../type_traits/common_type.md]
- P0533R9 constexpr for
<cmath>
and<cstdlib>
- C++23での、一部関数の
constexpr
対応
- C++23での、一部関数の
- P1467R9 Extended floating-point types and standard names
- C++23で導入された拡張浮動小数点数型への対応として、
float
、double
、long double
のオーバーロードをfloating-point-type
のオーバーロードに統合し、拡張浮動小数点数型も扱えるようにした
- C++23で導入された拡張浮動小数点数型への対応として、