std.float.Float
Valueclass pub builtin Float
A 64-bit floating point number.
Static methods
from_bits
Show source codeHide source code
fn pub static from_bits(bits: Int) -> Float {
_INKO.float_from_bits(bits)
}
fn pub static from_bits(bits: Int) -> Float
Returns a Float
by interpreting the given Int
as the bits of the float.
Example
Float.from_bits(0x4029000000000000) # => 12.5
infinity
Show source codeHide source code
fn pub static infinity -> Float {
1.0 / 0.0
}
fn pub static infinity -> Float
Returns the positive infinity value.
negative_infinity
Show source codeHide source code
fn pub static negative_infinity -> Float {
-1.0 / 0.0
}
fn pub static negative_infinity -> Float
Returns the negative infinity value.
not_a_number
Show source codeHide source code
fn pub static not_a_number -> Float {
0.0 / 0.0
}
fn pub static not_a_number -> Float
Returns a NaN.
parse
Show source codeHide source code
fn pub static parse[T: Bytes](bytes: ref T) -> Option[Float] {
match inko_string_to_float(bytes.to_pointer, bytes.size) {
case { @tag = 0, @value = v } -> Option.Some(from_bits(v as Int))
case _ -> Option.None
}
}
fn pub static parse[T: Bytes](bytes: ref T) -> Option[Float]
Parses a Bytes
into a Float
, returning a Some
if the value is valid,
and a None
otherwise.
The input is expected to be a base 10 representation of a float. This method accepts inputs such as:
1.2
-1.2
+1.2
1.2e1
and1.2E1
.5
5.
Infinity
,-Infinity
,inf
,infinity
,-inf
,-infinity
NaN
Leading and/or trailing whitespace is considered invalid.
Examples
Float.parse('10.5') # => Option.Some(10.5)
Float.parse('1.2e1') # => Option.Some(12.0)
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> Bool
Returns true
if self
and the given object are not equal to each other.
%
Show source codeHide source code
fn pub %(other: ref Float) -> Float {
_INKO.float_mod(self, other)
}
fn pub %(other: ref Float) -> Float
Gets the remainder after dividing self
by the given object.
*
Show source codeHide source code
fn pub *(other: ref Float) -> Float {
_INKO.float_mul(self, other)
}
fn pub *(other: ref Float) -> Float
Multiplies self
with the given object.
**
Show source codeHide source code
fn pub **(other: ref Int) -> Float {
_INKO.float_powi(self, other)
}
fn pub **(other: ref Int) -> Float
Raises self
to the power of the given exponent.
+
Show source codeHide source code
fn pub +(other: ref Float) -> Float {
_INKO.float_add(self, other)
}
fn pub +(other: ref Float) -> Float
Adds the given object to self
.
-
Show source codeHide source code
fn pub -(other: ref Float) -> Float {
_INKO.float_sub(self, other)
}
fn pub -(other: ref Float) -> Float
Subtracts the given object from self
.
/
Show source codeHide source code
fn pub /(other: ref Float) -> Float {
_INKO.float_div(self, other)
}
fn pub /(other: ref Float) -> Float
Divides self
by the given object.
<
Show source codeHide source code
fn pub <(other: ref Float) -> Bool {
_INKO.float_lt(self, other)
}
fn pub <(other: ref Float) -> Bool
Returns true
if self
is lower than the given argument.
<=
Show source codeHide source code
fn pub <=(other: ref Float) -> Bool {
_INKO.float_le(self, other)
}
fn pub <=(other: ref Float) -> Bool
Returns true
if self
is lower than or equal to the given argument.
==
Show source codeHide source code
fn pub ==(other: ref Float) -> Bool {
# Handle simple comparisons such as `1.2 == 1.2` and `0.0 == -0.0`.
if _INKO.float_eq(self, other) { return true }
if positive_sign? != other.positive_sign? { return false }
if not_a_number? or other.not_a_number? { return false }
let diff = to_bits.wrapping_sub(other.to_bits)
diff >= -1 and diff <= 1
}
fn pub ==(other: ref Float) -> Bool
Returns true
if self
and other
are equal to each other.
This method uses "Units in the Last Place" or ULPs to perform an approximate
comparison when two values aren't exactly identical. This means most common
floats can be compared for equality and give consistent results, but you
still shouldn't rely on it always being accurate. Or to put it
differently, if you need 100% accuracy, you should use either Int
or some
other data type.
See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for more details on how ULPs work.
>
Show source codeHide source code
fn pub >(other: ref Float) -> Bool {
_INKO.float_gt(self, other)
}
fn pub >(other: ref Float) -> Bool
Returns true
if self
is greater than the given argument.
>=
Show source codeHide source code
fn pub >=(other: ref Float) -> Bool {
_INKO.float_ge(self, other)
}
fn pub >=(other: ref Float) -> Bool
Returns true
if self
is equal to or greater than the given argument.
absolute
Show source codeHide source code
fn pub absolute -> Float {
Float.from_bits(to_bits & MAX)
}
fn pub absolute -> Float
Returns the absolute value of self
.
Examples
42.0.absolute # => 42
-42.0.absolute # => 42
ceil
Show source codeHide source code
fn pub ceil -> Float {
_INKO.float_ceil(self)
}
fn pub ceil -> Float
Returns the smallest number greater than or equal to self
.
Examples
Using a regular float:
3.5.ceil # => 4.0
Using a NAN value will produce a new NAN:
Float.not_a_number.ceil.not_a_number? # => true
clone
Show source codeHide source code
fn pub clone -> Float {
self
}
fn pub clone -> Float
Creates a clone of self
.
cmp
Show source codeHide source code
fn pub cmp(other: ref Float) -> Ordering {
let mut lhs = to_bits
let mut rhs = other.to_bits
lhs ^= lhs >> 63 >>> 1
rhs ^= rhs >> 63 >>> 1
lhs.cmp(rhs)
}
fn pub cmp(other: ref Float) -> Ordering
Return the ordering between self
and other
.
This method implements total ordering of floats as per the IEEE 754 specification. Values are ordered in the following order:
- negative quiet NaN
- negative signaling NaN
- negative infinity
- negative numbers
- negative subnormal numbers
- negative zero
- positive zero
- positive subnormal numbers
- positive numbers
- positive infinity
- positive signaling NaN
- positive quiet NaN
floor
Show source codeHide source code
fn pub floor -> Float {
_INKO.float_floor(self)
}
fn pub floor -> Float
Returns the largest number less than or equal to self
.
Examples
Using a regular float:
3.5.floor # => 3.0
Using a NAN value will produce a new NAN:
Float.not_a_number.floor.not_a_number? # => true
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
formatter.write(to_string)
}
fn pub fmt(formatter: mut Formatter)
Formats self
in a human-readable format for debugging purposes.
fractional
Show source codeHide source code
fn pub fractional -> Float {
absolute % 1.0
}
fn pub fractional -> Float
Returns the fractional part of this float.
Examples
Returning the fractional:
3.5.fract => 0.5
hash
Show source codeHide source code
fn pub hash[H: mut + Hasher](hasher: mut H) {
hasher.write(to_bits)
}
fn pub hash[H: mut + Hasher](hasher: mut H: mut)
Writes the hash for self
into the given Hasher
.
infinite?
Show source codeHide source code
fn pub infinite? -> Bool {
_INKO.float_is_inf(self)
}
fn pub infinite? -> Bool
Returns true
if self
is an infinite number.
Examples
Using a regular float:
10.5.infinite? # => false
Using an infinite number:
(10.0 / 0.0).infinite? # => true
negative_sign?
Show source codeHide source code
fn pub negative_sign? -> Bool {
to_bits & MIN != 0
}
fn pub negative_sign? -> Bool
Returns true
if self
has a negative sign, including -0.0
, NaNs with a
negative sign bit, and negative infinity.
not_a_number?
Show source codeHide source code
fn pub not_a_number? -> Bool {
_INKO.float_is_nan(self)
}
fn pub not_a_number? -> Bool
Returns true
if self
is not a number (NAN).
Examples
Using a regular float:
10.5.not_a_number? => false
Using a NAN value:
Float.not_a_number.not_a_number? # => true
opposite
Show source codeHide source code
fn pub opposite -> Float {
Float.from_bits(to_bits ^ MIN)
}
fn pub opposite -> Float
Returns a value with the opposite sign of self
.
Examples
42.0.opposite # => -42
-42.0.opposite # => 42
positive_sign?
Show source codeHide source code
fn pub positive_sign? -> Bool {
negative_sign?.false?
}
fn pub positive_sign? -> Bool
Returns true
if self
has a positive sign, including +0.0
, NaNs with a
positive sign bit, and positive infinity.
round
Show source codeHide source code
fn pub round(decimals: Int) -> Float {
if decimals <= 0 { return _INKO.float_round(self) }
if decimals > 4_294_967_295 { return self }
let pow = 10.0 ** decimals
let mul = self * pow
if mul.infinite? { self } else { _INKO.float_round(mul) / pow }
}
fn pub round(decimals: Int) -> Float
Rounds self
to the nearest number.
The decimals
argument can be used to control the number of decimals of
the returned Float
. When a negative value is used, the number is rounded
on the left hand side of the decimal point.
Examples
Using a regular float:
3.5.round # => 3.0
Rounding to a given number of decimals:
3.123.round(1) # => 3.1
Rounding a number using a negative number of decimals:
34567.89.round(-2) # => 34600.0
Using a NAN value will produce a new NAN:
Float.not_a_number.round.not_a_number? # => true
to_bits
Show source codeHide source code
fn pub to_bits -> Int {
_INKO.float_to_bits(self)
}
fn pub to_bits -> Int
Returns the bitwise representation of self
, as an Int
.
This does not cast the Float
, instead the returned Int
has all
the bits set that make up self
.
Examples
Converting a Float
to its bitwise representation:
1.0.to_bits # => 4607182418800017408
to_float
Show source codeHide source code
fn pub to_float -> Float {
clone
}
fn pub to_float -> Float
Converts self
to a Float
to_int
Show source codeHide source code
fn pub to_int -> Int {
self as Int
}
fn pub to_int -> Int
Converts self
to a Int
to_string
Show source codeHide source code
fn pub to_string -> String {
inko_float_to_string(_INKO.state, self as Float64)
}
fn pub to_string -> String
Converts self
to a String
.
Examples
Converting a positive float:
10.5.to_string # => '10.5'
Converting a negative float:
-10.5.to_string # => '-10.5'
Converting a NAN:
Float.not_a_number.to_string # => 'NaN'
Converting an infinite number:
Float.infinity.to_string # => 'Infinity'
Implemented traits
Clone
impl Clone[Float] for Float
Compare
impl Compare[Float] for Float
Equal
impl Equal[ref Float] for Float
ToFloat
impl ToFloat for Float
Format
impl Format for Float
Hash
impl Hash for Float
ToInt
impl ToInt for Float
Add
impl Add[Float, Float] for Float
Divide
impl Divide[Float, Float] for Float
Modulo
impl Modulo[Float, Float] for Float
Multiply
impl Multiply[Float, Float] for Float
Power
impl Power[Int, Float] for Float
Subtract
impl Subtract[Float, Float] for Float
ToString
impl ToString for Float