Module : math

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.

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.toString());

   // Get the value of E from the ‘ballerina/math module’.
   io:println("Value of E  : ", math:E.toString());

   // Get the absolute value of the given floating point number. 
   float absoluteFloatValue = math:absFloat(-152.2544);
   io:println("Absolute value of -152.2544 : ", absoluteFloatValue.toString());

   // Get the absolute value of an integer.
   int absoluteIntValue = math:absInt(-152);
   io:println("Absolute value of -152 : ", absoluteIntValue.toString());

   // Get the Arc cosine of a given value.
   float acosValue = math:acos(0.027415567780803774);
   io:println("Arc cosine of 0.027415567780803774 : ", acosValue.toString());

   // Get the Arc Sine value of a given value.
   float arcSineValue = math:asin(0.027415567780803774);
   io:println("Arc sine of 0.027415567780803774 : ", arcSineValue.toString());

   // Get the Arc Tangent value of a given value.
   float arcTangent = math:atan(0.027415567780803774);
   io:println("Arc tangent of 0.027415567780803774 : ", arcTangent.toString());

   // Calculate the cubic root of a given value.
   float cubeRoot = math:cbrt(-27);
   io:println("Cube root of -27 : ", cubeRoot.toString());
}

Functions

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.

remainder

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

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.

Constants

ARITHMETIC_ERROR
PI

The ratio of the circumference of a circle to its diameter.

E

The base of the natural logarithms.

Errors

Error