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:

  1. 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);
    }
    
  2. 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
    

Module Detail

Functions

Function Description
IEEEremainder

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

absFloat

Returns the absolute value of a float value.

absInt

Returns the absolute value of an int value.

acos

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

asin

Returns the arc sine of a value.

atan

Returns the arc tangent of a value.

atan2

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

cbrt

Returns the cube root of a float value.

ceil

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

copySign

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

cos

Returns the trigonometric cosine of an angle.

cosh

Returns the hyperbolic cosine of a float value.

exp

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

expm1

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

floor

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.

floorDiv

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

floorMod

Returns the floor modulus of the long arguments.

getExponent

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

hypot

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

log

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

log10

Returns the base 10 logarithm of a float value.

log1p

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

negateExact

Returns the negation of the argument.

nextAfter

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

nextDown

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

nextUp

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

pow

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

random

Returns a random number between 0.0 and 1.0.

randomInRange

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

rint

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

round

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

scalb

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.

signum

Returns the signum function of the argument.

sin

Returns the trigonometric sine of an angle.

sinh

Returns the hyperbolic sine of a float value.

sqrt

Returns rounded positive square root of the given value.

tan

Returns the trigonometric tangent of an angle.

tanh

Returns 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.

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