div()库函数的目的是什么?

时间:2021-01-24 07:20:12

When c has the / operator to divide two numbers, what is the purpose of having the div() library function?

当c让/操作符划分两个数字时,div()库函数的目的是什么?

Is there any scenario where / can't be used but div() can?

是否存在可以使用/不能使用div()的场景?

4 个解决方案

#1


12  

From the C99 Rationale document:

根据C99基本原理文件:

(7.20.6.2 The div, ldiv, and lldiv functions) Because C89 had implementation-defined semantics for division of signed integers when negative operands were involved, div and ldiv, and lldiv in C99, were invented to provide well-specified semantics for signed integer division and remainder operations. The semantics were adopted to be the same as in Fortran. Since these functions return both the quotient and the remainder, they also serve as a convenient way of efficiently modeling underlying hardware that computes both results as part of the same operation. [...] Now that C99 requires similar semantics for the division operator, the main reason for new programs to use div, ldiv or lldiv is to simultaneously obtain quotient and remainder.

(7.20.6.2 div、ldiv和lldiv函数)因为C89在涉及负操作数时对有符号整数除法有实现定义的语义,所以在C99中发明了div和ldiv以及lldiv,以便为有符号整数除法和余数运算提供指定良好的语义。采用与Fortran相同的语义。由于这些函数同时返回商和余数,因此它们也可以作为一种方便的方法,可以有效地对底层硬件进行建模,从而在相同的操作中计算两个结果。[…既然C99要求分部运算符具有类似的语义,那么新程序使用div、ldiv或lldiv的主要原因是同时获得商和余数。

#2


2  

div_t is a structure, which contains a quotient member and a remainder member. For example :

div_t是一个结构,它包含一个商成员和一个剩余成员。例如:

typedef struct {
    int quot;
    int rem;
} div_t;

Some simple implementations of the div function use / and % operators. You can also see this topic.

div函数的一些简单实现使用/和%操作符。您也可以看到这个主题。

#3


2  

div() returns the result of the division and the remainder. So you don't have to use % operator to find the remainder.

div()返回分区的结果和其余部分。所以不需要用%运算符来求余数。

#4


1  

As other people have mentioned, div() will give you both the quotient and the remainder. This is because the (software) integer division algorithm that most C runtimes use computes both at the same time.

正如其他人提到的,div()将给出商和余数。这是因为大多数C运行时使用的(软件)整数除法同时计算两者。

If the target computer does not have a hardware divider, the / operator typically gets turned into a call to div() and the remainder gets thrown away. The same thing happens with the % operator except that it's the quotient that gets tossed. So if you're doing something like this:

如果目标计算机没有硬件分隔符,那么/操作符通常会被转换为对div()的调用,其余的会被丢弃。同样的事情发生在%运算符上,除了它是被抛出的商。如果你这样做:

quot = a / b;
rem = a % b;

you're calling the divide routine twice (and divide is pretty slow if your computer doesn't do it in hardware). So it's faster to use div() to get both.

您将调用划分例程两次(如果您的计算机在硬件上不这样做,那么divide是相当慢的)。所以使用div()来同时获得这两个选项会更快。

(Of course, it's also less readable and whether you actually gain any performance advantage depends on your specific platform and compiler. You should only ever switch to div() if you've determined that it's a performance bottleneck. Remember what Knuth said about premature optimization.)

当然,它的可读性也较差,您是否真正获得性能优势取决于您的特定平台和编译器。如果您确定div()是性能瓶颈,那么您应该只切换到div()。还记得Knuth说过的关于过早优化的观点吗?

#1


12  

From the C99 Rationale document:

根据C99基本原理文件:

(7.20.6.2 The div, ldiv, and lldiv functions) Because C89 had implementation-defined semantics for division of signed integers when negative operands were involved, div and ldiv, and lldiv in C99, were invented to provide well-specified semantics for signed integer division and remainder operations. The semantics were adopted to be the same as in Fortran. Since these functions return both the quotient and the remainder, they also serve as a convenient way of efficiently modeling underlying hardware that computes both results as part of the same operation. [...] Now that C99 requires similar semantics for the division operator, the main reason for new programs to use div, ldiv or lldiv is to simultaneously obtain quotient and remainder.

(7.20.6.2 div、ldiv和lldiv函数)因为C89在涉及负操作数时对有符号整数除法有实现定义的语义,所以在C99中发明了div和ldiv以及lldiv,以便为有符号整数除法和余数运算提供指定良好的语义。采用与Fortran相同的语义。由于这些函数同时返回商和余数,因此它们也可以作为一种方便的方法,可以有效地对底层硬件进行建模,从而在相同的操作中计算两个结果。[…既然C99要求分部运算符具有类似的语义,那么新程序使用div、ldiv或lldiv的主要原因是同时获得商和余数。

#2


2  

div_t is a structure, which contains a quotient member and a remainder member. For example :

div_t是一个结构,它包含一个商成员和一个剩余成员。例如:

typedef struct {
    int quot;
    int rem;
} div_t;

Some simple implementations of the div function use / and % operators. You can also see this topic.

div函数的一些简单实现使用/和%操作符。您也可以看到这个主题。

#3


2  

div() returns the result of the division and the remainder. So you don't have to use % operator to find the remainder.

div()返回分区的结果和其余部分。所以不需要用%运算符来求余数。

#4


1  

As other people have mentioned, div() will give you both the quotient and the remainder. This is because the (software) integer division algorithm that most C runtimes use computes both at the same time.

正如其他人提到的,div()将给出商和余数。这是因为大多数C运行时使用的(软件)整数除法同时计算两者。

If the target computer does not have a hardware divider, the / operator typically gets turned into a call to div() and the remainder gets thrown away. The same thing happens with the % operator except that it's the quotient that gets tossed. So if you're doing something like this:

如果目标计算机没有硬件分隔符,那么/操作符通常会被转换为对div()的调用,其余的会被丢弃。同样的事情发生在%运算符上,除了它是被抛出的商。如果你这样做:

quot = a / b;
rem = a % b;

you're calling the divide routine twice (and divide is pretty slow if your computer doesn't do it in hardware). So it's faster to use div() to get both.

您将调用划分例程两次(如果您的计算机在硬件上不这样做,那么divide是相当慢的)。所以使用div()来同时获得这两个选项会更快。

(Of course, it's also less readable and whether you actually gain any performance advantage depends on your specific platform and compiler. You should only ever switch to div() if you've determined that it's a performance bottleneck. Remember what Knuth said about premature optimization.)

当然,它的可读性也较差,您是否真正获得性能优势取决于您的特定平台和编译器。如果您确定div()是性能瓶颈,那么您应该只切换到div()。还记得Knuth说过的关于过早优化的观点吗?