This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

# Functions - math

 absFloat Calculates the absolute value of a float value. absInt Calculates the absolute value of an int value. acos Calculates the arc cosine of a value; the returned angle is in the range 0. asin Calculates the arc sine of a value. atan Calculates the arc tangent of a value. atan2 Calculates the angle theta from the conversion of rectangular coordinates (a, b) to polar coordinates (r, theta). cbrt Calculates the cube root of a float value. ceil Calculates the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. copySign Calculates the first floating-point argument with the sign of the second floating-point argument. cos Calculates the trigonometric cosine of an angle. cosh Calculates the hyperbolic cosine of a float value. exp Calculates Euler's number, that is 'e' raised to the power of exponent. expm1 Calculates (e to the power of x) -1. floor Calculates the largest (closest to positive infinity) float value that is less than or equal to the argument and is equal to a mathematical integer. floorDiv Calculates the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. floorMod Calculates the floor modulus of the long arguments. getExponent Calculates the unbiased exponent used in the representation of a float. hypot Calculates sqrt(a squared +b squared) without intermediate overflow or underflow. log Calculates the natural logarithm (base e) of a float value. log10 Calculates the base 10 logarithm of a float value. log1p Calculates the natural logarithm of the sum of the argument and 1. negateExact Calculates the negation of the argument. nextAfter Calculates the floating-point number adjacent to the first argument in the direction of the second argument. nextDown Calculates the adjacent floating-point value closer to negative infinity. nextUp Calculates the adjacent floating-point value closer to positive infinity. pow Calculates the value of the 'a' raised to the power of 'b'. random Selects a random number between 0. randomInRange Selects a random number between the given start(inclusive) and end(exclusive) values. remainder Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. rint Calculates the double value that is closest in value to the argument and is equal to a mathematical integer. round Calculates the closest int to the argument, with ties rounding to positive infinity. scalb Calculates 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. signum Calculates the signum function of the argument. sin Calculates the trigonometric sine of an angle. sinh Calculates the hyperbolic sine of a float value. sqrt Calculates rounded positive square root of the given value. tan Calculates the trigonometric tangent of an angle. tanh Calculates the hyperbolic tangent of a double value. toDegrees Converts an angle measured in radians to an approximately equivalent angle measured in degrees. toRadians Converts an angle measured in degrees to an approximately equivalent angle measured in radians. ulp Returns the size of an ulp of the argument.

## absFloat

(float val)

returns float

Calculates the absolute value of a float value.

`````` float absoluteFloatValue = math:absFloat(-152.2544);
``````

### Parameters

• val float
• Value to get absolute value

• ### Return Type

(float)
• Calculated absolute value

## absInt

(int val)

returns int

Calculates the absolute value of an int value.

`````` int absoluteIntValue = math:absInt(-152);
``````

### Parameters

• val int
• Value to get the absolute value

• ### Return Type

(int)
• Calculated absolute value

## acos

(float val)

returns float

Calculates the arc cosine of a value; the returned angle is in the range 0.0 through pi.

`````` float acosValue = math:acos(0.027415567780803774);
``````

### Parameters

• val float
• Value to get the arc cosine

• ### Return Type

(float)
• Calculated arc cosine value

## asin

(float val)

returns float

Calculates the arc sine of a value.

`````` float arcSineValue = math:asin(0.027415567780803774);
``````

### Parameters

• val float
• Value to get the arc sine

• ### Return Type

(float)
• Calculates arc sine value

## atan

(float val)

returns float

Calculates the arc tangent of a value.

`````` float arcTangent = math:atan(0.027415567780803774);
``````

### Parameters

• val float
• Value to get the arc tangent

• ### Return Type

(float)
• Calculated arc tangent value

## atan2

(float a, float b)

returns float

Calculates the angle theta from the conversion of rectangular coordinates (a, b) to polar coordinates (r, theta).

`````` float arcTangentFromCoordinates = math:atan2(6.4, 3.2);
``````

### Parameters

• a float
• Ordinate coordinate

• b float
• Abscissa coordinate

• ### Return Type

(float)
• Calculated angle theta

## cbrt

(float val)

returns float

Calculates the cube root of a float value.

`````` float cubeRoot = math:cbrt(-27.0);
``````

### Parameters

• val float
• Value to get the cube root

• ### Return Type

(float)
• Calculated cube root value

## ceil

(float val)

returns float

Calculates the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

`````` float ceilingValue = math:ceil(6.4);
``````

### Parameters

• val float
• Value to get the ceil

• ### Return Type

(float)
• Calculated smallest double value

## copySign

(float a, float b)

returns float

Calculates the first floating-point argument with the sign of the second floating-point argument.

`````` float copySignValue = math:copySign(6.4, 2.4);
``````

### Parameters

• a float
• Parameter providing the magnitude of the result

• b float
• Parameter providing the sign of the result

• ### Return Type

(float)
• Calculated floating-point argument

## cos

(float val)

returns float

Calculates the trigonometric cosine of an angle.

`````` float cosineValue = math:cos(0.3124);
``````

### Parameters

• val float
• Value to get the trigonometric cosine

• ### Return Type

(float)
• Calculated cosine value

## cosh

(float val)

returns float

Calculates the hyperbolic cosine of a float value.

`````` float hyperbolicCosineValue = math:cosh(0.3124);
``````

### Parameters

• val float
• Number whose hyperbolic cosine is to be returned

• ### Return Type

(float)
• Calculated hyperbolic cosine of given float value

## exp

(float val)

returns float

Calculates Euler's number, that is 'e' raised to the power of exponent.

`````` float euler = math:exp(3.2);
``````

### Parameters

• val float
• Exponential value to raise

• ### Return Type

(float)
• Calculated exponential value

## expm1

(float val)

returns float

Calculates (e to the power of x) -1.

`````` float exponentValue = math:expm1(6.4);
``````

### Parameters

• val float
• Exponent to raise e to in the computation

• ### Return Type

(float)
• Calculated result

## floor

(float val)

returns float

Calculates the largest (closest to positive infinity) float value that is less than or equal to the argument and is equal to a mathematical integer.

`````` float floorValue = math:floor(6.4);
``````

### Parameters

• val float
• A float value

• ### Return Type

(float)
• Calculated float value

## floorDiv

(int a, int b)

returns int | Error

Calculates the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

`````` int|error floorDivValue = math:floorDiv(6, 4);
``````

• a int
• Dividend

• b int
• Divisor

• ### Return Type

(int | Error)
• Calculated int value or else `Error` if b is 0

## floorMod

(int a, int b)

returns int | Error

Calculates the floor modulus of the long arguments.

`````` int|error floorModulesValue = math:floorMod(6, 4);
``````

• a int
• dividend

• b int
• divisor

• ### Return Type

(int | Error)
• Calculated floor modulus or else `Error` if b is 0

## getExponent

(float val)

returns int

Calculates the unbiased exponent used in the representation of a float.

`````` int unbiasedExponentValue = math:getExponent(6.4);
``````

• val float
• Float value

• ### Return Type

(int)
• Calculated unbiased exponent of the argument

## hypot

(float a, float b)

returns float

Calculates sqrt(a squared +b squared) without intermediate overflow or underflow.

`````` float pythogarusValue = math:hypot(6.4, 3.6);
``````

• a float
• Float value

• b float
• Float value

• ### Return Type

(float)
• Calculated square root value

## log

(float val)

returns float

Calculates the natural logarithm (base e) of a float value.

`````` float logarithmValue = math:log(6.4);
``````

### Parameters

• val float
• A float value

• ### Return Type

(float)
• Calculated natural logarithm value

## log10

(float val)

returns float

Calculates the base 10 logarithm of a float value.

`````` float logarithmValueBaseTen = math:log10(6.4);
``````

### Parameters

• val float
• A float value

• ### Return Type

(float)
• Calculated base 10 logarithm of a given float value

## log1p

(float val)

returns float

Calculates the natural logarithm of the sum of the argument and 1.

`````` float naturalLogarithmValue = math:log1p(6.4);
``````

### Parameters

• val float
• A float value

• ### Return Type

(float)
• Calculated natural log of x + 1

## negateExact

(int val)

returns int | Error

Calculates the negation of the argument.

`````` int|error negationValue = math:negateExact(6);
``````

### Parameters

• val int
• value to negate

• ### Return Type

(int | Error)
• Calculated negation value or else `Error` if overflow occurred

## nextAfter

(float a, float b)

returns float

Calculates the floating-point number adjacent to the first argument in the direction of the second argument.

`````` float nextAfterValue = math:nextAfter(6.4, 3.4);
``````

### Parameters

• a float
• Starting floating-point value

• b float
• Value indicating which of start's neighbors or start should be returned

• ### Return Type

(float)
• Calculated floating-point number

## nextDown

(float val)

returns float

Calculates the adjacent floating-point value closer to negative infinity.

`````` float nextDownValue = math:nextDown(6.4);
``````

### Parameters

• val float
• Starting floating-point value

• ### Return Type

(float)
• Calculated floating-point value

## nextUp

(float val)

returns float

Calculates the adjacent floating-point value closer to positive infinity.

`````` float nextUpValue = math:nextUp(6.4);
``````

### Parameters

• val float
• Starting floating-point value

• ### Return Type

(float)
• Calculates floating-point value

## pow

(float a, float b)

returns float

Calculates the value of the 'a' raised to the power of 'b'.

`````` float aPowerB = math:pow(3.2, 2.4);
``````

### Parameters

• a float
• Base value

• b float
• Exponential value

• ### Return Type

(float)
• Calculated exponential value

## random

()

returns float

Selects a random number between 0.0 and 1.0.

`````` float randomValue = math:random();
``````
• ### Return Type

(float)
• Selected random value

## randomInRange

(int startRange, int endRange)

returns int | Error

Selects a random number between the given start(inclusive) and end(exclusive) values.

`````` int|error randomInteger = math:randomInRange(1, 100);
``````

### Parameters

• startRange int
• Range start value

• endRange int
• Range end value

• ### Return Type

(int | Error)
• Selected random value or else `Error` if start range is greater than the end range

## remainder

(float a, float b)

returns float

Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.

`````` float remainderValue = math:remainder(6.4, 3.6);
``````

• a float
• dividend

• b float
• divisor

• ### Return Type

(float)
• Computed remainder when a is divided by b

## rint

(float val)

returns float

Calculates the double value that is closest in value to the argument and is equal to a mathematical integer.

`````` float roundedValue = math:rint(6.4);
``````

### Parameters

• val float
• A float value

• ### Return Type

(float)
• Calculated double value

## round

(float val)

returns int

Calculates the closest int to the argument, with ties rounding to positive infinity.

`````` int roundedIntegerValue = math:round(6.4);
``````

### Parameters

• val float
• A floating-point value to be rounded to an integer

• ### Return Type

(int)
• Calculated value of the argument rounded to the nearest int value

## scalb

(float a, int b)

returns float

Calculates 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 scalbValue = math:scalb(6.4, 2);
``````

### Parameters

• a float
• Number to be scaled by a power of two

• b int
• Power of 2 used to scale a

• ### Return Type

(float)
• Calculated result

## signum

(float val)

returns float

Calculates the signum function of the argument.

`````` float signumValue = math:signum(6.4);
``````

### Parameters

• val float
• floating-point value whose signum is to be returned

• ### Return Type

(float)
• Calculated signum function of the argument

## sin

(float val)

returns float

Calculates the trigonometric sine of an angle.

`````` float sineValue = math:sin(0.96);
``````

• val float

• ### Return Type

(float)
• Calculated sine of the argument

## sinh

(float val)

returns float

Calculates the hyperbolic sine of a float value.

`````` float hyperbolicSineValue = math:sinh(0.96);
``````

### Parameters

• val float
• Number whose hyperbolic sine is to be returned

• ### Return Type

(float)
• Calculated hyperbolic sine of a given float

## sqrt

(float val)

returns float

Calculates rounded positive square root of the given value.

`````` float squareRoot = math:sqrt(6.4);
``````

### Parameters

• val float
• Value to get square root

• ### Return Type

(float)
• Calculated square root value

## tan

(float val)

returns float

Calculates the trigonometric tangent of an angle.

`````` float tanValue = math:tan(0.96);
``````

• val float

• ### Return Type

(float)
• Calculated tangent of the argument

## tanh

(float val)

returns float

Calculates the hyperbolic tangent of a double value.

`````` float hyperbolicTanValue = math:tanh(0.96);
``````

### Parameters

• val float
• Number whose hyperbolic tangent is to be returned

• ### Return Type

(float)
• Calculated hyperbolic tangent of x

## toDegrees

(float val)

returns float

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

`````` float angleValueInDegrees = math:toDegrees(0.96);
``````

• val float

• ### Return Type

(float)
• Measurement of the angle angrad in degrees

(float val)

returns float

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

`````` float angleValueInRadians = math:toRadians(0.96);
``````

### Parameters

• val float
• An angle, in degrees

• ### Return Type

(float)
• Measurement of the angle angdeg in radians

## ulp

(float val)

returns float

Returns the size of an ulp of the argument.

`````` float ulpValue = math:ulp(0.96);
``````

### Parameters

• val float
• Floating-point value whose ulp is to be returned

• ### Return Type

(float)
• Size of an ulp of the argument