Math module¶
This module contains the required functions to perform common numeric operations such as exponential, logarithm, square root, trigonometric or hyperbolic functions. These functions only accept numbers as input and return numbers. They are only available in the modeling language, and they can’t be used with solver expressions. You must use the dedicated operators for that.
Note
Invalid operations like sqrt(-1)
or cos(+inf)
never throw
exceptions. When such cases appear, the functions of this module return nan
.
Constants¶
This module defines the following constants:
-
math.
pi
¶ The mathematical constant π = 3.141592653589793.
-
math.
e
¶ The mathematical constant e = 2.718281828459045
Common functions¶
-
math.
abs
(val)¶ Returns the absolute value of the argument.
-
math.
ceil
(val)¶ Returns the ceiling of x, i.e the smallest integer value greater than or equal to the given value. The result is casted to an integer. If the given value is already an integer, the argument is simply returned.
If the final result is out of the range of integers (e.g
math.ceil(+inf)
), an exception is thrown.
-
math.
floor
(val)¶ Returns the floor of x, i.e the largest integer value less than or equal to the given value. The result is casted to an integer. If the given value is already an integer, the argument is simply returned.
If the final result is out of the range of integers (e.g
math.floor(+inf)
), an exception is thrown.
-
math.
round
(val)¶ Returns the closest integer to the given value. The result is casted to an integer. If the given value is already an integer, the argument is simply returned.
If the final result is out of the range of integers (e.g
math.round(+inf)
), an exception is thrown.
-
math.
isInf
(val)¶ Returns true if the given value is positive or negative infinity (
+inf
or-inf
). Please note that the given value must be a float or an integer. This function will throw an exception otherwise.
-
math.
isNan
(val)¶ Returns true if the given value is a
nan
(Not a Number). Please note that the given value must be a float or an integer. This function will throw an exception otherwise.
Power and logarithm functions¶
-
math.
exp
(val)¶ Returns Euler’s number
e
raised to the power of the given value. Special cases:- If the value is
nan
, the result isnan
. - If the value is
+inf
, then the result is+inf
. - If the value is
-inf
, then the result is positive zero. - If the final result overflow, then
+inf
is returned.
- If the value is
-
math.
pow
(base, exponent)¶
-
math.
log
(val)¶ Returns the logarithm (base e) of the given value. Special cases:
- If the value is
nan
or less than zero, then the result isnan
. - If the value is
+inf
, then the result is+inf
. - If the value is zero (positive or negative), then the result is
-inf
.
- If the value is
-
math.
log10
(val)¶ Returns the logarithm (base 10) of the given value. Special cases:
- If the value is
nan
or less than zero, then the result isnan
. - If the value is
+inf
, then the result is+inf
. - If the value is zero (positive or negative), then the result is
-inf
.
- If the value is
-
math.
log2
(val)¶ Returns the logarithm (base 2) of the given value. Special cases:
- If the value is
nan
or less than zero, then the result isnan
. - If the value is
+inf
, then the result is+inf
. - If the value is zero (positive or negative), then the result is
-inf
.
- If the value is
-
math.
sqrt
(val)¶ Returns the square root of a the given value. Special cases:
- If the value is
nan
or less than zero, the result isnan
. - If the value is
+inf
, the result is+inf
.
- If the value is
Trigonometric functions¶
-
math.
cos
(val)¶ Returns the trigonometric cosine of an angle. If the argument is
nan
,+inf
or-inf
, then the result isnan
.
-
math.
sin
(val)¶ Returns the trigonometric sine of an angle. Special cases:
- If the argument is
nan
,+inf
or-inf
, then the result isnan
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is
-
math.
tan
(val)¶ Returns the trigonometric tangent of an angle. If the argument is
nan
,+inf
or-inf
, then the result isnan
.
-
math.
acos
(val)¶ Returns the arc cosine of the given value. The result is in the range
0.0
throughπ
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is greater than
1.0
or less than1.0
, then the result isnan
.
- If the argument is
-
math.
asin
(val)¶ Returns the arc sine of the given value. The result is in the range
0.0
throughπ
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is greater than
1.0
or less than1.0
, then the result isnan
.
- If the argument is
-
math.
atan
(val)¶ Returns the arc tangent of the given value. The result is in the range
-π/2
throughπ/2
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is
Hyperbolic functions¶
-
math.
cosh
(val)¶ Returns the hyperbolic cosine of the given value. This function is equivalent to
(e^x + e^-x)/2
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is
+inf
, then the result is+inf
. - If the argument is
-inf
, then the result is+inf
. - If the argument is zero, then the result is
1.0
.
- If the argument is
-
math.
sinh
(val)¶ Returns the hyperbolic sine of the given value. This function is equivalent to
(e^x - e^-x)/2
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is
+inf
, then the result is+inf
. - If the argument is
-inf
, then the result is-inf
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is
-
math.
tanh
(val)¶ Returns the hyperbolic tangent of the given value. This function is equivalent to
sinh(x)/cosh(x)
. Special cases:- If the argument is
nan
, then the result isnan
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is
+inf
, then the result is+1.0
. - If the argument is
-inf
, then the result is-1.0
.
- If the argument is
-
math.
acosh
(val)¶ Returns the inverse hyperbolic cosine of the given value. Special cases:
- If the argument is
nan
, then the result isnan
. - If the argument is less than 1.0, then the result is
nan
.
- If the argument is
-
math.
asinh
(val)¶ Returns the inverse hyperbolic sine of the given value. Special cases:
- If the argument is
nan
, then the result isnan
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is
-
math.
atanh
(val)¶ Returns the inverse hyperbolic tangent of the given value. Special cases:
- If the argument is
nan
, then the result isnan
. - If the argument is positive zero, then the result is positive zero.
- If the argument is negative zero, then the result is negative zero.
- If the argument is