Link Search Menu Expand Document

math

This module contains functions and constants to make trigonometric and non-trignonometric mathematics a breeze. The module also defines a couple of commonly used scientific and mathematical constants such as PI.

Properties


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: number)
calculates the product of all positive numbers less than or equal to a given positive number n
return number

Example:

%> import math
%> math.factorial(60)
8.320987112741392e+81
math.sin(n: number)
returns a numeric value between -1 and 1, which represents the sine of the angle given in radians
return number

Example:

%> math.sin(46)
0.9017883476488092
math.cos(n: number)
returns a numeric value between -1 and 1, which represents the cosine of the angle
return number

Example:

%> math.cos(93)
0.3174287015197017
math.tan(n: number)
returns a numeric value that represents the tangent of the angle given
return number

Example:

%> math.tan(11.43)
-2.155225644164932
math.sinh(n: number)
returns the hyperbolic sine (in radians) of number n
return number

Example:

%> math.sinh(1.4)
1.904301501451534
math.cosh(n: number)
returns the hyperbolic cosine (in radians) of number n
return number

Example:

%> math.cosh(1.91)
3.450584592563374
math.tanh(n: number)
returns the hyperbolic tangent (in radians) of number n
return number

Example:

%> math.tanh(2.19)
0.9752591705196751
math.returns a numeric value between -(π/2)
for x between -1 and 1. If the value of x is outside this range, it returns NaN
return number

Example:

%> math.asin(0.123)
0.123312275191872
math.acos(n: number)
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
return number

Example:

%> math.acos(0.471)
1.080372275769021
math.returns a numeric value between -(π/2)
return number

Example:

  %> math.atan(math.Infinity)
  1.570796326794897
math.atan2(n: number)
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).
  • the arguments to this function pass the y-coordinate first and the x-coordinate second
return number

Example:

%> math.atan2(math.Infinity, -math.Infinity)
2.356194490192345
%> math.atan2(1, 2)
0.4636476090008061
%> math.atan2(-1.5, 2.4)
-0.5585993153435624
math.asinh(n: number)
returns the hyperbolic arcsine (in radians) of number n
return number

Example:

%> math.asinh(3.42)
1.943507380182802
math.acosh(n: number)
returns the hyperbolic arccosine (in radians) of number n
return number

Example:

%> math.acosh(1.21)
0.637237379754108
math.atanh(n: number)
returns the hyperbolic arctangent (in radians) of number n
return number

Example:

%> math.atanh(0.11)
0.1104469157900971
math.exp(n: number)
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
return number

Example:

%> math.exp(4)
54.59815003314424
math.expm1(n: number)
returns (e ** x) - 1, where x is the argument, and e the base of the natural logarithms
return number

Example:

%> math.expm1(1)
1.718281828459045
math.ceil(n: number)
returns number n rounded up to the next largest integer
return number

Example:

%> math.ceil(1.65)
2
%> math.ceil(1.01)
2
math.round(n: number)
returns the value of a number rounded to the nearest integer
return number

Example:

%> math.round(103.51)
104
%> math.round(103.49)
103
math.log(n: number)
returns the natural logarithm (base e) of a number (mathematical ln(x))
  • 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
return number

Example:

%> math.log(45)
3.80666248977032
math.log2(n: number)
returns the base 2 logarithm of the given number. If the number is negative, NaN is returned
return number

Example:

%> math.log2(45)
5.491853096329675
math.log10(n: number)
returns the base 10 logarithm of the given number. If the number is negative, NaN is returned
return number

Example:

%> math.log10(45)
1.653212513775344
math.log1p(n: number)
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 roundoff 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.

return number

Example:

%> math.log1p(45)
3.828641396489095
math.cbrt(n: number)
returns the cube root of a number n
return number

Example:

%> math.cbrt(64)
4
math.sign(n: number)
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.
return number

Example:

%> math.sign(10)
1
%> math.sign(-20)
-1
%> math.sign(-0)
-0
%> math.sign(0)
0
math.floor(n: number)
A number representing the largest integer less than or equal to the specified number
return number

Example:

%> math.floor(1.92)
1
math.is_nan(n: number)
returns true if the given number is equal to NaN or false otherwise
return bool
math.is_inf(n: number)
returns true if the given number is equal to Infinity or -Infinity or false otherwise
return bool

Example:

%> math.is_inf(math.Infinity)
true
%> math.is_inf(-math.Infinity)
true
%> math.is_inf(0)
false
math.is_finite(n: number)
return true if x is neither an Infinity nor a NaN, and false otherwise
return bool

Example:

%> math.is_finite(0)
true
%> math.is_finite(math.NaN)
true
%> math.is_finite(-math.Infinity)
false
math.trunc(n: number)
returns the integer part of a number by removing any fractional
return number

Example:

%> math.trunc(1.92)
1
%> math.trunc(1.0)
1
%> math.trunc(1.01)
1
%> math.trunc(-1.01)
-1
math.sqrt(n: number)
returns the square root of a nunmber
return number

Example:

%> math.sqrt(100)
10
math.sum(arg: iterable)
calculate 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
return number

Example:

%> math.sum([1, 2, [3, 4, [5, 6]]])
21
math.product(arg: iterable)
calculate 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
return number

Example:

%> math.product([1, 2, [3, 4, [5, 6]]])
720
math.fraction(n: number)
returns the fractional part of a number as a whole number by removing any integer
return number

Example:

%> math.fraction(1.92)
92

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.