Post

Float Data Type

The Float data type represents 64-bit IEEE 754-2008 binary floating point numbers. Floating point numbers are used to represent fractional values. They are suitable for scientific and engineering calculations.

Floating point values do not provide exact precision. Hence, they are not suitable for financial and monetary calculations. For such calculations, you can use the Decimal type.

✍ Syntax

float

✍ Syntax

The floating point type is written as float. You can use Floating Point Literal to represent a float value.

Ballerina’s float type provides several utility functions and constants through the ballerina/lang.float lang library module, such as float:NaN, float:Infinity, isNaN(), and isFinite(), to handle specific floating-point scenarios like infinity and “not a number” (NaN) values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import ballerina/io;

public function main() {

    float positiveFloat = 10.5;
    float negativeFloat = -5.25;

    float sum = positiveFloat + negativeFloat;

    float infinity = float:Infinity;
    float notANumber = float:NaN;

    io:println("Positive float:", positiveFloat);
    io:println("Negative float:", negativeFloat);
    io:println("Sum of floats:", sum);
    io:println("Infinity:", infinity);
    io:println("Not a number:", notANumber);
}
Positive float:10.5
Negative float:-5.25
Sum of floats:5.25
Infinity:Infinity
Not a number:NaN
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float positiveFloat = 10.5;
    float negativeFloat = -5.25;

    float infinity = float:Infinity;
    float notANumber = float:NaN;

}
// Highlights Only. Change View  to see full code.
float positiveFloat = 10.5;
float negativeFloat = -5.25;
float infinity = float:Infinity;
float notANumber = float:NaN;

Float Literal

Properties and Operations

The float type supports the following operations and many more language features. I will discuss these operations in detail in later posts.

  • Unary expressions - + x, - x
  • Multiplicative expressions - x * y, x / y
  • Additive expressions - x + y, x - y
  • Relational expressions - x < y, x > y, x <= y, x >= y
  • Equality expressions - x == y, x != y
  • Type Cast expression - <int> x, <decimal> x

Floating-point operations can produce NaN, infinity, and negative zero, which may have unexpected results in some expressions. Also it can generate rounding errors due to the limited precision of floating-point values. Be cautious when using floating-point arithmetic.

Unlike some programming languages, there is no implicit conversion between Ballerina numerical types. Learn more.

float values do not have a storage identity, so they cannot be modified once created. As a result, they can be assigned to a readonly variable like other numeric types, such as int and decimal.

The float type part of the anydata and json. Also, it is an ordered type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import ballerina/io;

public function main() {

    float x = 5.5;
    float y = 10.0;

    string output = string `Unary, Arithmetic, and Relational Operations:
    +x: ${+x}
    -x: ${-x}
    x + y: ${x + y}
    x - y: ${x - y}
    x * y: ${x * y}
    x / y: ${x / y}
    x % y: ${x % y}
    x < y: ${x < y}
    x <= y: ${x <= y}
    x > y: ${x > y}
    x >= y: ${x >= y}
    x == y: ${x == y}
    x != y: ${x != y}
    `;
    io:println(output);

    output = string `Type Cast Expressions:
    int(a): ${<int>x}
    decimal(a): ${<decimal>x}
    `;
    io:println(output);
}
Unary, Arithmetic, and Relational Operations:
    +x: 5.5
    -x: -5.5
    x + y: 15.5
    x - y: -4.5
    x * y: 55.0
    x / y: 0.55
    x % y: 5.5
    x < y: true
    x <= y: true
    x > y: false
    x >= y: false
    x == y: false
    x != y: true
    
Type Cast Expressions:
    int(a): 6
    decimal(a): 5.5
    
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float x = 5.5;
    float y = 10.0;

    string output = string `Unary, Arithmetic, and Relational Operations:
    +x: ${+x}
    -x: ${-x}
    x + y: ${x + y}
    x - y: ${x - y}
    x * y: ${x * y}
    x / y: ${x / y}
    x % y: ${x % y}
    x < y: ${x < y}
    x <= y: ${x <= y}
    x > y: ${x > y}
    x >= y: ${x >= y}
    x == y: ${x == y}
    x != y: ${x != y}
    `;

    output = string `Type Cast Expressions:
    int(a): ${<int>x}
    decimal(a): ${<decimal>x}
    `;
}
// Highlights Only. Change View  to see full code.
    float x = 5.5;
    float y = 10.0;

    string output = string `Unary, Arithmetic, and Relational Operations:
    +x: ${+x}
    -x: ${-x}
    x + y: ${x + y}
    x - y: ${x - y}
    x * y: ${x * y}
    x / y: ${x / y}
    x % y: ${x % y}
    x < y: ${x < y}
    x <= y: ${x <= y}
    x > y: ${x > y}
    x >= y: ${x >= y}
    x == y: ${x == y}
    x != y: ${x != y}
    `;
    output = string `Type Cast Expressions:
    int(a): ${<int>x}
    decimal(a): ${<decimal>x}
    `;

Float Operations

Language Library - ballerina/lang.float

lang.float lang library provides utility functions for float type. See available operations with examples in Float Lang Library.

Additionally, ballerina/lang.value Lang Library provides toString and another set of functions that can operate on a float value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import ballerina/io;

public function main() {

    float x = 5.5;
    float y = -10.0;

    float maxVal = float:max(x, y);
    float minVal = float:min(x, y);
    float absY = y.abs();
    float roundVal = x.round();

    string output = string `ballerina/lang.float Functions:
    x: ${x}
    y: ${y}
    max(x, y): ${maxVal}
    min(x, y): ${minVal}
    abs(y): ${absY}
    round(x): ${roundVal}
    `;
    io:println(output);
}
ballerina/lang.float Functions:
    x: 5.5
    y: -10.0
    max(x, y): 5.5
    min(x, y): -10.0
    abs(y): 10.0
    round(x): 6.0
    
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    float x = 5.5;
    float y = -10.0;

    float maxVal = float:max(x, y);
    float minVal = float:min(x, y);
    float absY = y.abs();
    float roundVal = x.round();

}
// Highlights Only. Change View  to see full code.
float x = 5.5;
float y = -10.0;

float maxVal = float:max(x, y);
float minVal = float:min(x, y);
float absY = y.abs();
float roundVal = x.round();

Float Lang Library

Conclusion

In this post, I have discussed the float data type in Ballerina. Here are the key points:

  • Floating Point Literal, e.g. 12.34, 0xA1.2B
  • Floating-point operations can produce NaN, infinity, and negative zero, which may have unexpected results in some expressions.
  • No implicit conversion between numbers.
  • Lang Library : ballerina/lang.float.

Float Data Type

Navigation

Site Settings

Site Theme

Source Code Density