Float
Types
ballerina/lang.float
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.
| |
bal run float_intro.balPositive float:10.5
Negative float:-5.25
Sum of floats:5.25
Infinity:Infinity
Not a number:NaN
bal versionBallerina 2201.6.0 (Swan Lake Update 6)
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.
| |
bal run float_operation.balUnary, 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 versionBallerina 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.
| |
bal run float_lib.balballerina/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 versionBallerina 2201.6.0 (Swan Lake Update 6)
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.