Hide minor edits - Show changes to markup
max(a, 0); // keep other math outside the function
max(a, 0); // keep other math outside the function
min(a, 100); // keep other math outside the function
max(a, 0); // keep other math outside the function
Because of the way the max() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results
max(a--, 0); // avoid this - yields incorrect results a--; // use this instead - min(a, 100); // keep other math outside the function
// (effectively ensuring that it is at least 20)
@]
// (effectively ensuring that it is at least 20)@]
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.
The larger of the two numbers.
The larger of the two parameter values.
[@sensVal = max(senVal, 20); // assigns sensVal to the bigger of sensVal or 20
[@sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20
// (effectively ensuring that it is at least 20)
// (effectively ensuring that it is at least 20)
[@sensVal = max(senVal, 20); // assigns sensVal to the bigger of sensVal or 20 (effectively ensuring that it is at least 20)
[@sensVal = max(senVal, 20); // assigns sensVal to the bigger of sensVal or 20
// (effectively ensuring that it is at least 20)
[@sensVal = max(senVal, 20); // limits sensor value to at least 20
[@sensVal = max(senVal, 20); // assigns sensVal to the bigger of sensVal or 20 (effectively ensuring that it is at least 20)
max is useful for limiting the range a variable (say reading a sensor) can move. Even though the name max would seem to suggest it should be used to limit the sensor's maximum value, its real effect is to limit the variable's lowest value. This can be slightly counterintuitive.
Programmers coming to C from the BASIC language may also expect max to affect a variable without assigning the returned result to anything.
[@sensVal = max(senVal, 20); // limits sensor value to 20 MINIMUM
[@sensVal = max(senVal, 20); // limits sensor value to at least 20
Paradoxically, even though max would seem to limit the desired variable to a maximum value, it is really used to set the minimum value to which a variable can descend.
Programmers coming to C from the BASIC language may expect max to affect a variable without assigning the returned result to anything
max is useful for limiting the range a variable (say reading a sensor) can move. Even though the name max would seem to suggest it should be used to limit the sensor's maximum value, its real effect is to limit the variable's lowest value. This can be slightly counterintuitive.
Programmers coming to C from the BASIC language may also expect max to affect a variable without assigning the returned result to anything.
x: the first number
y: the second number
x: the first number, any data type
y: the second number, any data type
The bigger of the two numbers.
The larger of the two numbers.
sensVal = max(senVal, 20); // limits sensor value to 20 MINIMUM
Paradoxically, even though max would seem to limit the desired variable to a maximum value, it is really used to set the minimum value to which a variable can descend.
Programmers coming to C from the BASIC language may expect max to affect a variable without assigning the returned result to anything