Skip to content

Commit

Permalink
abs, frexp, ilogb, ldexp : constexpr対応 #1067
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Feb 16, 2023
1 parent a26647a commit d3cb083
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 33 deletions.
43 changes: 35 additions & 8 deletions reference/cmath/abs.md
Expand Up @@ -6,22 +6,41 @@

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

double abs(Integral x); // (4) C++11 から C++14 まで
double abs(double x); // (2) C++03からC++20まで

int abs(int x); // (5) C++17 から
long abs(long int x); // (6) C++17 から
long long abs(long long x); // (7) C++17 から
long double abs(long double x); // (3) C++03からC++20まで

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

double abs(Integral x); // (5) C++11 から C++14 まで

int abs(int x); // (6) C++17
constexpr int abs(int x); // (6) C++23

long abs(long int x); // (7) C++17
constexpr long abs(long int x); // (7) C++23

long long abs(long long x); // (8) C++17
constexpr long long abs(long long x); // (8) C++23
}
```
* Integral[italic]
## 概要
浮動小数点数型の絶対値を求める。abs は absolute value(絶対値)の略。
- (1) : `float`に対するオーバーロード
- (2) : `double`に対するオーバーロード
- (3) : `long double`に対するオーバーロード
- (4) : 浮動小数点数型に対するオーバーロード
- (5) : 整数型に対するオーバーロード (`double`にキャストして計算される)
- (6) : `int`に対するオーバーロード
- (7) : `long int`に対するオーバーロード
- (8) : `long long`に対するオーバーロード
## 戻り値
引数 `x` の絶対値を返す。
Expand All @@ -31,7 +50,8 @@ namespace std {
## 備考
- $$ f(x) = | x | $$
- 任意の整数型に対するオーバーロード(4)は C++11 で追加されたが、[一部の符号なし整数型に対して問題を引き起こす](http://wg21.cmeerw.net/lwg/issue2192)ことから C++17 で削除され、符号付き整数型に対するオーバーロード(5),(6),(7)が追加された。
- 任意の整数型に対するオーバーロード(5)は C++11 で追加されたが、[一部の符号なし整数型に対して問題を引き起こす](http://wg21.cmeerw.net/lwg/issue2192)ことから C++17 で削除され、符号付き整数型に対するオーバーロード(6),(7),(8)が追加された
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
## 例
Expand Down Expand Up @@ -76,3 +96,10 @@ namespace std {
}
}
```
## 参照
- [P0533R9 constexpr for `<cmath>` and `<cstdlib>`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf)
- C++23での、一部関数の`constexpr`対応
- [P1467R9 Extended floating-point types and standard names](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1467r9.html)
- C++23で導入された拡張浮動小数点数型への対応として、`float`、`double`、`long double`のオーバーロードを`floating-point-type`のオーバーロードに統合し、拡張浮動小数点数型も扱えるようにした
47 changes: 36 additions & 11 deletions reference/cmath/frexp.md
Expand Up @@ -5,25 +5,46 @@

```cpp
namespace std {
float frexp(float value, int* exp);
double frexp(double value, int* exp);
long double frexp(long double value, int* exp);

double frexp(Integral value, int* exp); // C++11 から

float frexpf(float value, int* exp); // C++17 から
long double frexpl(long double value, int* exp); // C++17 から
float frexp(float value, int* exp); // (1) C++03からC++20まで
double frexp(double value, int* exp); // (2) C++03からC++20まで
long double frexp(long double value, int* exp); // (3) C++03からC++20まで

constexpr floating-point-type
frexp(floating-point-type value, int* exp); // (4) C++23

double
frexp(Integral value, int* exp); // (5) C++11
constexpr double
frexp(Integral value, int* exp); // (5) C++23

float
frexpf(float value, int* exp); // (6) C++17
constexpr float
frexpf(float value, int* exp); // (6) C++23

long double
frexpl(long double value, int* exp); // (7) C++17
constexpr long double
frexpl(long double value, int* exp); // (7) C++23
}
```
* Integral[italic]
## 概要
`frexp`関数 (fraction and exponent)は、浮動小数点数`value`を、正規化された仮数部と 2 の累乗へ分解する。
`frexp`関数 (fraction and exponent) は、浮動小数点数`value`を、正規化された仮数部と 2 の累乗へ分解する。
この関数は、与えられた浮動小数点数`value`を仮数部と指数部に分解し、仮数部を戻り値で返し、指数を`*exp`に書き込んで返す。
この関数と反対に、[`std::ldexp()`](ldexp.md)関数を使用することで、仮数部と指数部の値から浮動小数点数を作り出せる。
- (1) : `float`に対するオーバーロード
- (2) : `double`に対するオーバーロード
- (3) : `long double`に対するオーバーロード
- (4) : 浮動小数点数型に対するオーバーロード
- (5) : 整数型に対するオーバーロード (`double`にキャストして計算される)
- (6) : `float`型規定
- (7) : `long double`型規定
## 戻り値
引数 `value` が浮動小数点数ではない、もしくは2の乗数が `int` の範囲外である場合、戻り値は未規定。
Expand All @@ -39,7 +60,8 @@ C++11 以降では、処理系が IEC 60559 に準拠している場合([`std:
- `value = ±0` の場合、戻り値は `±0` となり、`*exp` にはゼロが設定される。
- `value = ±∞` の場合、戻り値は `±∞` となり、`*exp` には未規定の値が設定される。
- `value` が NaN の場合、戻り値は NaN となり、`*exp` には未規定の値が設定される。
- `frexp` は浮動小数点例外を全く発生させない。
- この関数は浮動小数点例外を発生させない。
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
## 例
Expand Down Expand Up @@ -68,4 +90,7 @@ int main()
## 参照
- [Why does `frexp()` not yield scientific notation?](http://stackoverflow.com/questions/24928833/why-does-frexp-not-yield-scientific-notation)
- `frexp()`が戻り値を`[1, 2)`の範囲ではなく、`[0.5, 1)`の範囲に収めるようにしている理由は、IEEE 754およびISO/IEC 60559が策定される前に作られた関数であることが理由と考えられる

- [P0533R9 constexpr for `<cmath>` and `<cstdlib>`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf)
- C++23での、一部関数の`constexpr`対応
- [P1467R9 Extended floating-point types and standard names](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1467r9.html)
- C++23で導入された拡張浮動小数点数型への対応として、`float``double``long double`のオーバーロードを`floating-point-type`のオーバーロードに統合し、拡張浮動小数点数型も扱えるようにした
41 changes: 35 additions & 6 deletions reference/cmath/ilogb.md
Expand Up @@ -6,21 +6,42 @@

```cpp
namespace std {
int ilogb(float);
int ilogb(double);
int ilogb(long double);
int ilogb(float x); // (1) C++11からC++20まで
int ilogb(double x); // (2) C++11からC++20まで
int ilogb(long double x); // (3) C++11からC++20まで

int ilogb(Integral);
constexpr int
ilogb(floating-point-type x); // (3) C++23

int ilogbf(float x); // C++17 から
int ilogbl(long double x); // C++17 から
int
ilogb(Integral); // (4) C++11
constexpr int
ilogb(Integral); // (4) C++23

int
ilogbf(float x); // (5) C++17
constexpr int
ilogbf(float x); // (5) C++23

int
ilogbl(long double x); // (6) C++17
constexpr int
ilogbl(long double x); // (6) C++23
}
```
* Integral[italic]
## 概要
`ilogb`関数(integer log binary)は、浮動小数点数 `x` の指数部を `int` として返す。
- (1) : `float`に対するオーバーロード
- (2) : `double`に対するオーバーロード
- (3) : `long double`に対するオーバーロード
- (4) : 浮動小数点数型に対するオーバーロード
- (5) : 整数型に対するオーバーロード (`double`にキャストして計算される)
- (6) : `float`型規定
- (7) : `long double`型規定
## 戻り値
`x` がゼロの場合は [`FP_ILOGB0`](fp_ilogb0.md) を、無限大の場合は [`INT_MAX`](/reference/climits/int_max.md) を、`NaN` の場合は [`FP_ILOGBNAN`](fp_ilogbnan.md) を返す。
Expand All @@ -34,6 +55,7 @@ namespace std {
- 正しい結果が戻り値の型(`int`)の範囲で表現可能な場合は、戻り値は正確で、現在の丸め方式に依存しない。
- 正しい結果が戻り値の型(`int`)の範囲外の場合は、戻り値は未規定で、[`FE_INVALID`](../cfenv/fe_invalid.md)(無効演算浮動小数点例外)が発生する。
- `x` がゼロ、無限大、あるいは NaN の場合には、[`FE_INVALID`](../cfenv/fe_invalid.md)(無効演算浮動小数点例外)が発生する。
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
## 例
Expand Down Expand Up @@ -74,3 +96,10 @@ ilogb(1e-309) = -1027
- [GCC](/implementation.md#gcc): 4.3.6
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 参照
- [P0533R9 constexpr for `<cmath>` and `<cstdlib>`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf)
- C++23での、一部関数の`constexpr`対応
- [P1467R9 Extended floating-point types and standard names](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1467r9.html)
- C++23で導入された拡張浮動小数点数型への対応として、`float``double``long double`のオーバーロードを`floating-point-type`のオーバーロードに統合し、拡張浮動小数点数型も扱えるようにした
44 changes: 36 additions & 8 deletions reference/cmath/ldexp.md
Expand Up @@ -5,14 +5,27 @@

```cpp
namespace std {
float ldexp(float x, int exp);
double ldexp(double x, int exp);
long double ldexp(long double x, int exp);

double ldexp(Integral x, int exp); // C++11 から

float ldexpf(float x, int exp); // C++17 から
long double ldexpl(long double x, int exp); // C++17 から
float ldexp(float x, int exp); // (1) C++03からC++20まで
double ldexp(double x, int exp); // (2) C++03からC++20まで
long double ldexp(long double x, int exp); // (3) C++03からC++20まで

constexpr floating-point-type
ldexp(floating-point-type x, int exp); // (4) C++23

double
ldexp(Integral x, int exp); // (5) C++11
constexpr double
ldexp(Integral x, int exp); // (5) C++23

float
ldexpf(float x, int exp); // (6) C++17
constexpr float
ldexpf(float x, int exp); // (6) C++23

long double
ldexpl(long double x, int exp); // (7) C++17
constexpr long double
ldexpl(long double x, int exp); // (7) C++23
}
```
* Integral[italic]
Expand All @@ -24,6 +37,14 @@ namespace std {
この関数と反対に、[`std::frexp()`](frexp.md)関数を使用することで、浮動小数点数を仮数部と指数部に分解できる。
- (1) : `float`に対するオーバーロード
- (2) : `double`に対するオーバーロード
- (3) : `long double`に対するオーバーロード
- (4) : 浮動小数点数型に対するオーバーロード
- (5) : 整数型に対するオーバーロード (`double`にキャストして計算される)
- (6) : `float`型規定
- (7) : `long double`型規定
## 戻り値
<code>x * 2<sup>exp</sup></code>
Expand All @@ -34,6 +55,7 @@ namespace std {
## 備考
- オーバーフローエラー、アンダーフローエラーが発生した場合の挙動については、[`<cmath>`](../cmath.md) を参照。
- C++11 以降では、処理系が IEC 60559 に準拠している場合([`std::numeric_limits`](../limits/numeric_limits.md)`<T>::`[`is_iec559`](../limits/numeric_limits/is_iec559.md)`() != false`)、かつ、基数が 2 の場合([`std::numeric_limits`](../limits/numeric_limits.md)`<T>::`[`radix`](../limits/numeric_limits/radix.md)`() == 2`)、[`scalbn`](scalbn.md)`(x, exp)` と等価である。
- C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された
## 例
Expand Down Expand Up @@ -89,3 +111,9 @@ namespace std {
}
```
* std::pow[link pow.md]
## 参照
- [P0533R9 constexpr for `<cmath>` and `<cstdlib>`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0533r9.pdf)
- C++23での、一部関数の`constexpr`対応
- [P1467R9 Extended floating-point types and standard names](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1467r9.html)
- C++23で導入された拡張浮動小数点数型への対応として、`float`、`double`、`long double`のオーバーロードを`floating-point-type`のオーバーロードに統合し、拡張浮動小数点数型も扱えるようにした

0 comments on commit d3cb083

Please sign in to comment.