import ballerina/io;
import ballerina/math;

public function main() {

    io:println("Value of PI : ", math:PI);
    io:println("Value of E  : ", math:E);

    //This returns the absolute value of a `float` value.
    float absoluteFloatValue = math:absFloat(-152.2544);
    io:println("Absolute value of -152.2544 : ", absoluteFloatValue);

    //This returns the absolute value of an `int` value.
    int absoluteIntValue = math:absInt(-152);
    io:println("Absolute value of -152 : ", absoluteIntValue);

    //This returns the arc cosine of a value.
    float acosValue = math:acos(0.027415567780803774);
    io:println("Arc cosine of 0.027415567780803774 : ", acosValue);

    //This returns the arc sine of a value.
    float arcSineValue = math:asin(0.027415567780803774);
    io:println("Arc sine of 0.027415567780803774 : ", arcSineValue);

    //This returns the arc tangent of a value.
    float arcTangent = math:atan(0.027415567780803774);
    io:println("Arc tangent of 0.027415567780803774 : ", arcTangent);

    //This returns the cube root of a `float` value.
    float cubeRoot = math:cbrt(-27.0);
    io:println("Cube root of -27.0 : ", cubeRoot);

    //There are over 40 methods in the ballerina math API that can be used to perform numeric operations.
    //You can find them in the `ballerina/math` module.
}

Math

The Ballerina Math API contains methods to perform various numerical operations.

import ballerina/io;
import ballerina/math;
public function main() {
    io:println("Value of PI : ", math:PI);
    io:println("Value of E  : ", math:E);
    float absoluteFloatValue = math:absFloat(-152.2544);
    io:println("Absolute value of -152.2544 : ", absoluteFloatValue);

This returns the absolute value of a float value.

    int absoluteIntValue = math:absInt(-152);
    io:println("Absolute value of -152 : ", absoluteIntValue);

This returns the absolute value of an int value.

    float acosValue = math:acos(0.027415567780803774);
    io:println("Arc cosine of 0.027415567780803774 : ", acosValue);

This returns the arc cosine of a value.

    float arcSineValue = math:asin(0.027415567780803774);
    io:println("Arc sine of 0.027415567780803774 : ", arcSineValue);

This returns the arc sine of a value.

    float arcTangent = math:atan(0.027415567780803774);
    io:println("Arc tangent of 0.027415567780803774 : ", arcTangent);

This returns the arc tangent of a value.

    float cubeRoot = math:cbrt(-27.0);
    io:println("Cube root of -27.0 : ", cubeRoot);

This returns the cube root of a float value.

}

There are over 40 methods in the ballerina math API that can be used to perform numeric operations. You can find them in the ballerina/math module.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run math_functions.bal
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 -27.0 : -3.0