Skip to content

Commit

Permalink
<cstdlib> : divを追加 #1067 #1022
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Feb 20, 2023
1 parent 2d01c56 commit 72d23cc
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 9 deletions.
18 changes: 9 additions & 9 deletions reference/cstdlib.md
Expand Up @@ -80,15 +80,15 @@

| 名前 | 説明 | 対応バージョン |
|------|------|----------------|
| [`abs`](cstdlib/abs.md) | `int`の絶対値を取得する (function) | |
| [`labs`](cstdlib/abs.md) | `long`の絶対値を取得する (function) | |
| [`llabs`](cstdlib/abs.md) | `long long`の絶対値を取得する (function) | C++11 |
| `div_t` | `div`関数の戻り値型 (class) | |
| `div` | `int`の除算を行う (function) | |
| `ldiv_t` | `div`関数の戻り値型 (class) | |
| `ldiv` | `long`の除算を行う (functon) | |
| `lldiv_t` | `div`関数の戻り値型 (class) | C++11 |
| `lldiv` | `long long`の除算を行う (function) | C++11 |
| [`abs`](cstdlib/abs.md) | `int`の絶対値を取得する (function) | |
| [`labs`](cstdlib/abs.md) | `long`の絶対値を取得する (function) | |
| [`llabs`](cstdlib/abs.md) | `long long`の絶対値を取得する (function) | C++11 |
| [`div_t`](cstdlib/div_t.md) | `div`関数の戻り値型 (class) | |
| [`div`](cstdlib/div.md) | `int`の除算と剰余算を行う (function) | |
| [`ldiv_t`](cstdlib/ldiv_t.md) | `ldiv`関数の戻り値型 (class) | |
| [`ldiv`](cstdlib/div.md) | `long`の除算と剰余算を行う (functon) | |
| [`lldiv_t`](cstdlib/lldiv_t.md) | `lldiv`関数の戻り値型 (class) | C++11 |
| [`lldiv`](cstdlib/div.md) | `long long`の除算と剰余算を行う (function) | C++11 |


## マルチバイト文字とワイド文字の変換
Expand Down
96 changes: 96 additions & 0 deletions reference/cstdlib/div.md
@@ -0,0 +1,96 @@
# div
* cstdlib[meta header]
* std[meta namespace]
* function[meta id-type]

```cpp
namespace std {
div_t
div(int numer,
int denom); // (1) C++03
constexpr div_t
div(int numer,
int denom); // (1) C++23

ldiv_t
div(long numer,
long denom); // (2) C++03
constexpr ldiv_t
div(long numer,
long denom); // (2) C++23

lldiv_t
div(long long numer,
long long denom); // (3) C++03
constexpr lldiv_t
div(long long numer,
long long denom); // (3) C++23

ldiv_t
ldiv(long numer,
long denom); // (4) C++03
constexpr ldiv_t
ldiv(long numer,
long denom); // (4) C++23

lldiv_t
lldiv(long long numer,
long long denom); // (5) C++11
constexpr lldiv_t
lldiv(long long numer,
long long denom); // (5) C++23
}
```
* div_t[link div_t.md]
* ldiv_t[link ldiv_t.md]
* lldiv_t[link lldiv_t.md]
## 概要
`number / denom`と`number % denom`の計算をひとつの操作で行う。
- (1) : `int`型のオーバーロード
- (2) : `long`型のオーバーロード
- (3) : `long long`型のオーバーロード
- (4) : `long`型規定
- (5) : `long long`型規定
## 戻り値
戻り値型となるクラスの`quot`に商、`rem`に剰余を代入して返す。
結果のどちらかが表現できない場合は未定義動作となる。
## 例
```cpp example
#include <iostream>
#include <cstdlib>
int main()
{
std::div_t x = std::div(5, 2);
std::cout << x.quot << std::endl;
std::cout << x.rem << std::endl;
}
```
* std::div[color ff0000]
* std::div_t[link div_t.md]

### 出力
```
2
1
```


### 備考
特定の環境では、早期に `constexpr` 対応されている場合がある:

- GCC 4.6.1 以上


## 参照
- [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: 41 additions & 0 deletions reference/cstdlib/div_t.md
@@ -0,0 +1,41 @@
# div_t
* cstdlib[meta header]
* std[meta namespace]
* class[meta id-type]

```cpp
namespace std {
struct div_t {
int quot;
int rem;
};
}
```
## 概要
[`std::div()`](div.md)関数の戻り値。
`quot`は「quotient (商)」、`rem`は「remainder (剰余)」。
## 例
```cpp example
#include <iostream>
#include <cstdlib>
int main()
{
std::div_t x = std::div(5, 2);
std::cout << x.quot << std::endl;
std::cout << x.rem << std::endl;
}
```
* std::div_t[color ff0000]
* std::div[link div.md]

### 出力
```
2
1
```

41 changes: 41 additions & 0 deletions reference/cstdlib/ldiv_t.md
@@ -0,0 +1,41 @@
# ldiv_t
* cstdlib[meta header]
* std[meta namespace]
* class[meta id-type]

```cpp
namespace std {
struct ldiv_t {
long quot;
long rem;
};
}
```
## 概要
[`std::div()`](div.md)関数の`long`版の戻り値。
`quot`は「quotient (商)」、`rem`は「remainder (剰余)」。
## 例
```cpp example
#include <iostream>
#include <cstdlib>
int main()
{
std::ldiv_t x = std::div(5L, 2L);
std::cout << x.quot << std::endl;
std::cout << x.rem << std::endl;
}
```
* std::ldiv_t[color ff0000]
* std::div[link div.md]

### 出力
```
2
1
```

42 changes: 42 additions & 0 deletions reference/cstdlib/lldiv_t.md
@@ -0,0 +1,42 @@
# lldiv_t
* cstdlib[meta header]
* std[meta namespace]
* class[meta id-type]
* cpp11[meta cpp]

```cpp
namespace std {
struct lldiv_t {
long long quot;
long long rem;
};
}
```
## 概要
[`std::div()`](div.md)関数の`long long`版の戻り値。
`quot`は「quotient (商)」、`rem`は「remainder (剰余)」。
## 例
```cpp example
#include <iostream>
#include <cstdlib>
int main()
{
std::lldiv_t x = std::div(5LL, 2LL);
std::cout << x.quot << std::endl;
std::cout << x.rem << std::endl;
}
```
* std::lldiv_t[color ff0000]
* std::div[link div.md]

### 出力
```
2
1
```

0 comments on commit 72d23cc

Please sign in to comment.