Skip to content
gicking edited this page Feb 10, 2018 · 4 revisions

back to Command Reference / Math

Description

Calculates the maximum of two numbers.

Notes:

  • Because of the way the max() function is implemented, using other functions inside the brackets may lead to incorrect results. For example, avoid max(a++,b).

Inclusion

  • defined in misc.h
  • auto-loaded in main_general.h
  • no #define required

Syntax

z = max(x, y)

Parameters

  • input:

    • x: the first number, any data type
    • y: the second number, any data type
  • output:

    • none

Returns

  • The larger of the two parameter values.

Example Code

The below code finds the maximum and minimum random number and prints it.

#include "main_general.h"
#include "uart1.h"
#include "putchar.h"

long   minVal=INT32_MAX, maxVal=INT32_MIN;

void setup() {

  // init UART and printf()
  UART1_begin(115200);
  putcharAttach(UART1_write);
  
}

void loop() {

  // get random number and check for new min/max
  long r = random();
  minVal = min(r, minVal);
  maxVal = max(r, maxVal);

  // every 1s send new found min/max
  if ((millis() % 1000) == 0)
    printf("%ld  %ld\n", minVal, maxVal);

}

Relevant Tutorial

  • tbd

See also

Clone this wiki locally