Hide minor edits - Show changes to markup
min(a++, 100); // avoid this - counts by two's
min(a++, 100); // avoid this - yields incorrect results
@]
Because of the way the min() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results
[@ min(a++, 100); // avoid this - counts by two's
a++; min(a, 100); // use this instead - keep other math outside the function
// ensuring that it never gets above 100.
@]
// ensuring that it never gets above 100.@]
[@sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100, ensuring that it never gets above 100.
[@sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100
// ensuring that it never gets above 100.
Perhaps counter-intuitively, max() is often used to constrain the lower end of a variable's range, while min() is used to constrain the upper end of the range.
[@sensVal = min(senVal, 100); // limits sensor's top reading to 100 maximum
[@sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100, ensuring that it never gets above 100.
min is useful for limiting the range a variable (say reading a sensor) can move. Even though the name min would seem to suggest it should be used to limit the sensor's minimum value, its real effect is to limit the variable's highest value. This can be slightly counterintuitive.
Programmers coming to C from the BASIC language may alsoexpect min to affect a variable without assigning the returned result to anything.
[@sensVal = min(senVal, 100); // limits sensor to 100 MAXIMUM
[@sensVal = min(senVal, 100); // limits sensor's top reading to 100 maximum
Paradoxically, even though min would seem to limit the desired variable to a minimum value, it is really used to set the maximum value a variable can hold.
Programmers coming to C from the BASIC language may expect min to affect a variable without assigning the returned result to anything
min is useful for limiting the range a variable (say reading a sensor) can move. Even though the name min would seem to suggest it should be used to limit the sensor's minimum value, its real effect is to limit the variable's highest value. This can be slightly counterintuitive.
Programmers coming to C from the BASIC language may alsoexpect min to affect a variable without assigning the returned result to anything.
Paradoxically, even though min would seem to limit the desired variable to a minimum value, it is really used to set the maximum value a variable can achieve.
Paradoxically, even though min would seem to limit the desired variable to a minimum value, it is really used to set the maximum value a variable can hold.
[@sensVal = min(senVal, 100); // limits sensor to 100 MAX
[@sensVal = min(senVal, 100); // limits sensor to 100 MAXIMUM
[@sensVal = min(senVal, 100); // limits sensor to 100 max
[@sensVal = min(senVal, 100); // limits sensor to 100 MAX
x: the first number
y: the second number
x: the first number, any data type
y: the second number, any data type
sensVal = min(senVal, 100); // limits sensor to 100 max
Paradoxically, even though min would seem to limit the desired variable to a minimum value, it is really used to set the maximum value a variable can achieve.
Programmers coming to C from the BASIC language may expect min to affect a variable without assigning the returned result to anything