leapc
Loading...
Searching...
No Matches
src
quo_mod.c
Go to the documentation of this file.
1
/* SPDX-License-Identifier: MIT */
10
#include "
quo_mod.h
"
11
12
struct
quo_mod
quo_mod
(int x, int y) {
13
/*
14
* Compute modulus using C's % operator. Note that C's % operator will yield
15
* negative results when the numerator is negative.
16
*/
17
int
mod
= x % y;
18
19
/* Adjust negative modulus to be positive. Ensures that:
20
*
21
* 0 <= mod < y when y > 0
22
* y < mod <= 0 when y < 0
23
*
24
* This matches Lua's modulo operator behaviour.
25
*/
26
if
(
mod
!= 0 && (
mod
^ y) < 0) {
27
mod
+= y;
28
}
29
30
/*
31
* Returns a quo_mod structure by casting an initialiser. Is this portable?
32
*/
33
return
(
struct
quo_mod
){.
quo
= (x -
mod
) / y, .
mod
=
mod
};
34
}
quo_mod.h
Quotient and modulus prototypes.
quo_mod
Quotient and remainder in integer space.
Definition
quo_mod.h:22
quo_mod::mod
int mod
Integer modulus.
Definition
quo_mod.h:30
quo_mod::quo
int quo
Integer quotient.
Definition
quo_mod.h:26
Generated by
1.9.8