From 32afcd0e7a032f209fb6ef3359cb5529dce02e04 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 3 Feb 2019 16:27:24 -0800 Subject: [PATCH] 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(). --- Language/Functions/Math/constrain.adoc | 18 ++++++++++++++++++ Language/Functions/Math/sq.adoc | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Language/Functions/Math/constrain.adoc b/Language/Functions/Math/constrain.adoc index 633b5b779..2c36ab331 100644 --- a/Language/Functions/Math/constrain.adoc +++ b/Language/Functions/Math/constrain.adoc @@ -60,6 +60,24 @@ The code limits the sensor values to between 10 to 150. sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150 ---- +[float] +=== Notes and Warnings +Because of the way the `constrain()` function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results. + +This code will yield incorrect results: +[source,arduino] +---- +int constrainedInput = constrain(Serial.parseInt(), minimumValue, maximumValue); // avoid this +---- + +Use this instead: +[source,arduino] +---- +int input = Serial.parseInt(); // keep other operations outside the constrain function +int constrainedInput = constrain(input, minimumValue, maximumValue); +---- +[%hardbreaks] + -- // HOW TO USE SECTION ENDS diff --git a/Language/Functions/Math/sq.adoc b/Language/Functions/Math/sq.adoc index 5cee474ae..cff891cf2 100644 --- a/Language/Functions/Math/sq.adoc +++ b/Language/Functions/Math/sq.adoc @@ -38,6 +38,32 @@ The square of the number. (double) // OVERVIEW SECTION ENDS +// HOW TO USE SECTION STARTS +[#howtouse] +-- + +[float] +=== Notes and Warnings +Because of the way the `sq()` function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results. + +This code will yield incorrect results: +[source,arduino] +---- +int inputSquared = sq(Serial.parseInt()); // avoid this +---- + +Use this instead: +[source,arduino] +---- +int input = Serial.parseInt(); // keep other operations outside the sq function +int inputSquared = sq(input); +---- +[%hardbreaks] + +-- +// HOW TO USE SECTION ENDS + + // SEE ALSO SECTION [#see_also] --