diff options
author | Marti Bolivar <mbolivar@mit.edu> | 2010-11-28 11:23:33 -0500 |
---|---|---|
committer | Marti Bolivar <mbolivar@mit.edu> | 2010-11-28 11:23:33 -0500 |
commit | d9cbd78e29d42e70bb46641dd43ee0772c8c975f (patch) | |
tree | 80a67cba4468dbcd89b3cd23ad56695b1f146c66 /source/lang/constrain.rst | |
parent | 546b34076d230b617ba86defb6b90cd934b01878 (diff) | |
download | librambutan-d9cbd78e29d42e70bb46641dd43ee0772c8c975f.tar.gz librambutan-d9cbd78e29d42e70bb46641dd43ee0772c8c975f.zip |
reorganized all the arduino/ docs into a lang/ subdirectory since
they're properly CC attributed now.
Diffstat (limited to 'source/lang/constrain.rst')
-rw-r--r-- | source/lang/constrain.rst | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/source/lang/constrain.rst b/source/lang/constrain.rst new file mode 100644 index 0000000..297a2d4 --- /dev/null +++ b/source/lang/constrain.rst @@ -0,0 +1,65 @@ +.. highlight:: cpp + +.. _lang-constrain: + +constrain(x, a, b) +================== + +Description +----------- + +(Macro) Constrains a number to be within a range. + + +Parameters +---------- + +**x**: the number to constrain + +**a**: the lower end of the range + +**b**: the upper end of the range + +Returns +------- + +**x**: if **x** is between **a** and **b** + +**a**: if **x** is less than **a** + +**b**: if **x** is greater than **b** + +Example +------- + +:: + + // limits range of sensor values to between 10 and 150: + sensVal = constrain(sensVal, 10, 150); + + +Warning +------- + +Because of the way ``constrain()`` is implemented, avoid using other +functions or causing side effects inside the parentheses, as it may +lead to incorrect results:: + + constrain(x,a++,b); // avoid this - yields incorrect results + + constrain(x,a,b); // use this instead- + a++; // keep other math outside constrain() + +Arduino Compatibility +--------------------- + +Maple's implementation of ``constrain()`` is compatible with Arduino. + +See also +-------- + +- :ref:`min() <lang-min>` +- :ref:`max() <lang-max>` + + +.. include:: cc-attribution.txt |