What is the purpose of the library function div ()?

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

Is there a scenario in which / cannot be used, but div() can?

+3
c standard-library division
Jul 30 2018-12-12T00:
source share
5 answers

From C99 Justification:

(7.20.6.2 Functions div, ldiv and lldiv) Since C89 had a certain implementation of semantics for the division of signed integers when negative operands were involved, div and ldiv and lldiv in C99 were invented to provide well-defined semantics for operations with integers with familiar and leftover. The semantics were adopted in the same way as in Fortran. Since these functions return both a factor and a remainder, they also serve as a convenient way to efficiently simulate basic equipment that calculates 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 should be both coefficient and remainder.

+13
Jul 30 2018-12-12T00:
source share

div_t is a structure containing a member factor and a remainder. For example:

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

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

+2
Jul 30 '12 at 16:45
source share

div() returns the result division and remainder . Therefore, you do not need to use the % operator to search for remainder .

+2
Jul 30 2018-12-12T00:
source share

Quoted from C Programming: A Modern Approach, 2nd Edition , Chapter 26, Q & A Section.

Q: Why are there div and ldiv functions? Can't we just use the / and% operators?

A: div and ldiv don't exactly match / and % . Recall from Section 4.1 that applying / and % to negative operands does not produce a portable result in C89. If i or j negative, will the value of i / j rounded up or down according to the implementation defined as well as the sign of i % j . The answer calculated by div and ldiv , on the other hand, is implementation independent . The coefficient is rounded to zero; the remainder is calculated by the formula n = qxd + r , where n is the original number, q is the quotient, d is the divisor, and r is the remainder. Here are some 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 give the same result as the div and ldiv .

Efficiency is another reason div and ldiv . Many machines have instructions that can calculate both the quotient and the remainder, the so-called div or ldiv can be faster than using the / and % operators separately.

+2
Nov 10 '18 at 3:09
source share

As other people have said, div() will provide you with the private and the rest. This is because the integer (software) is a separation algorithm that in most cases uses C as at the same time.

If the target computer does not have a hardware splitter, the / operator usually turns into a div() call, and the remainder is discarded. The same thing happens with % operator, except that he receives a throw. So if you are something like this:

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

you call the division procedure twice (and division is rather slow if your computer does not do this in hardware). So it's faster to use div() to get both.

(Of course, this is also less readable and whether you really gain performance depends on your specific platform and compiler. You should only switch to div() if you determine that this is a performance bottleneck. Remember what Knut said about premature optimization) .

0
Jul 30 '12 at 17:13
source share



All Articles