math
This module contains functions and constants to make trigonometric and
non-trigonometric mathematics a breeze. The module also defines a couple
of commonly used scientific and mathematical constants such as PI
.
Fields
- math.PI
- represents the ratio of the circumference of a circle to its diameter
- math.E
- represents Euler's number, the base of natural logarithms
- math.LOG_10
- represents the natural logarithm of 10
- math.LOG_10_E
- represents the base 10 logarithm of e
- math.LOG_2
- represents the natural logarithm of 2
- math.LOG_2_E
- represents the base 2 logarithm of e
- math.ROOT_2
- represents the square root of 2
- math.ROOT_3
- represents the square root of 3
- math.ROOT_HALF
- represents the square root of 1/2
- math.Infinity
- Mathematical infinity
- math.NaN
- Mathematical NaN
Functions
- math.factorial(n)
-
factorial(n: number) calculates the product of all positive numbers less than or equal to a given positive number n
Example:
%> import math %> math.factorial(60) 8.320987112741392e+81
- @returns: number
- math.sin(n)
-
Returns a numeric value between -1 and 1, which represents the sine of the angle given in radians.
Example:
%> math.sin(46) 0.9017883476488092
- @params:
- number n
- @returns: number
- @params:
- math.cos(n)
-
Returns a numeric value between -1 and 1, which represents the cosine of the angle.
Example:
%> math.cos(93) 0.3174287015197017
- @params:
- number n
- @returns: number
- @params:
- math.tan(n)
-
Returns a numeric value that represents the tangent of the angle given.
Example:
%> math.tan(11.43) -2.155225644164932
- @params:
- number n
- @returns: number
- @params:
- math.sinh(n)
-
Returns the hyperbolic sine (in radians) of number n.
Example:
%> math.sinh(1.4) 1.904301501451534
- @params:
- number n
- @returns: number
- @params:
- math.cosh(n)
-
Returns the hyperbolic cosine (in radians) of number n.
Example:
%> math.cosh(1.91) 3.450584592563374
- @params:
- number n
- @returns: number
- @params:
- math.tanh(n)
-
Returns the hyperbolic tangent (in radians) of number n.
Example:
%> math.tanh(2.19) 0.975
- @params:
- number n
- @returns: number2591705196751
- @params:
- math.asin(n)
-
Returns a numeric value between -(π/2) and π/2 radians for x between -1 and 1. If the value of x is outside this range, it returns NaN.
Example:
%> math.asin(0.123) 0.123312275191872
- @params:
- number n
- @returns: number
- @params:
- math.acos(n)
-
Returns a numeric value between 0 and π radians for x between -1 and 1. If the value of x is outside this range, it returns NaN.
Example:
%> math.acos(0.471) 1.080372275769021
- @params:
- number n
- @returns: number
- @params:
- math.atan(n)
-
Returns a numeric value between -(π/2) and π/2 radians.
Example:
%> math.atan(math.Infinity) 1.570796326794897
- @params:
- number n
- @returns: number
- @params:
- math.atan2(x, y)
-
Returns a numeric value between -π and π representing the angle theta of an (x, y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x, y).
Example:
%> math.atan2(math.Infinity, -math.Infinity) 2.356194490192345 %> math.atan2(1, 2) 0.4636476090008061 %> math.atan2(-1.5, 2.4) -0.5585993153435624
@notes:
- the arguments to this function pass the y-coordinate first and the x-coordinate second.
- @params:
- number n
- @returns: number
- math.asinh(n)
-
Returns the hyperbolic arc-sine (in radians) of number n.
Example:
%> math.asinh(3.42) 1.943507380182802
- @params:
- number n
- @returns: number
- @params:
- math.acosh(n)
-
Returns the hyperbolic arc-cosine (in radians) of number n.
Example:
%> math.acosh(1.21) 0.637237379754108
- @params:
- number n
- @returns: number
- @params:
- math.atanh(n)
-
Returns the hyperbolic arc-tangent (in radians) of number n.
Example:
%> math.atanh(0.11) 0.1104469157900971
- @params:
- number n
- @returns: number
- @params:
- math.exp(n)
-
Returns e ** x, where x is the argument, and e is Euler's number (also known as Napier's constant), the base of the natural logarithms.
Example:
%> math.exp(4) 54.59815003314424
- @params:
- number n
- @returns: number
- @params:
- math.expm1(n)
-
Returns (e ** x) - 1, where x is the argument, and e the base of the natural logarithms.
Example:
%> math.expm1(1) 1.718281828459045
- @params:
- number n
- @returns: number
- @params:
- math.ceil(n)
-
Returns number n rounded up to the next largest integer.
Example:
%> math.ceil(1.65) 2 %> math.ceil(1.01) 2
- @params:
- number n
- @returns: number
- @params:
- math.round(n)
-
Returns the value of a number rounded to the nearest integer.
Example:
%> math.round(103.51) 104 %> math.round(103.49) 103
- @params:
- number n
- @returns: number
- @params:
- math.log(n)
-
Returns the natural logarithm (base e) of a number (mathematical ln(x)).
Example:
%> math.log(45) 3.80666248977032
@notes:
- If the value of x is 0, the return value is always -inf.
- If the value of x is negative, the return value is always NaN.
- @params:
- number n
- @returns: number
- math.log2(n)
-
Returns the base 2 logarithm of the given number. If the number is negative, NaN is returned
Example:
%> math.log2(45) 5.491853096329675
- @params:
- number n
- @returns: number
- @params:
- math.log10(n)
-
Returns the base 10 logarithm of the given number. If the number is negative, NaN is returned.
Example:
%> math.log10(45) 1.653212513775344
- @params:
- number n
- @returns: number
- @params:
- math.log1p(n)
-
For very small values of x, adding 1 can reduce or eliminate precision.
The double floats used in JS give you about 15 digits of precision.
1 + 1e-15 = 1.000000000000001, but 1 + 1e-16 = 1.000000000000000 and therefore exactly 1.0 in that arithmetic, because digits past 15 are rounded off.When you calculate log(1 + x), you should get an answer very close to x, if x is small (that's why these are called 'natural' logarithms).
If you calculate log(1 + 1.1111111111e-15) you should get an answer close to 1.1111111111e-15.
Instead, you will end up taking the logarithm of 1.00000000000000111022 (the round-off is in binary so sometimes it gets ugly), so you get the answer 1.11022...e-15, with only 3 correct digits.
If, instead, you calculate log1p(1.1111111111e-15) you will get a much more accurate answer 1.1111111110999995e-15 with 15 correct digits of precision (actually 16 in this case).returns the natural logarithm (base e) of 1 + a number If the value of x is less than -1, the return value is always NaN.
Example:
%> math.log1p(45) 3.828641396489095
- @params:
- number n
- @returns: number
- @params:
- math.cbrt(n)
-
Returns the cube root of a number n.
Example:
%> math.cbrt(64) 4
- @params:
- number n
- @returns: number
- @params:
- math.sign(n)
-
Returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument. If the number passed into sign() is 0, it will return a 0.
Example:
%> math.sign(10) 1 %> math.sign(-20) -1 %> math.sign(-0) -0 %> math.sign(0) 0
- @params:
- number n
- @returns: number
- @params:
- math.floor(n)
-
A number representing the largest integer less than or equal to the specified number
Example:
%> math.floor(1.92) 1
- @params:
- number n
- @returns: number
- @params:
- math.is_nan(n)
-
is_nan(n: number)
returns true if the given number is equal to NaN or false otherwise.
- @params:
- number n
- @returns: bool
- @params:
- math.is_inf(n)
-
Returns
true
if the given number is equal to Infinity or -Infinity orfalse
otherwise.Example:
%> math.is_inf(math.Infinity) true %> math.is_inf(-math.Infinity) true %> math.is_inf(0) false
- @params:
- number n
- @returns: bool
- @params:
- math.is_finite(n)
-
Return
true
if x is neither an Infinity nor a NaN, andfalse
otherwise.Example:
%> math.is_finite(0) true %> math.is_finite(math.NaN) true %> math.is_finite(-math.Infinity) false
- @params:
- number n
- @returns: bool
- @params:
- math.trunc(n)
-
Returns the integer part of a number by removing any fractional.
Example:
%> math.trunc(1.92) 1 %> math.trunc(1.0) 1 %> math.trunc(1.01) 1 %> math.trunc(-1.01) -1
- @params:
- number n
- @returns: number
- @params:
- math.sqrt(n)
-
Returns the square root of a number.
Example:
%> math.sqrt(100) 10
- @params:
- number n
- @returns: number
- @params:
- math.sum(arg)
-
Calculates the sum of all the elements in the input iterable the default start value for the product is 1. when the iterable is empty, it returns 1
Example:
%> math.sum([1, 2, [3, 4, [5, 6]]]) 21
- @params:
- iterable arg
- @returns: number
- @params:
- math.product(arg)
-
Calculates the product of all the elements in the input iterable the default start value for the product is 1. when the iterable is empty, it returns 1
Example:
%> math.product([1, 2, [3, 4, [5, 6]]]) 720
- @params:
- iterable arg
- @returns: number
- @params:
- math.fraction(n)
-
Returns the fractional part of a number as a whole number by removing any integer
Example:
%> math.fraction(1.92) 92
- @params:
- number n
- @returns: number
- @params: