MAX: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
; Example 1: Showcasing the [[_MIN]] and [[_MAX]] functions. | ; Example 1: Showcasing the [[_MIN]] and [[_MAX]] functions. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Text|<nowiki>'maximum of two values</nowiki>|#919191}} | |||
{{Cl|PRINT}} {{Cl|_MIN}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MIN}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}) | ||
{{Cl|PRINT}} {{Cl|_MIN}}({{Text|5.0001|#F580B1}}, {{Text|5.0|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MIN}}({{Text|5.0001|#F580B1}}, {{Text|5.0|#F580B1}}) | ||
{{Cl|PRINT}} {{Cl|_MIN}}({{Text|-34|#F580B1}}, {{Text|-1.0E|#F580B1}}+{{Text|1|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MIN}}({{Text|-34|#F580B1}}, {{Text|-1.0E|#F580B1}}+{{Text|1|#F580B1}}) | ||
{{Cl|PRINT}} | {{Cl|PRINT}} | ||
{{Text|<nowiki>'minimum of two values</nowiki>|#919191}} | |||
{{Cl|PRINT}} {{Cl|_MAX}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MAX}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}) | ||
{{Cl|PRINT}} {{Cl|_MAX}}({{Text|5.0001|#F580B1}}, {{Text|5.0|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MAX}}({{Text|5.0001|#F580B1}}, {{Text|5.0|#F580B1}}) | ||
{{Cl|PRINT}} {{Cl|_MAX}}({{Text|-34|#F580B1}}, {{Text|-1.0E|#F580B1}}+{{Text|1|#F580B1}}) | {{Cl|PRINT}} {{Cl|_MAX}}({{Text|-34|#F580B1}}, {{Text|-1.0E|#F580B1}}+{{Text|1|#F580B1}}) | ||
{{Cl|PRINT}} | |||
{{Text|<nowiki>'min/max of multiple values</nowiki>|#919191}} | |||
{{Cl|PRINT}} {{Cl|_MIN}}({{Cl|_MIN}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}), {{Text|255|#F580B1}}) | |||
{{Cl|PRINT}} {{Cl|_MAX}}({{Cl|_MAX}}({{Cl|_MAX}}({{Text|345|#F580B1}}, {{Text|123|#F580B1}}), {{Text|413|#F580B1}}), {{Text|255|#F580B1}}) | |||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 50: | Line 56: | ||
{{Ot| 5.0001}} | {{Ot| 5.0001}} | ||
{{Ot|-10}} | {{Ot|-10}} | ||
{{Ot| 123}} | |||
{{Ot| 413}} | |||
{{OutputEnd}} | {{OutputEnd}} | ||
---- | |||
; Example 2: Find the minimum and maximum values in a numeric array. | |||
{{CodeStart}} | |||
{{Text|<nowiki>'min/max in array</nowiki>|#919191}} | |||
{{Cl|DIM}} a({{Text|5|#F580B1}}): {{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}} {{Text|<nowiki>'some random values</nowiki>|#919191}} | |||
a({{Text|0|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}): a({{Text|1|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}): a({{Text|2|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}) | |||
a({{Text|3|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}): a({{Text|4|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}): a({{Text|5|#F580B1}}) = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}}) | |||
{{Cl|PRINT}} {{Text|<nowiki>"array values:"</nowiki>|#FFB100}} | |||
{{Cl|FOR}} i = {{Text|0|#F580B1}} {{Cl|TO}} {{Text|5|#F580B1}}: {{Cl|PRINT}} a(i);: {{Cl|NEXT}} i: {{Cl|PRINT}}: {{Cl|PRINT}} {{Text|<nowiki>'print array</nowiki>|#919191}} | |||
minimum = {{Cl|_MIN}}(a({{Text|0|#F580B1}}), a({{Text|1|#F580B1}})) {{Text|<nowiki>'initial min/max</nowiki>|#919191}} | |||
maximum = {{Cl|_MAX}}(a({{Text|0|#F580B1}}), a({{Text|1|#F580B1}})) | |||
{{Cl|FOR}} i = {{Text|2|#F580B1}} {{Cl|TO}} {{Text|5|#F580B1}} | |||
minimum = {{Cl|_MIN}}(minimum, a(i)) {{Text|<nowiki>'check remaining indexes</nowiki>|#919191}} | |||
maximum = {{Cl|_MAX}}(maximum, a(i)) | |||
{{Cl|NEXT}} i | |||
{{Cl|PRINT}} {{Text|<nowiki>"array minimum ="</nowiki>|#FFB100}}; minimum | |||
{{Cl|PRINT}} {{Text|<nowiki>"array maximum ="</nowiki>|#FFB100}}; maximum | |||
{{CodeEnd}} | |||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [https://qb64phoenix.com/forum/showthread.php?tid=3383 Featured in our "Keyword of the Day" series] | |||
* [[_MIN]], [[_CLAMP]] | * [[_MIN]], [[_CLAMP]] | ||
* [[FIX]], [[INT]], [[CINT]], [[CLNG]] | * [[FIX]], [[INT]], [[CINT]], [[CLNG]] |
Latest revision as of 13:15, 16 January 2025
The _MAX function returns the greater of two given numeric values.
Syntax
- maximum## = _MAX(value1, value2)
Parameters
- value1 and value2 are the two numbers to compare, any integer or floating point type is supported.
- maximum## is the greater of both values returned as _FLOAT type (suffix ##).
Description
- The function compares the given numeric values and then returns the greater of both numbers.
- Will return value1 if the values are equivalent.
Availability
Examples
'maximum of two values PRINT _MIN(345, 123) PRINT _MIN(5.0001, 5.0) PRINT _MIN(-34, -1.0E+1) PRINT 'minimum of two values PRINT _MAX(345, 123) PRINT _MAX(5.0001, 5.0) PRINT _MAX(-34, -1.0E+1) PRINT 'min/max of multiple values PRINT _MIN(_MIN(345, 123), 255) PRINT _MAX(_MAX(_MAX(345, 123), 413), 255) |
123 5 -34 345 5.0001 -10 123 413 |
- Example 2
- Find the minimum and maximum values in a numeric array.
'min/max in array DIM a(5): RANDOMIZE TIMER 'some random values a(0) = INT(RND * 10): a(1) = INT(RND * 10): a(2) = INT(RND * 10) a(3) = INT(RND * 10): a(4) = INT(RND * 10): a(5) = INT(RND * 10) PRINT "array values:" FOR i = 0 TO 5: PRINT a(i);: NEXT i: PRINT: PRINT 'print array minimum = _MIN(a(0), a(1)) 'initial min/max maximum = _MAX(a(0), a(1)) FOR i = 2 TO 5 minimum = _MIN(minimum, a(i)) 'check remaining indexes maximum = _MAX(maximum, a(i)) NEXT i PRINT "array minimum ="; minimum PRINT "array maximum ="; maximum |
See also