ballerina/math module
Module overview
This module provides functions to perform fixed-precision integer arithmetic and fixed-precision decimal arithmetic. It includes functions to get the absolute, cosine, sine, root, tangent, and more for a given value.
Sample
The sample given below uses a few functions that are in the ballerina/math
module.
Follow the steps given below to run the sample:
-
Copy the code given below to file and save it as
math.bal
.import ballerina/io; import ballerina/math; public function main(string... args) { // Get the value of Pi from the ‘ballerina/math module’. io:println("Value of Pi : " + math:PI); // Get the value of E from the ‘ballerina/math module’. io:println("Value of E : " + math:E); // Get the absolute value of the given floating point number. float absoluteFloatValue = math:absFloat(-152.2544); io:println("Absolute value of -152.2544 : " + absoluteFloatValue); // Get the absolute value of an integer. int absoluteIntValue = math:absInt(-152); io:println("Absolute value of -152 : " + absoluteIntValue); // Get the Arc cosine of a given value. float acosValue = math:acos(0.027415567780803774); io:println("Arc cosine of 0.027415567780803774 : " + acosValue); // Get the Arc Sine value of a given value. float arcSineValue = math:asin(0.027415567780803774); io:println("Arc sine of 0.027415567780803774 : " + arcSineValue); // Get the Arc Tangent value of a given value. float arcTangent = math:atan(0.027415567780803774); io:println("Arc tangent of 0.027415567780803774 : " + arcTangent); // Calculate the cubic root of a given value. float cubeRoot = math:cbrt(-27); io:println("Cube root of -27 : " + cubeRoot); }
-
Navigate to the directory where the
math.bal
file is saved via the terminal and run the file using the command given below.ballerina run math.bal
The following output is given for each function that was used in the
math.bal
file.Value of Pi : 3.141592653589793 Value of E : 2.718281828459045 Absolute value of -152.2544 : 152.2544 Absolute value of -152 : 152 Arc cosine of 0.027415567780803774 : 1.5433773235341761 Arc sine of 0.027415567780803774 : 0.02741900326072046 Arc tangent of 0.027415567780803774 : 0.0274087022410345 Cube root of 0.027415567780803774 : -3.0
Functions Summary
Return Type | Function and Description | ||
---|---|---|---|
float | IEEEremainder(float a, float b) Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. |
||
float | absFloat(float val) Returns the absolute value of a float value. |
||
int | absInt(int val) Returns the absolute value of an int value. |
||
float | acos(float val) Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. |
||
float | asin(float val) Returns the arc sine of a value. |
||
float | atan(float val) Returns the arc tangent of a value. |
||
float | atan2(float a, float b) Returns the angle theta from the conversion of rectangular coordinates (a, b) to polar coordinates (r, theta). |
||
float | cbrt(float val) Returns the cube root of a float value. |
||
float | ceil(float val) Returns the smallest (closest to negative infinity) double value that is greater than orequal to the argument and is equal to a mathematical integer. |
||
float | copySign(float a, float b) Returns the first floating-point argument with the sign of the second floating-point argument. |
||
float | cos(float val) Returns the trigonometric cosine of an angle. |
||
float | cosh(float val) Returns the hyperbolic cosine of a float value. |
||
float | exp(float val) Returns Euler's number, that is 'e' raised to the power of exponent. |
||
float | expm1(float val) Returns (e to the power of x) -1. |
||
float | floor(float val) Returns the largest (closest to positive infinity) float value that is less than or equal to the argument and is equal to a mathematical integer. |
||
int | floorDiv(int a, int b) Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. |
||
int | floorMod(int a, int b) Returns the floor modulus of the long arguments. |
||
int | getExponent(float val) Returns the unbiased exponent used in the representation of a float. |
||
float | hypot(float a, float b) Returns sqrt(a squared +b squared) without intermediate overflow or underflow. |
||
float | log(float val) Returns the natural logarithm (base e) of a float value. |
||
float | log10(float val) Returns the base 10 logarithm of a float value. |
||
float | log1p(float val) Returns the natural logarithm of the sum of the argument and 1. |
||
int | negateExact(int val) Returns the negation of the argument. |
||
float | nextAfter(float a, float b) Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
||
float | nextDown(float val) Returns the adjacent floating-point value closer to negative infinity. |
||
float | nextUp(float val) Returns the adjacent floating-point value closer to positive infinity. |
||
float | pow(float a, float b) Returns the value of the 'a' raised to the power of 'b'. |
||
float | random() Returns a random number between 0.0 and 1.0. |
||
int | randomInRange(int startRange, int endRange) Returns a random number between given start(inclusive) and end(exclusive) values. |
||
float | rint(float val) Returns the double value that is closest in value to the argument and is equal to a mathematical integer. |
||
int | round(float val) Returns the closest int to the argument, with ties rounding to positive infinity. |
||
float | scalb(float a, int b) Returns a �� (2 to the power of b) rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. |
||
float | signum(float val) Returns the signum function of the argument. |
||
float | sin(float val) Returns the trigonometric sine of an angle. |
||
float | sinh(float val) Returns the hyperbolic sine of a float value. |
||
float | sqrt(float val) Returns rounded positive square root of the given value. |
||
float | tan(float val) Returns the trigonometric tangent of an angle. |
||
float | tanh(float val) Returns the hyperbolic tangent of a double value. |
||
float | toDegrees(float val) Converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
||
float | toRadians(float val) Converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
||
float | ulp(float val) Returns the size of an ulp of the argument. |
Constants
Name | Data Type | Value | Description | |
---|---|---|---|---|
PI | float | 3.141592653589793 | The ratio of the circumference of a circle to its diameter. |
|
E | float | 2.718281828459045 | The base of the natural logarithms. |
public function IEEEremainder(float a, float b) returns (float)
Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | The dividend |
|
b | float | The divisor |
Return Type | Description | ||
---|---|---|---|
float | The remainder when a is divided by b |
public function absFloat(float val) returns (float)
Returns the absolute value of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get absolute value |
Return Type | Description | ||
---|---|---|---|
float | Absolute value |
public function absInt(int val) returns (int)
Returns the absolute value of an int value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | int | Value to get the absolute value |
Return Type | Description | ||
---|---|---|---|
int | Absolute value |
public function acos(float val) returns (float)
Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the arc cosine |
Return Type | Description | ||
---|---|---|---|
float | Arc cosine value |
public function asin(float val) returns (float)
Returns the arc sine of a value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the arc sine |
Return Type | Description | ||
---|---|---|---|
float | Arc sine value |
public function atan(float val) returns (float)
Returns the arc tangent of a value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the arc tangent |
Return Type | Description | ||
---|---|---|---|
float | Arc tangent value |
public function atan2(float a, float b) returns (float)
Returns the angle theta from the conversion of rectangular coordinates (a, b) to polar coordinates (r, theta).
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | The ordinate coordinate |
|
b | float | The abscissa coordinate |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function cbrt(float val) returns (float)
Returns the cube root of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the cube root |
Return Type | Description | ||
---|---|---|---|
float | Cube root value |
public function ceil(float val) returns (float)
Returns the smallest (closest to negative infinity) double value that is greater than orequal to the argument and is equal to a mathematical integer.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the ceil |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function copySign(float a, float b) returns (float)
Returns the first floating-point argument with the sign of the second floating-point argument.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | The parameter providing the magnitude of the result |
|
b | float | The parameter providing the sign of the result |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function cos(float val) returns (float)
Returns the trigonometric cosine of an angle.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get the trigonometric cosine |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function cosh(float val) returns (float)
Returns the hyperbolic cosine of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The number whose hyperbolic cosine is to be returned |
Return Type | Description | ||
---|---|---|---|
float | The hyperbolic cosine of given float value |
public function exp(float val) returns (float)
Returns Euler's number, that is 'e' raised to the power of exponent.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Exponent value to raise |
Return Type | Description | ||
---|---|---|---|
float | Exp value |
public function expm1(float val) returns (float)
Returns (e to the power of x) -1.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The exponent to raise e to in the computation |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function floor(float val) returns (float)
Returns the largest (closest to positive infinity) float value that is less than or equal to the argument and is equal to a mathematical integer.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A float value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function floorDiv(int a, int b) returns (int)
Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | int | The dividend |
|
b | int | The divisor |
Return Type | Description | ||
---|---|---|---|
int | The result |
public function floorMod(int a, int b) returns (int)
Returns the floor modulus of the long arguments.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | int | The dividend |
|
b | int | The divisor |
Return Type | Description | ||
---|---|---|---|
int | The result |
public function getExponent(float val) returns (int)
Returns the unbiased exponent used in the representation of a float.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Float value |
Return Type | Description | ||
---|---|---|---|
int | The unbiased exponent of the argument |
public function hypot(float a, float b) returns (float)
Returns sqrt(a squared +b squared) without intermediate overflow or underflow.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | Float value |
|
b | float | Float value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function log(float val) returns (float)
Returns the natural logarithm (base e) of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A float value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function log10(float val) returns (float)
Returns the base 10 logarithm of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A float value |
Return Type | Description | ||
---|---|---|---|
float | The base 10 logarithm of a given float value |
public function log1p(float val) returns (float)
Returns the natural logarithm of the sum of the argument and 1.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A float value |
Return Type | Description | ||
---|---|---|---|
float | The natural log of x + 1 |
public function negateExact(int val) returns (int)
Returns the negation of the argument.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | int | The value to negate |
Return Type | Description | ||
---|---|---|---|
int | The result |
public function nextAfter(float a, float b) returns (float)
Returns the floating-point number adjacent to the first argument in the direction of the second argument.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | Starting floating-point value |
|
b | float | Value indicating which of start's neighbors or start should be returned |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function nextDown(float val) returns (float)
Returns the adjacent floating-point value closer to negative infinity.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Starting floating-point value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function nextUp(float val) returns (float)
Returns the adjacent floating-point value closer to positive infinity.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Starting floating-point value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function pow(float a, float b) returns (float)
Returns the value of the 'a' raised to the power of 'b'.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | The base value |
|
b | float | The exponent value |
Return Type | Description | ||
---|---|---|---|
float | Result value |
public function random() returns (float)
Returns a random number between 0.0 and 1.0.
Return Type | Description | ||
---|---|---|---|
float | Random value |
public function randomInRange(int startRange, int endRange) returns (int)
Returns a random number between given start(inclusive) and end(exclusive) values.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
startRange | int | Range start value |
|
endRange | int | Range end value |
Return Type | Description | ||
---|---|---|---|
int | Random value |
public function rint(float val) returns (float)
Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A float value |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function round(float val) returns (int)
Returns the closest int to the argument, with ties rounding to positive infinity.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | A floating-point value to be rounded to an integer |
Return Type | Description | ||
---|---|---|---|
int | The value of the argument rounded to the nearest int value |
public function scalb(float a, int b) returns (float)
Returns a �� (2 to the power of b) rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
a | float | Number to be scaled by a power of two |
|
b | int | Power of 2 used to scale a |
Return Type | Description | ||
---|---|---|---|
float | The result |
public function signum(float val) returns (float)
Returns the signum function of the argument.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The floating-point value whose signum is to be returned |
Return Type | Description | ||
---|---|---|---|
float | The signum function of the argument |
public function sin(float val) returns (float)
Returns the trigonometric sine of an angle.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | An angle, in radians |
Return Type | Description | ||
---|---|---|---|
float | The sine of the argument |
public function sinh(float val) returns (float)
Returns the hyperbolic sine of a float value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The number whose hyperbolic sine is to be returned |
Return Type | Description | ||
---|---|---|---|
float | The hyperbolic sine of a given float |
public function sqrt(float val) returns (float)
Returns rounded positive square root of the given value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | Value to get square root |
Return Type | Description | ||
---|---|---|---|
float | Square root value |
public function tan(float val) returns (float)
Returns the trigonometric tangent of an angle.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | An angle, in radians |
Return Type | Description | ||
---|---|---|---|
float | The tangent of the argument |
public function tanh(float val) returns (float)
Returns the hyperbolic tangent of a double value.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The number whose hyperbolic tangent is to be returned |
Return Type | Description | ||
---|---|---|---|
float | The hyperbolic tangent of x |
public function toDegrees(float val) returns (float)
Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | An angle, in radians |
Return Type | Description | ||
---|---|---|---|
float | The measurement of the angle angrad in degrees |
public function toRadians(float val) returns (float)
Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | An angle, in degrees |
Return Type | Description | ||
---|---|---|---|
float | The measurement of the angle angdeg in radians |
public function ulp(float val) returns (float)
Returns the size of an ulp of the argument.
Parameter Name | Data Type | Default Value | Description |
---|---|---|---|
val | float | The floating-point value whose ulp is to be returned |
Return Type | Description | ||
---|---|---|---|
float | The size of an ulp of the argument |