Module: Toybox.Math

Overview

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

Example:

Prints the circumference of a circle.

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

System.println(circumference);

Example:

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

using Toybox.System;
using Toybox.Math;

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

Example:

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:

API Level 1.0.0

Classes Under Namespace

Classes: Filter, FirFilter, IirFilter

Constant Summary

Constant Variables

Type Name Value Since Description
Type E 2.7182818284590452354

API Level 1.0.0

32-bit floating point representation of E

Type PI 3.14159265358979323846

API Level 1.0.0

32-bit floating point representation of PI

Instance Method Summary collapse

Instance Method Details

acos(x as Numeric) as Decimal

Get the arc cosine of an angle.

Parameters:

Returns:

  • Float, Double

    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:

API Level 1.0.0

asin(x as Numeric) as Decimal

Get the arc sine of an angle.

Parameters:

Returns:

  • Float, Double

    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:

API Level 1.0.0

atan(x as Numeric) as Decimal

Get the arc tangent of an angle.

Parameters:

Returns:

  • Float, Double

    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:

API Level 1.0.0

atan2(y as Numeric, x as Numeric) as Decimal

Get the arc tangent of y/x in radians.

Parameters:

Returns:

  • Float, Double

    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:

API Level 1.3.0

ceil(x as Numeric) as Numeric

Compute the ceiling of a value.

Parameters:

Returns:

  • Number, Float, Long, Double

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

Since:

API Level 1.3.0

cos(x as Numeric) as Decimal

Get the cosine of an angle.

Parameters:

Returns:

  • Float, Double

    The cosine value of x in radians

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 1.0.0

floor(x as Numeric) as Numeric

Compute the floor of a value.

Parameters:

Returns:

  • Number, Float, Long, Double

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

Since:

API Level 1.3.0

ln(x as Numeric) as Decimal

Get natural logarithm of a value

Parameters:

Returns:

  • Float, Double

    natural logarithm of x

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 2.3.0

log(x as Numeric, base as Numeric) as Decimal

Get logarithm of a value using the specified base

Parameters:

Returns:

  • Float, Double

    specified base logarithm of x

    • Float if both inputs are Number or Float

    • Double if either input is Long or Double

Since:

API Level 1.0.0

mean(data as Array<Numeric>) as Double

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

Parameters:

Returns:

  • Double

    The arithmetic mean of the values in data

Since:

API Level 3.1.0

Throws:

mode(data as Array<Object>) as Object

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

Parameters:

Returns:

  • Object

    The most frequently occurring value in data.

Since:

API Level 3.1.0

Throws:

  • (InvalidValueException)

    Thrown if there is no most frequently occurring value or the passed in value array is empty

pow(x as Numeric, y as Numeric) as Decimal

Calculate x to the power of y.

Parameters:

Returns:

  • Float, Double

    x to the power of y

    • Float if both inputs are Number or Float

    • Double if either input is Long or Double

Since:

API Level 1.0.0

rand() as Number

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

Returns:

  • Number

    Non-negative random number

Since:

API Level 1.0.0

round(x as Numeric) as Numeric

Round a value.

Parameters:

Returns:

  • Number, Float, Long, Double

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

Since:

API Level 1.3.0

sin(x as Numeric) as Decimal

Get the sine of an angle.

Parameters:

Returns:

  • Float, Double

    The sine value of x in radians

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 1.0.0

sqrt(x as Numeric) as Decimal

Calculate the square root of a value.

Parameters:

Returns:

  • Float, Double

    The square root of x, or NaN if invalid

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 1.0.0

srand(seed as Number)

Seed the random number generator.

Note:

srand() does not return any value.

Parameters:

  • seed(Number)

    The value used for seeding rand()

Since:

API Level 1.0.0

stdev(data as Array<Numeric>, xbar as Double or Null) as Double

Get the standard deviation of a sample of population data.

Parameters:

  • data(Array)

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

  • xbar(Double)

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

Returns:

  • Double

    The standard deviation of the samples

Since:

API Level 3.1.0

Throws:

tan(x as Numeric) as Decimal

Get the tangent of an angle.

Parameters:

Returns:

  • Float, Double

    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:

API Level 1.0.0

toDegrees(x as Numeric) as Decimal

Convert an angle from radians to degrees.

Parameters:

Returns:

  • Float, Double

    The angle of x in degrees

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 1.3.0

toRadians(x as Numeric) as Decimal

Convert an angle from degrees to radians.

Parameters:

Returns:

  • Float, Double

    The angle of x in radians

    • Float if input is Number or Float

    • Double if input is Long or Double

Since:

API Level 1.3.0

variance(data as Array<Numeric>, xbar as Numeric or Null) as Double

Get the sample variance of an array of data.

Returns the sample variance with Bessel's correction.

Parameters:

Returns:

  • Double

    The variance of the samples

Since:

API Level 3.1.0

Throws:


Generated Apr 22, 2021 10:10:32 AM