Namespaces
Variants

div, ldiv, lldiv, imaxdiv

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
div ldiv lldiv imaxdiv
(C99) (C99)
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
div_t ldiv_t lldiv_t imaxdiv_t
(C99) (C99)
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <stdlib.h>
div_t     div ( int x, int y ) ;
(1)
ldiv_t    ldiv ( long x, long y ) ;
(2)
lldiv_t   lldiv ( long long x, long long y ) ;
(3) (C99 起)
定义于头文件 <inttypes.h>
imaxdiv_t imaxdiv ( intmax_t x, intmax_t y ) ;
(4) (C99 起)

计算分子 x 除以分母 y 的商和余数。

同时计算商和余数。商为代数商并舍弃小数部分(向零截断)。余数满足 quot * y + rem == x

(C99前)

同时计算商(表达式 x / y 的结果)和余数(表达式 x % y 的结果)。

(C99起)

目录

参数

x, y - 整数值

返回值

若余数和商均能以对应类型( int long long long intmax_t )的对象形式表示,则返回由以下方式定义的 div_t ldiv_t lldiv_t imaxdiv_t 类型对象:

div_t

struct div_t { int quot; int rem; };

struct div_t { int rem; int quot; };

ldiv_t

struct ldiv_t { long quot; long rem; };

struct ldiv_t { long rem; long quot; };

lldiv_t

struct lldiv_t { long long quot; long long rem; };

struct lldiv_t { long long rem; long long quot; };

imaxdiv_t

struct imaxdiv_t { intmax_t quot; intmax_t rem; };

struct imaxdiv_t { intmax_t rem; intmax_t quot; };

如果余数或商无法被表示,则行为是未定义的。

注释

在C99标准之前,如果任一操作数为负,内置除法和取余运算符中商的舍入方向和余数的符号由实现定义,但在 div ldiv 函数中这些行为是明确规定的。

在许多平台上,单条CPU指令可同时获取商和余数,尽管编译器通常能够在适当时机合并相邻的 / % 运算符,但本函数仍可能利用这一特性。

示例

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void reverse(char* first, char* last)
{
    for (--last; first < last; ++first, --last)
    {
        char c = *last;
        *last = *first;
        *first = c;
    }
}
// 在缓冲区溢出时返回空缓冲区
char* itoa(int n, int base, char* buf, size_t buf_size)
{
    assert(2 <= base && base <= 16 && buf && buf_size);
    div_t dv = {.quot = n};
    char* p = buf;
    do
    {
        if (!--buf_size)
            return (*buf = '\0'), buf;
        dv = div(dv.quot, base);
        *p++ = "0123456789abcdef"[abs(dv.rem)];
    }
    while(dv.quot);
    if (n < 0)
        *p++ = '-';
    *p = '\0';
    reverse(buf, p);
    return buf;
}
int main(void)
{
    char buf[16];
    printf("%s\n", itoa(0, 2, buf, sizeof buf));
    printf("%s\n", itoa(007, 3, buf, sizeof buf));
    printf("%s\n", itoa(12346, 10, buf, sizeof buf));
    printf("%s\n", itoa(-12346, 10, buf, sizeof buf));
    printf("%s\n", itoa(-42, 2, buf, sizeof buf));
    printf("%s\n", itoa(INT_MAX, 16, buf, sizeof buf));
    printf("%s\n", itoa(INT_MIN, 16, buf, sizeof buf));
}

可能的输出:

0
21
12346
-12346
-101010
7fffffff
-80000000

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.8.2.2 imaxdiv 函数 (p: TBD)
  • 7.22.6.2 div、ldiv 和 lldiv 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.8.2.2 imaxdiv 函数 (页: 159)
  • 7.22.6.2 div、ldiv 和 lldiv 函数 (页: 259)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.8.2.2 imaxdiv 函数 (页码: 219)
  • 7.22.6.2 div、ldiv 和 lldiv 函数 (页码: 356)
  • C99标准(ISO/IEC 9899:1999):
  • 7.8.2.2 imaxdiv函数(页码:200)
  • 7.20.6.2 div、ldiv和lldiv函数(页码:320)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.10 div_t, ldiv_t
  • 4.10.6.2 div 函数
  • 4.10.6.4 ldiv 函数

参见

(C99) (C99)
计算浮点除法运算的余数
(函数)
计算浮点除法运算的有符号余数
(函数)
(C99) (C99) (C99)
计算有符号余数及除法运算的最后三位比特
(函数)
C++ 文档 for div

外部链接

1. 欧几里得除法 —— 摘自维基百科
2. 模运算(及截断除法) —— 摘自维基百科