# Module: Toybox::Math

## Overview

The Math Module provides various math methods available for use by Apps.

Examples:

Prints the circumference of a circle.

``````using Toybox.System;
using Toybox.Math;
var r = 5;
var circumference = (2 * Math.PI * r);

System.println(circumference);``````

Prints the area of a square with Math.pow via direct call.

``````using Toybox.System;
using Toybox.Math;

System.println(Math.pow(10, 2));``````

Solves for c using the Pythagorean Theorem and multiple Math API methods.

``````using Toybox.System;
using Toybox.Math;
var a = 2;
var b = 3;
var c = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2)));

System.println(c);``````

Since:

• 1.0.0

## Defined Under Namespace

Classes: Filter, FirFilter, IirFilter

## Constant Summary

PI = 3.14159265358979323846

Since:

• 1.0.0

E = 2.7182818284590452354

Since:

• 1.0.0

## Instance Method Summary collapse

• Get the arc cosine of an angle.

• Get the arc sine of an angle.

• Get the arc tangent of an angle.

• Get the arc tangent of y/x in radians.

• Compute the ceiling of a value.

• Get the cosine of an angle.

• Compute the floor of a value.

• Get natural logarithm of a value.

• Get logarithm of a value using the specified base.

• Get the arithmetic mean (average) of an array of data.

• Get the most common value found in an array of data.

• Calculate x to the power of y.

• Returns a pseudo-random Number.

• Round a value.

• Get the sine of an angle.

• Calculate the square root of a value.

• Seed the random number generator.

• Get the standard deviation of a sample of population data.

• Get the tangent of an angle.

• Convert an angle from radians to degrees.

• Convert an angle from degrees to radians.

• Get the sample variance of an array of data.

## Instance Method Details

### acos(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the arc cosine of an angle.

Parameters:

• x

The cosine value

Returns:

• The interval [0..PI] in radians, or `NaN` if invalid

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7629 7630``` ```# File 'Monkeybrains.rb', line 7629 def acos(x) end```

### asin(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the arc sine of an angle.

Parameters:

• x

The sine value

Returns:

• The interval [-PI/2..PI/2] in radians, or `NaN` if invalid

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7640 7641``` ```# File 'Monkeybrains.rb', line 7640 def asin(x) end```

### atan(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the arc tangent of an angle.

Parameters:

• x

The tangent value

Returns:

• The interval [-PI/2..PI/2] in radians, or `NaN` if invalid

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7651 7652``` ```# File 'Monkeybrains.rb', line 7651 def atan(x) end```

### atan2(y, x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the arc tangent of y/x in radians.

Parameters:

• y

The proportion of the y coordinate

• x

The proportion of the x coordinate

Returns:

• The principal arc tangent of y/x, in the interval [-PI..PI] radians, or `NaN` if invalid

• Float if both inputs are Number or Float

• Double if either input is Long or Double

Since:

• 1.3.0

 ``` 7665 7666``` ```# File 'Monkeybrains.rb', line 7665 def atan2(y, x) end```

### ceil(x) ⇒ Toybox::Lang::Number, ...

Compute the ceiling of a value.

Parameters:

• x

A numeric value

Returns:

• The smallest integer greater than or equal to x Return type matches the input parameter type

Since:

• 1.3.0

 ``` 7675 7676``` ```# File 'Monkeybrains.rb', line 7675 def ceil(x) end```

### cos(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the cosine of an angle.

Parameters:

• x

The angle in radians

Returns:

• The cosine value of x in radians

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7686 7687``` ```# File 'Monkeybrains.rb', line 7686 def cos(x) end```

### floor(x) ⇒ Toybox::Lang::Number, ...

Compute the floor of a value.

Parameters:

• x

A numeric value

Returns:

• The largest integer less than or equal to x Return type matches the input parameter type

Since:

• 1.3.0

 ``` 7696 7697``` ```# File 'Monkeybrains.rb', line 7696 def floor(x) end```

### ln(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get natural logarithm of a value

Parameters:

• x

The value for which to get the logarithm

Returns:

• natural logarithm of x

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 2.3.0

 ``` 7720 7721``` ```# File 'Monkeybrains.rb', line 7720 def ln(x) end```

### log(x, base) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get logarithm of a value using the specified base

Parameters:

• x

The value for which to get the logarithm

• base

The base value.

Returns:

• specified base logarithm of x

• Float if both inputs are Number or Float

• Double if either input is Long or Double

Since:

• 1.0.0

 ``` 7709 7710``` ```# File 'Monkeybrains.rb', line 7709 def log(x, base) end```

### mean(data) ⇒ Toybox::Lang::Double

Get the arithmetic mean (average) of an array of data.

Parameters:

Returns:

• The arithmetic mean of the values in data

Raises:

• Thrown if the provided data array is empty.

Since:

• 3.1.0

 ``` 7732 7733``` ```# File 'Monkeybrains.rb', line 7732 def mean(data) end```

### mode(data) ⇒ Toybox::Lang::Object

Get the most common value found in an array of data.

Parameters:

Returns:

• The most frequently occurring value in data.

Raises:

• Thrown if there is no most frequently occurring value.

Since:

• 3.1.0

 ``` 7742 7743``` ```# File 'Monkeybrains.rb', line 7742 def mode(data) end```

### pow(x, y) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Calculate x to the power of y.

Parameters:

• x

Base

• y

Exponent

Returns:

• x to the power of y

• Float if both inputs are Number or Float

• Double if either input is Long or Double

Since:

• 1.0.0

 ``` 7755 7756``` ```# File 'Monkeybrains.rb', line 7755 def pow(x, y) end```

### rand ⇒ Toybox::Lang::Number

Returns a pseudo-random Number. Use the srand() function to seed the random number generator.

Returns:

• Non-negative random number

Since:

• 1.0.0

 ``` 7761 7762``` ```# File 'Monkeybrains.rb', line 7761 def rand() end```

### round(x) ⇒ Toybox::Lang::Number, ...

Round a value.

Parameters:

• x

A numeric value

Returns:

• The closest integer to x. Decimal values >= .5 will be rounded up Return type matches the input parameter type

Since:

• 1.3.0

 ``` 7771 7772``` ```# File 'Monkeybrains.rb', line 7771 def round(x) end```

### sin(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the sine of an angle.

Parameters:

• x

The angle in radians

Returns:

• The sine value of x in radians

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7782 7783``` ```# File 'Monkeybrains.rb', line 7782 def sin(x) end```

### sqrt(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Calculate the square root of a value.

Parameters:

• x

The value for which to get the square root

Returns:

• The square root of x, or `NaN` if invalid

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.0.0

 ``` 7793 7794``` ```# File 'Monkeybrains.rb', line 7793 def sqrt(x) end```

### srand(seed) ⇒ Object

Note:

srand() does not return any value.

Seed the random number generator.

Parameters:

• seed

The value used for seeding rand()

Since:

• 1.0.0

 ``` 7800 7801``` ```# File 'Monkeybrains.rb', line 7800 def srand(seed) end```

### stdev(data, xbar) ⇒ Toybox::Lang::Double

Get the standard deviation of a sample of population data.

Parameters:

• data

An array of Number, Float, Long, or Double values with at least two elements.

• xbar

The mean, if known. Otherwise, pass `null` and the mean of data will be calculated.

Returns:

• The standard deviation of the samples

Raises:

• Thrown if the provided data array has fewer than two elements.

Since:

• 3.1.0

 ``` 7814 7815``` ```# File 'Monkeybrains.rb', line 7814 def stdev(data, xbar) end```

### tan(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Get the tangent of an angle.

Parameters:

• x

The angle in radians

Returns:

• The tangent value of x in radians Toybox::Lang::Float if input is Toybox::Lang::Number or Toybox::Lang::Float Toybox::Lang::Double if input is Toybox::Lang::Long or Toybox::Lang::Double

Since:

• 1.0.0

 ``` 7825 7826``` ```# File 'Monkeybrains.rb', line 7825 def tan(x) end```

### toDegrees(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Convert an angle from radians to degrees.

Parameters:

• x

The angle in radians

Returns:

• The angle of x in degrees

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.3.0

 ``` 7836 7837``` ```# File 'Monkeybrains.rb', line 7836 def toDegrees(x) end```

### toRadians(x) ⇒ Toybox::Lang::Float, Toybox::Lang::Double

Convert an angle from degrees to radians.

Parameters:

• x

The angle in degrees

Returns:

• The angle of x in radians

• Float if input is Number or Float

• Double if input is Long or Double

Since:

• 1.3.0

 ``` 7847 7848``` ```# File 'Monkeybrains.rb', line 7847 def toRadians(x) end```

### variance(data, xbar) ⇒ Toybox::Lang::Double

Get the sample variance of an array of data.

Returns the sample variance with Bessel's correction.

Parameters:

• data

An array of Number, Float, Long, or Double values with at least two elements.

• xbar

The mean, if known. Otherwise, pass `null` and the mean of data will be calculated.

Returns:

• The variance of the samples

Raises:

• Thrown if the provided data array has fewer than two elements.

Since:

• 3.1.0

 ``` 7863 7864``` ```# File 'Monkeybrains.rb', line 7863 def variance(data, xbar) end```