Skip to content

Commit 32afcd0

Browse files
committed
Add warnings about doing operations inside macro parameters
The way these macros are written, causes operations done within the parameters to be repeated when the macro runs. Although warnings were already added to max() and min(), they were still missing from constrain() and sq().
1 parent 0ff7cf4 commit 32afcd0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Diff for: Language/Functions/Math/constrain.adoc

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ The code limits the sensor values to between 10 to 150.
6060
sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150
6161
----
6262

63+
[float]
64+
=== Notes and Warnings
65+
Because of the way the `constrain()` function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
66+
67+
This code will yield incorrect results:
68+
[source,arduino]
69+
----
70+
int constrainedInput = constrain(Serial.parseInt(), minimumValue, maximumValue); // avoid this
71+
----
72+
73+
Use this instead:
74+
[source,arduino]
75+
----
76+
int input = Serial.parseInt(); // keep other operations outside the constrain function
77+
int constrainedInput = constrain(input, minimumValue, maximumValue);
78+
----
79+
[%hardbreaks]
80+
6381
--
6482
// HOW TO USE SECTION ENDS
6583

Diff for: Language/Functions/Math/sq.adoc

+26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ The square of the number. (double)
3838
// OVERVIEW SECTION ENDS
3939

4040

41+
// HOW TO USE SECTION STARTS
42+
[#howtouse]
43+
--
44+
45+
[float]
46+
=== Notes and Warnings
47+
Because of the way the `sq()` function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
48+
49+
This code will yield incorrect results:
50+
[source,arduino]
51+
----
52+
int inputSquared = sq(Serial.parseInt()); // avoid this
53+
----
54+
55+
Use this instead:
56+
[source,arduino]
57+
----
58+
int input = Serial.parseInt(); // keep other operations outside the sq function
59+
int inputSquared = sq(input);
60+
----
61+
[%hardbreaks]
62+
63+
--
64+
// HOW TO USE SECTION ENDS
65+
66+
4167
// SEE ALSO SECTION
4268
[#see_also]
4369
--

0 commit comments

Comments
 (0)