Skip to content

Commit 4c6ebb8

Browse files
authored
Merge pull request #517 "Document compound remainder" from per1234
Document compound remainder
2 parents 24d0e45 + 11593ec commit 4c6ebb8

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: "%="
3+
title_expanded: compound remainder
4+
categories: [ "Structure" ]
5+
subCategories: [ "Compound Operators" ]
6+
---
7+
8+
9+
10+
11+
12+
= %= Compound Remainder
13+
14+
15+
// OVERVIEW SECTION STARTS
16+
[#overview]
17+
--
18+
19+
[float]
20+
=== Description
21+
This is a convenient shorthand to calculate the remainder when one integer is divided by another and assign it back to the variable the calculation was done on.
22+
[%hardbreaks]
23+
24+
25+
[float]
26+
=== Syntax
27+
[source,arduino]
28+
----
29+
x %= divisor; // equivalent to the expression x = x % divisor;
30+
----
31+
32+
[float]
33+
=== Parameters
34+
`x`: variable. *Allowed data types:* int +
35+
`divisor`: *non zero* variable or constant. *Allowed data types:* int
36+
37+
--
38+
// OVERVIEW SECTION ENDS
39+
40+
41+
42+
// HOW TO USE SECTION STARTS
43+
[#howtouse]
44+
--
45+
46+
[float]
47+
=== Example Code
48+
49+
[source,arduino]
50+
----
51+
int x = 7;
52+
x %= 5; // x now contains 2
53+
----
54+
[%hardbreaks]
55+
56+
[float]
57+
=== Notes and Warnings
58+
1. The compound remainder operator does not work on floats.
59+
60+
2. If the *first* operand is negative, the result is negative (or zero).
61+
Therefore, the result of `x %= 10` will not always be between 0 and 9 if `x` can be negative.
62+
[%hardbreaks]
63+
64+
--
65+
// HOW TO USE SECTION ENDS
66+
67+
68+
69+
//SEE ALSO SECTION BEGINS
70+
[#see_also]
71+
--
72+
73+
[float]
74+
=== See also
75+
76+
[role="language"]
77+
* #LANGUAGE# link:../../arithmetic-operators/remainder[Remainder]
78+
79+
--
80+
// SEE ALSO SECTION ENDS

0 commit comments

Comments
 (0)