leapc
Loading...
Searching...
No Matches
Functions
quo_mod.c File Reference

Quotient and modulo function implementations. More...

#include "quo_mod.h"
Include dependency graph for quo_mod.c:

Go to the source code of this file.

Functions

struct quo_mod quo_mod (int x, int y)
 Compute the integer quotient and modulus.
 

Detailed Description

Quotient and modulo function implementations.

Implements the quotient and modulo function declared in the quo_mod.h header file.

Definition in file quo_mod.c.

Function Documentation

◆ quo_mod()

struct quo_mod quo_mod ( int  x,
int  y 
)

Compute the integer quotient and modulus.

Performs an integer modulo operation. Then subtracts the modulus from the numerator and applies an integer division in order to compute the quotient.

The following invariant proves true: the numerator matches the denominator multiplied by the quotient plus the modulus.

struct quo_mod qm = quo_mod(x, y);
x == (y * qm.quo + qm.mod);
Quotient and remainder in integer space.
Definition quo_mod.h:22
int mod
Integer modulus.
Definition quo_mod.h:30
int quo
Integer quotient.
Definition quo_mod.h:26
Parameters
xNumerator integer.
yDenominator integer. Must not be zero.
Returns
quo_mod structure comprising the quotient and modulus.
Remarks
Like Lua's modulo operator, this function ensures that the modulus is always non-negative when the denominator is positive, and always non-positive when the denominator is negative.
Note
The quo_mod identifier exists in structure namespace as well as function namespace.
Warning
Throws a division-by-zero error if the denominator y is zero.

Definition at line 12 of file quo_mod.c.