When C has the /
operator to divide two numbers, what is the purpose of having the div()
library function?
Is there any scenario where /
can't be used but div()
can?
Use the DIV function to calculate the value of the quotient after dividend is divided by divisor. The dividend and divisor expressions can evaluate to any numeric value. The only exception is that divisor cannot be 0. If either dividend or divisor evaluates to the null value, null is returned.
The div() function in C++ computes the integral quotient and remainder of the division of two numbers. The div() function is defined in <cstdlib> header file.
printf("Enter dividend: "); scanf("%d", ÷nd); printf("Enter divisor: "); scanf("%d", &divisor); Then the quotient is evaluated using / (the division operator), and stored in quotient . quotient = dividend / divisor; Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder .
DIV() function :This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned.
From the C99 Rationale document:
(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.
Quoted from C Programming: A Modern Approach, 2nd Edition, chapter 26, Q & A section.
Q: Why do the div and ldiv functions exist? Can't we just use the / and % operators?
A:
div
andldiv
aren't quite the same as/
and%
. Recall from Section 4.1 that applying/
and%
to negative operands doesn't give a portable result in C89. Ifi
orj
is negative, whether the value ofi / j
is rounded up or down is implementation defined, as is the sign ofi % j
. The answer computed bydiv
andldiv
, on the other hand, don't depend on the implementation. The quotient is rounded toward zero; the remainder is computed according to the formulan = q x d + r
, wheren
is the original number,q
is the quotient,d
is the divisor, andr
is the remainder. Here are a few examples:n | d | q | r --------|--------|--------|-------- 7 | 3 | 2 | 1 -7 | 3 | -2 | -1 7 | -3 | -2 | 1 -7 | -3 | 2 | -1
In C99, the
/
and%
operators are guaranteed to produce the same result asdiv
andldiv
.Efficiency is the other reason that
div
andldiv
exist. Many machines have an instruction that can compute both the quotient and remainder, so callingdiv
orldiv
may be faster than using the/
and%
operators separately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With