lang.float
ballerina/lang.float Ballerina library
Module overview
The lang.float
module corresponds to the float
basic type.
Functions
abs
Returns the IEEE absolute value of a float value.
float f = -3.21; f.abs() ⇒ 3.21
Parameters
- x float - float value to operate on
Return Type
- float - absolute value of parameter
x
acos
Returns the arccosine of a float value.
Corresponds to IEEE acos operation.
float f = 0.5; f.acos() ⇒ 1.0471975511965979
Parameters
- x float - float value to operate on
Return Type
- float - the arccosine of parameter
x
in radians
asin
Returns the arcsine of a float value.
Corresponds to IEEE asin operation.
float f = 0.5; f.asin() ⇒ 0.5235987755982989
Parameters
- x float - float value to operate on
Return Type
- float - the arcsine of parameter
x
in radians
atan
Returns the arctangent of a float value.
float f = 243.25; f.atan() ⇒ 1.5666853530369307
Corresponds to IEEE atan operation.
Parameters
- x float - float value to operate on
Return Type
- float - the arctangent of parameter
x
in radians
atan2
Performs the 2-argument arctangent operation.
Corresponds IEEE atan2(y, x) operation.
float:atan2(24.21, 12.345) ⇒ 1.0992495979622232
Return Type
- float - the angle in radians from the positive x-axis to the point
whose Cartesian coordinates are
(x, y)
avg
Returns the average of its arguments. Return NaN if there are no arguments,
float:avg(2, 2) ⇒ 2.0
Parameters
- xs float... - float values
Return Type
- float - average of all of parameter
xs
cbrt
Returns the cube root of a float value.
Corresponds to IEEE rootn(x, 3) operation.
float f = 0.125; f.cbrt() ⇒ 0.5
Parameters
- x float - float value to operate on
Return Type
- float - cube root of parameter
x
ceiling
Rounds a float up to the closest integral value.
float f = 3.51; f.ceiling() ⇒ 4.0
Parameters
- x float - float value to operate on
Return Type
- float - smallest (closest to -∞) decimal value not less than parameter
x
that is a mathematical integer
cos
Returns the cosine of a float value.
Corresponds to IEEE cos operation.
float f = 0.7; f.cos() ⇒ 0.7648421872844885
Parameters
- x float - float value, specifying an angle in radians
Return Type
- float - the cosine of parameter
x
cosh
Returns the hyperbolic cosine of a float value.
Corresponds to IEEE cosh operation.
float f = 0.52; f.cosh() ⇒ 1.1382740988345403
Parameters
- x float - float value to operate on
Return Type
- float - hyperbolic cosine of parameter
x
exp
Raises Euler's number to a power.
Corresponds to IEEE exp operation.
float f = 2.3; f.exp() ⇒ 9.974182454814718
Parameters
- x float - float value to operate on
Return Type
- float - Euler's number raised to the power parameter
x
floor
Rounds a float down to the closest integral value.
float f = 3.51; f.floor() ⇒ 3.0
Parameters
- x float - float value to operate on
Return Type
- float - largest (closest to +∞) float value not greater than parameter
x
that is a mathematical integer
fromBitsInt
Returns the float that is represented in IEEE 64-bit floating point by an int.
All bit patterns that IEEE defines to be NaNs will all be mapped to the single float NaN value.
float:fromBitsInt(4) ⇒ 2.0E-323
Parameters
- x int - int value
Return Type
- float - parameter
x
bit pattern as a float
fromHexString
Return the float value represented by a string.
parameter s
must follow the syntax of HexFloatingPointLiteral as defined by the Ballerina specification
with the following modifications
- the HexFloatingPointLiteral may have a leading
+
or-
sign NaN
is allowedInfinity
is allowed with an optional leading+
or-
sign
float:fromHexString("0x1.0a3d70a3d70a4p4") ⇒ 16.64 float:fromHexString("0x1J") ⇒ error
Parameters
- s string - hexadecimal floating point hex string representation
fromString
Returns the float value represented by a string.
parameter s
must follow the syntax of DecimalFloatingPointNumber as defined by the Ballerina specification
with the following modifications
- the DecimalFloatingPointNumber may have a leading
+
or-
sign NaN
is allowedInfinity
is allowed with an optional leading+
or-
sign- a FloatingPointTypeSuffix is not allowed
This is the inverse of function
value:toString
applied to anfloat
.
float:fromString("0.2453") ⇒ 0.2453 float:fromString("-10") ⇒ -10.0 float:fromString("123f") ⇒ error
Parameters
- s string - string representation of a float
isFinite
Tests whether a float is finite.
Exactly one of isFinite, isInfinite and IsNaN will be true for any float value
float f = 1.2; f.isFinite() ⇒ true float:Infinity.isFinite() ⇒ false
Parameters
- x float - the float to be tested
Return Type
- boolean - true if parameter
x
is finite, i.e., neither NaN nor +∞ nor -∞
isInfinite
Tests whether a float is infinite.
Exactly one of isFinite, isInfinite and IsNaN will be true for any float value
float f = 3.21; f.isInfinite() ⇒ false float:Infinity.isInfinite() ⇒ true
Parameters
- x float - the float to be tested
Return Type
- boolean - true if parameter
x
is either +∞ or -∞
isNaN
Tests whether a float is NaN.
Exactly one of isFinite, isInfinite and IsNaN will be true for any float value.
float f = 0.23; f.isNaN() ⇒ false float:NaN.isNaN() ⇒ true
Parameters
- x float - the float to be tested
Return Type
- boolean - true if parameter
x
is NaN
log
Returns the natural logarithm of a float value.
Corresponds to IEEE log operation.
float f = 234.56; f.log() ⇒ 5.4577114186982865
Parameters
- x float - float value to operate on
Return Type
- float - natural logarithm of parameter
x
log10
Returns the base 10 logarithm of a float value.
Corresponds to IEEE log10 operation.
float f = 0.1; f.log10() ⇒ -1.0
Parameters
- x float - float value to operate on
Return Type
- float - base 10 logarithm of parameter
x
max
Returns the maximum of zero or more float values.
Result is -∞ if no args NaN if any arg is NaN
float:max(1.2, 12.3, 3.4) ⇒ 12.3 float[] marks = [70.3, 80.5, 98.1, 92.3]; float:max(...marks) ⇒ 98.1 float f = 21.2; f.max(40.5, 21, 32.4) ⇒ 40.5 float:max() ⇒ -Infinity float:max(1.2, float:NaN, 3.4) ⇒ NaN
Parameters
- xs float... - float values to operate on
Return Type
- float - maximum value of all of parameter
xs
min
Returns the minimum of zero or more float values.
Result is +∞ if no args Result is NaN if any arg is NaN
float:min(5.2, 2.3, 3.4) ⇒ 2.3 float[] marks = [90.3, 80.5, 98, 92.3]; float:min(...marks) ⇒ 80.5 float f = 1.2; f.min(10.5, 21, 32.4) ⇒ 1.2 float:min() ⇒ Infinity float:min(5.2, float:NaN, 3.4) ⇒ NaN
Parameters
- xs float... - float values to operate on
Return Type
- float - minimum value of all of parameter
xs
pow
Raises one float value to the power of another float values.
Corresponds to IEEE pow(x, y) operation.
float f = 2.1; f.pow(2) ⇒ 4.41
Return Type
- float -
x
raised to the power of parametery
round
Rounds a float value to a specified number of digits.
Returns the float value that is an integral multiple of 10 raised to the power of -fractionDigits
and closest to x
.
If there are two such values, choose the one whose final digit is even
(this is the round-to-nearest rounding mode, which is the default for IEEE and for Ballerina).
A value of fractionDigits
greater than 0 thus corresponds to the number of digits after the decimal
point being fractionDigits
; a value of 0 for fractionDigits
rounds to an integer.
If x
is NaN, +0, -0, +∞ or -∞, then the result is x
.
When fractionDigits
is 0, this is
the same as Java Math.rint method, .NET Math.Round method and
IEEE roundToIntegralTiesToEven operation
Note that <int>x
is the same as <int>x.round(0)
.
float f = 3.55; f.round() ⇒ 4.0 float g = 4.55555; g.round(3) ⇒ 4.556 float h = 2.5; h.round(0) ⇒ 2.0 float i = 3.5; i.round(0) ⇒ 4.0
Parameters
- x float - float value to operate on
- fractionDigits int (default 0) - the number of digits after the decimal point
Return Type
- float - float value closest to
x
that is an integral multiple of 10 raised to the power of-fractionDigits
sin
Returns the sine of a float value.
Corresponds to IEEE sin operation.
float f = 2.3; f.sin() ⇒ 0.7457052121767203
Parameters
- x float - float value, specifying an angle in radians
Return Type
- float - the sine of parameter
x
sinh
Returns the hyperbolic sine of a float value.
Corresponds to IEEE sinh operation.
float f = 0.71; f.sinh() ⇒ 0.7711735305928927
Parameters
- x float - float value to operate on
Return Type
- float - hyperbolic sine of parameter
x
sqrt
Returns the square root of a float value.
Corresponds to IEEE squareRoot operation.
float f = 1.96; f.sqrt() ⇒ 1.4
Parameters
- x float - float value to operate on
Return Type
- float - square root of parameter
x
sum
Returns the sum of zero or more float values.
Result is NaN if any arg is NaN
float:sum(1.2, 2.3, 3.4) ⇒ 6.9 float[] scores = [11.1, 22.2, 33.3]; float:sum(...scores) ⇒ 66.6 float f = 21.2; f.sum(10.5, 21, 32.4) ⇒ 85.1 float:sum(float:NaN, 2.3, 3.4) ⇒ NaN
Parameters
- xs float... - float values to sum
Return Type
- float - sum of all of parameter
xs
, +0.0 if parameterxs
is empty
tan
Returns the tangent of a float value.
Corresponds to IEEE tan operation
float f = 0.2; f.tan() ⇒ 0.2027100355086725
Parameters
- x float - float value, specifying an angle in radians
Return Type
- float - the tangent of parameter
x
tanh
Returns the hyperbolic tangent of a float value.
Corresponds to IEEE tanh operation.
float f = 0.9; f.tanh() ⇒ 0.7162978701990245
Parameters
- x float - float value to operate on
Return Type
- float - hyperbolic tangent of parameter
x
toBitsInt
Returns IEEE 64-bit binary floating point format representation of a float value as an int.
float f = 4.16; f.toBitsInt() ⇒ 4616369762039853220
Parameters
- x float - float value
Return Type
- int - parameter
x
bit pattern as an int
toExpString
Returns a string that represents x
using scientific notation.
The returned string will be in the same format used by value:toString
,
except that it will always include an exponent and there will be exactly
one digit before the decimal point.
But if x
is NaN or infinite, the result will be the same as value:toString
.
The digit before the decimal point will be zero only if all other digits
are zero.
This will panic if fractionDigits
is less than 0.
If fractionDigits
is zero, there will be no decimal point.
Any necessary rounding will use the roundTiesToEven rounding direction.
The exponent in the result uses lower-case e
, followed by a +
or -
sign,
followed by at least two digits, and only as many more digits as are needed
to represent the result. If x
is zero, the exponent is zero. A zero exponent
is represented with a +
sign.
float f = 12.456; f.toExpString(2) ⇒ 1.25e+1 float g = 12.456; g.toExpString(()) ⇒ 1.2456e+1 float h = 12.456; h.toExpString(-2) ⇒ panic
Parameters
- x float - float value
- fractionDigits int? - number of digits following the decimal point;
()
means to use the minimum number of digits required to accurately represent the value
Return Type
- string - string representation of
x
in scientific notation
toFixedString
Returns a string that represents x
using fixed-point notation.
The returned string will be in the same format used by value:toString
,
except that it will not include an exponent.
If x
is NaN or infinite, the result will be the same as value:toString
.
This will panic if fractionDigits
is less than 0.
If fractionDigits
is zero, there will be no decimal point.
Any necessary rounding will use the roundTiesToEven rounding direction.
float f = 12.456; f.toFixedString(2) ⇒ 12.46 float g = 12.456; g.toFixedString(0) ⇒ 12 float h = 12.456; h.toFixedString(-3) ⇒ panic
Parameters
- x float - float value
- fractionDigits int? - number of digits following the decimal point;
()
means to use the minimum number of digits required to accurately represent the value
Return Type
- string - string representation of
x
in fixed-point notation
toHexString
Returns a string that represents a float value as a hexadecimal floating point number.
The returned string will comply to the grammar of HexFloatingPointLiteral in the Ballerina spec with the following modifications:
- it will have a leading
-
sign if negative - positive infinity will be represented by
Infinity
- negative infinity will be represented by
-Infinity
- NaN will be represented by
NaN
The representation includes0x
for finite numbers.
float f = -10.2453; f.toHexString() ⇒ -0x1.47d97f62b6ae8p3
Parameters
- x float - float value
Return Type
- string - hexadecimal floating point hex string representation