# Math

## Math

This library performs all types of calculations including min/max, absolute value and the square root of a number. Users can also constrain a number within a range, re-map a number from one range to another, and calculate the value of a number raised to a power.

### min

Description
Compares two values, returns the smaller one.
Syntax
min(x, y)
Parameters
x: The first number, any data type
y: The second number, any data type
Returns
The smaller of the two parameter values.

### max

Description
Compares two values, returns the larger one.
Syntax
max(x, y)
Parameters
x: The first number, any data type
y: The second number, any data type
Returns
The larger of the two parameter values.

### abs

Description
Computes and returns the absolute value of a number.
Syntax
abs(x)
Parameters
x: The number to be converted
Returns
If x is greater than or equal to 0, returns x; if less than 0, returns -x.

### constrain

Description
Constrains a number to be within a range.
Syntax
constrain(x, a, b)
Parameters
x: The number to constrain, all data types
a: The lower end of the range, all data types
b: The upper end of the range, all data types
Returns
If x is between a and b, returns x; if x is less than a, returns a; if x is greater than b, returns b.

### map

Description
Re-maps a number from one range to another. For example, when the value is 5 within the range of 0 to 10, it is re-mapped to 50 in the range of 0 to 100.
Syntax
map(value, fromLow, fromHigh, toLow, toHigh)
Parameters
value: The number to map
fromLow: The lower bound of the value's current range
fromHigh: The upper bound of the value's current range
toLow: The lower bound of the value's target range
toHigh: The upper bound of the value's target range
Returns
The value after mapping

### pow

Description
Calculates power (exponential).
Syntax
pow(base, exponent)
Parameters
base: Any value; (float)
exponent: The power to which the base is raised (float)
Returns
The result of the exponentiation (double)

### sqrt

Description
Calculates the square root of a number.
Syntax
sqrt(x)
Parameters
x: The number, any value
Returns
Double, the number's square root.

## Sample Program

Sample using constrain and pow.

``````
#include <Arduino.h>
void setup(){
Serial.begin(9600);
while(!Serial.available()); //key wait
Serial.read(); // dummy read
}

void loop(){
Serial.println("Caluculate the area of a circle ");
Serial.println("Input value 0-9");
while(!Serial.available());

int d = Serial.read();

d = constrain(d - 0x30, 0, 9); //convert from ASCII to numeric, and remove error value
Serial.print(d);
Serial.print(" x ");
Serial.print(d);
Serial.print(" x 3.14 = ");

Serial.println(pow(d, 2) * PI);
}
``````