div()库函数的作用是什么? [英] What is the purpose of the div() library function?

查看:137
本文介绍了div()库函数的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当C使用 / 运算符将两个数相除时, div()库函数

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

是否存在无法使用 / div()可以吗?

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

推荐答案

从C99基本原理文档中:

From the C99 Rationale document:


(7.20.6.2 div,ldiv和lldiv函数)因为C89在涉及负操作数,div和ldiv和C99中的divdiv时具有实现定义的语义来划分有符号整数被发明来为带符号的整数除法和余数运算提供明确指定的语义。采用的语义与Fortran中的相同。由于这些函数同时返回商和余数,因此它们还用作高效建模基础硬件的便捷方法,
将这两个结果作为同一操作的一部分进行计算。 [...]
现在C99要求除法运算符具有相似的语义,所以新的
程序使用div,ldiv或lldiv的主要原因是要同时获得商和余数。

(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.

这篇关于div()库函数的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆