10.6. Integer Arithmetic Functions

10.6.1. The abs function

Synopsis
#include <stdlib.h>

int abs (int j );

Description

The abs function computes the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

Returns

The abs function returns the absolute value.

Implementation Notes

10.6.2. The div function

Synopsis
#include <stdlib.h>

div_t div (int numer , int denom );

Description

The div function computes the quotient and remainder of the division of the numerator numer by the denominator denom. If the division is inexact, the resulting quotient is the integer of lesser magnitude that is nearest to the algebraic quotient. If the result cannot be represented, the behavior is undefined: otherwise quot * denom + rem shall equal numer.

Returns

The div function returns a structure of type div_t comprising both the quotient and the remainder. The structure shall contain the following members, in either order:

int quot; /* quotient */
int rem;  /* remainder */
Implementation Notes

If denom is zero, or if numer is -32768 and denom is -1, then fixed point overflow is detected. If the corresponding interrupt is enabled, then SIGFIXED_OVERFLOW is raised.

10.6.3. The labs function

Synopsis
#include <stdlib.h>

long labs ( long );

Description

The labs function computes the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

Returns

The labs function returns the absolute value.

Implementation Notes

10.6.4. The ldiv function

Synopsis
#include <stdlib.h>

ldiv_t ldiv (long numer , long denom );

Description

The ldiv function computes the quotient and remainder of the division of the numerator numer by the denominator denom. If the division is inexact, the resulting quotient is the integer of lesser magnitude that is nearest to the algebraic quotient. If the result cannot be represented, the behavior is undefined: otherwise quot * denom + rem shall equal numer.

Returns

The ldiv function returns a structure of type ldiv_t comprising both the quotient and the remainder. The structure shall contain the following members, in either order:

long int quot; /* quotient */
long int rem;  /* remainder */
Implementation Notes

If denom is zero, or if numer is -2147483648 and denom is -1, then fixed point overflow is detected. If the corresponding interrupt is enabled, then SIGFIXED_OVERFLOW is raised.