Jump to navigation

Namespace: math

Numbas.math

Mathematical functions, providing stuff that the built-in Math object doesn't

Source:

Members

(static) primes

The first 1000 primes

Source:

(static) re_scientificNumber :RegExp

Regex to match numbers in scientific notation

Type:
Source:

Methods

(static) abs(n) → {Number}

Magnitude of a number - absolute value of a real; modulus of a complex number.

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) add(a, b) → {Number}

Add two numbers

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) addDigits(n, digits) → {String}

Add the given number of zero digits to a string representation of a number.

Parameters:
Name Type Description
n String

a string representation of a number

digits Number

the number of digits to add

Returns:
Type Description
String
Source:

(static) arccos(x) → {Number}

Inverse cosine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arccosh(x) → {Number}

Inverse hyperbolic cosine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arcsin(x) → {Number}

Inverse sine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arcsinh(x) → {Number}

Inverse hyperbolic sine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arctan(x) → {Number}

Inverse tangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arctanh(x) → {Number}

Inverse hyperbolic tangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) arg(n) → {Number}

Argument of a (complex) number

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) binomialCoefficients(n) → {Array.<Number>}

Calculate the Nth row of Pascal's triangle

Parameters:
Name Type Description
n Number
Returns:
Type Description
Array.<Number>
Source:

(static) ceil(x) → {Number}

Round up to the nearest integer. For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) choose(selection) → {*}

Choose one item from an array, at random

Parameters:
Name Type Description
selection Array
Throws:

"math.choose.empty selection" if selection has length 0.

Type
Numbas.Error
Returns:
Type Description
*
Source:
See:

(static) combinations(n, k) → {Number}

nCk - number of ways of picking k unordered elements from n.

Parameters:
Name Type Description
n Number
k Number
Throws:

"math.combinations.complex" if either of n or k is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) complex(re, im) → {complex}

Construct a complex number from real and imaginary parts.

Elsewhere in this documentation, {Number} will refer to either a JavaScript float or a complex object, interchangeably.

Parameters:
Name Type Description
re Number
im Number
Returns:
Type Description
complex
Source:

(static) complexToString() → {String}

String version of a complex number

Returns:
Type Description
String
Source:
See:

(static) conjugate(n) → {Number}

Complex conjugate

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) coprime(a, b) → {Boolean}

Are a and b coprime? If either of a or b is not an integer, return false. Equivalent to gcd(a,b) = 1.

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Boolean
Source:
See:

(static) cos(x) → {Number}

Cosine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) cosec(x) → {Number}

Cosecant

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) cosech(x) → {Number}

Hyperbolic cosecant

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) cosh(x) → {Number}

Hyperbolic cosine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) cot(x) → {Number}

Cotangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) coth(x) → {Number}

Hyperbolic tangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) countDP(n) → {Number}

Count the number of decimal places used in the string representation of a number.

Parameters:
Name Type Description
n Number | String
Returns:
Type Description
Number
Source:

(static) countSigFigs(n, maxopt) → {Number}

Calculate the significant figures precision of a number.

Parameters:
Name Type Attributes Description
n Number | String
max Boolean <optional>

be generous with calculating sig. figs. for whole numbers. e.g. '1000' could be written to 4 sig figs.

Returns:
Type Description
Number
Source:

(static) deal(N) → {Array.<Number>}

Get a random shuffling of the numbers [0..n-1]

Parameters:
Name Type Description
N Number
Returns:
Type Description
Array.<Number>
Source:

(static) defineRange(a, b) → {range}

Write the range of integers [a..b] as an array of the form [min,max,step], for use with Numbas.math.random. If either number is complex, only the real part is used.

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
range
Source:
See:

(static) degrees(x) → {Number}

Convert from radians to degrees

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) div(a, b) → {Number}

Divide one number by another

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) divides(a, b) → {Boolean}

Does a divide b? If either of a or b is not an integer, return false.

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Boolean
Source:

(static) eq(a, b) → {Boolean}

Is a equal to b?

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Boolean
Source:

(static) except(range, exclude) → {Array.<Number>}

Remove all the values in the list exclude from the list range

Parameters:
Name Type Description
range Array.<Number>
exclude Array.<Number>
Returns:
Type Description
Array.<Number>
Source:

(static) exp(n) → {Number}

Calculate e^n

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) factorial(n) → {Number}

Factorial, or Gamma(n+1) if n is not a positive integer.

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) factorise(n) → {Array.<Number>}

Factorise n. When n=2^(a1)3^(a2)5^(a3)*..., this returns the powers [a1,a2,a3,...]

Parameters:
Name Type Description
n Number
Returns:
Type Description
Array.<Number>
  • exponents of the prime factors of n
Source:

(static) floor(x) → {Number}

Round down to the nearest integer. For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) fract(x) → {Number}

Fractional part of a number - Take away the whole number part. For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) gamma(n) → {Number}

Lanczos approximation to the gamma function

http://en.wikipedia.org/wiki/Lanczos_approximation#Simple_implementation

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) gcd(a, b) → {Number}

Greatest common factor (GCF), or greatest common divisor (GCD), of a and b.

Parameters:
Name Type Description
a Number
b Number
Throws:

"math.gcf.complex" if either of a or b is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) geq(a, b) → {Boolean}

Is a greater than or equal to b?

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Boolean
Source:

(static) gt(a, b) → {Boolean}

Is a greater than b?

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Boolean
Source:

(static) im(n) → {Number}

Imaginary part of a number

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) inverse(l) → {Array.<Number>}

Calculate the inverse of a shuffling

Parameters:
Name Type Description
l Array.<Number>
Returns:
Type Description
Array.<Number>

l

Source:
See:

(static) isclose(a, b, rel_tolopt, abs_tolopt) → {Boolean}

Is a close to b?

Parameters:
Name Type Attributes Default Description
a Number
b Number
rel_tol Number <optional>
1e-12

relative tolerance: amount of error relative to max(abs(a),abs(b)).

abs_tol Number <optional>
0

absolute tolerance: maximum absolute difference between a and b.

Returns:
Type Description
Boolean
Source:

(static) lcm(a, b) → {Number}

Lowest common multiple (LCM) of a and b.

Parameters:
Name Type Description
a Number
b Number
Throws:

"math.gcf.complex" if either of a or b is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) leq(a, b) → {Boolean}

Is a less than or equal to b?

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Boolean
Source:

(static) listmax(numbers) → {Number}

Greatest of a list of numbers

Parameters:
Name Type Description
numbers Array
Throws:

math.order complex numbers if any element of the list is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) listmin(numbers) → {Number}

Least of a list of numbers

Parameters:
Name Type Description
numbers Array
Throws:

math.order complex numbers if any element of the list is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) log(n) → {Number}

Natural logarithm (base e)

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) log10(n) → {Number}

Base-10 logarithm

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) log_base(n, b) → {Number}

Arbitrary base logarithm

Parameters:
Name Type Description
n Number
b Number
Returns:
Type Description
Number

log(n)/log(b)

Source:

(static) lt(a, b) → {Boolean}

Is a less than b?

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Boolean
Source:

(static) max(a, b) → {Number}

Greatest of two numbers - wraps Math.max

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) min(a, b) → {Number}

Least of two numbers - wraps Math.min

Parameters:
Name Type Description
a Number
b Number
Throws:

math.order complex numbers if a or b are complex numbers.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) mod(a, b) → {Number}

a mod b. Always returns a positive number

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) mul(a, b) → {Number}

Multiply two numbers

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) negate(n) → {Number}

Negate a number.

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) negative(n) → {Boolean}

Is n negative? (Real, and less than 0)

Parameters:
Name Type Description
n Number
Returns:
Type Description
Boolean
Source:

(static) neq(a, b) → {Boolean}

Are a and b unequal?

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Boolean
Source:
See:

(static) niceComplexDecimal(n, options) → {String}

Display a Numbas.math.ComplexDecimal as a string

Parameters:
Name Type Description
n Numbas.math.ComplexDecimal
options Numbas.math.niceNumber_settings
Returns:
Type Description
String
Source:
See:

(static) niceDecimal(n, options) → {String}

Display a Decimal as a string

Parameters:
Name Type Description
n Decimal
options Numbas.math.niceNumber_settings
Returns:
Type Description
String
Source:
See:

(static) niceNumber(n, options) → {String}

Display a number nicely - rounds off to 10dp so floating point errors aren't displayed

Parameters:
Name Type Description
n Number
options Numbas.math.niceNumber_settings
Returns:
Type Description
String
Source:
See:

(static) nonnegative(n) → {Boolean}

Is n nonnegative? (Real, and greater than or equal to 0)

Parameters:
Name Type Description
n Number
Returns:
Type Description
Boolean
Source:

(static) parseScientific(str) → {Object}

Get the significand and exponent of a number written in exponential form

Parameters:
Name Type Description
str String
Returns:
Type Description
Object
Source:

(static) permutations(n, k) → {Number}

nPk - number of ways of picking k ordered elements from n.

Parameters:
Name Type Description
n Number
k Number
Throws:

"math.combinations.complex" if either of n or k is complex.

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) piDegree(n) → {Number}

If n can be written in the form a*pi^n, with a an integer, return the biggest possible n, otherwise return 0. Also returns 1 for n of the form pi/a, with a an integer.

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) positive(n) → {Boolean}

Is n positive? (Real, and greater than 0)

Parameters:
Name Type Description
n Number
Returns:
Type Description
Boolean
Source:

(static) pow(a, b) → {Number}

Exponentiate a number

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) precround(a, b) → {Number}

Round a to b decimal places. Real and imaginary parts of complex numbers are rounded independently.

Parameters:
Name Type Description
a Number
b Number
Throws:

"math.precround.complex" if b is complex

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) radians(x) → {Number}

Convert from degrees to radians

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) random(range) → {Number}

Get a random number in the specified range.

Returns a random choice from min to max at step-sized intervals

If all the values in the range are appended to the list, eg [min,max,step,v1,v2,v3,...], just pick randomly from the values.

Parameters:
Name Type Description
range range

[min,max,step]

Returns:
Type Description
Number
Source:
See:

(static) randomint(n) → {Number}

Get a random number in range [0..n-1]

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) randomrange(min, max) → {Number}

Get a random real number between min and max (inclusive)

Parameters:
Name Type Description
min Number
max Number
Returns:
Type Description
Number
Source:
See:

(static) rangeSize(range) → {Number}

Calculate the number of elements in a range

Parameters:
Name Type Description
range range
Returns:
Type Description
Number
Source:

(static) rangeSteps(range, step) → {range}

Change the step size of a range created with Numbas.math.defineRange

Parameters:
Name Type Description
range range
step Number
Returns:
Type Description
range
Source:

(static) rangeToList(range) → {Array.<Number>}

Convert a range to a list - enumerate all the elements of the range

Parameters:
Name Type Description
range range
Returns:
Type Description
Array.<Number>
Source:

(static) rationalApproximation(n, accuracyopt) → {Array.<Number>}

Get a rational approximation to a real number by the continued fractions method.

If accuracy is given, the returned answer will be within Math.exp(-accuracy) of the original number

Based on frap.c by David Eppstein - https://www.ics.uci.edu/~eppstein/numth/frap.c

Parameters:
Name Type Attributes Description
n Number
accuracy Number <optional>
Returns:
Type Description
Array.<Number>
  • [numerator,denominator]
Source:

(static) re(n) → {Number}

Real part of a number

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) root(a, b) → {Number}

Calculate the b-th root of a

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) round(x) → {Number}

Round to the nearest integer; fractional part >= 0.5 rounds up. For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) sec(x) → {Number}

Secant

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) sech(x) → {Number}

Hyperbolic secant

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) shuffle(list) → {Array}

Randomly shuffle a list. Returns a new list - the original is unmodified.

Parameters:
Name Type Description
list Array
Returns:
Type Description
Array
Source:

(static) sign(x) → {Number}

Sign of a number - +1, 0, or -1. For complex numbers, gives the sign of the real and imaginary parts separately.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) siground(a, b) → {Number}

Round a to b significant figures. Real and imaginary parts of complex numbers are rounded independently.

Parameters:
Name Type Description
a Number
b Number
Throws:

"math.precround.complex" if b is complex

Type
Numbas.Error
Returns:
Type Description
Number
Source:

(static) sin(x) → {Number}

Sine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) sinh(x) → {Number}

Hyperbolic sine

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) sqrt(n) → {Number}

Square root

Parameters:
Name Type Description
n Number
Returns:
Type Description
Number
Source:

(static) sub(a, b) → {Number}

Subtract one number from another

Parameters:
Name Type Description
a Number
b Number
Returns:
Type Description
Number
Source:

(static) sum(list) → {Number}

Sum the elements in the given list

Parameters:
Name Type Description
list Array.<Number>
Returns:
Type Description
Number
Source:

(static) tan(x) → {Number}

Tangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) tanh(x) → {Number}

Hyperbolic tangent

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:

(static) toGivenPrecision(n, precisionType, precision, strictPrecision) → {Boolean}

Is n given to the desired precision?

Parameters:
Name Type Description
n Number | String
precisionType String

either 'dp' or 'sigfig'

precision Number

number of desired digits of precision

strictPrecision Boolean

must trailing zeros be used to get to the desired precision (true), or is it allowed to give fewer digits in that case (false)?

Returns:
Type Description
Boolean
Source:

(static) toNearest(x, a) → {Number}

Round to the nearest multiple of a;For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
a Number
Returns:
Type Description
Number
Source:
See:

(static) trunc(x) → {Number}

Integer part of a number - chop off the fractional part. For complex numbers, real and imaginary parts are rounded independently.

Parameters:
Name Type Description
x Number
Returns:
Type Description
Number
Source:
See:

(static) unscientific(str) → {String}

If the given string is scientific notation representing a number, return a string of the form \d+.\d+ For example, '1.23e-5' is returned as '0.0000123'

Parameters:
Name Type Description
str String
Returns:
Type Description
String
Source:

(static) withinTolerance(a, b, tolerance) → {Boolean}

Is a within +/- tolerance of b?

Parameters:
Name Type Description
a Number
b Number
tolerance Number
Returns:
Type Description
Boolean
Source:

Type Definitions

niceNumber_settings

Settings for Numbas.math.niceNumber

Properties:
Name Type Description
precisionType String

Either "dp" or "sigfig".

precision Number

number of decimal places or significant figures to show.

style String

Name of a notational style to use. See Numbas.util.numberNotationStyles.

Source: