Skip to content

Commit

Permalink
add some float utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfettes committed Jan 9, 2025
1 parent 401afd2 commit 1726540
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
89 changes: 89 additions & 0 deletions float/float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,92 @@ pub fn to_be_bytes(self : Float) -> Bytes {
pub fn to_le_bytes(self : Float) -> Bytes {
self.reinterpret_as_uint().to_le_bytes()
}

///|
/// Determines if the floating-point number is positive or negative infinity.
///
/// Parameters:
///
/// * `self` : The floating-point number to be checked.
///
/// Returns a boolean value indicating whether the number is positive or negative
/// infinity.
///
/// Example:
///
/// ```moonbit
/// test "Float::is_inf" {
/// inspect!(@float.infinity.is_inf(), content="true")
/// inspect!(@float.neg_infinity.is_inf(), content="true")
/// inspect!((1.0 : Float).is_inf(), content="false")
/// }
/// ```
pub fn Float::is_inf(self : Float) -> Bool {
self.is_pos_inf() || self.is_neg_inf()
}

///|
/// Determines if the floating-point number is positive infinity.
///
/// Parameters:
///
/// * `self` : The floating-point number to be checked.
///
/// Returns a boolean value indicating whether the number is positive infinity.
///
/// Example:
///
/// ```moonbit
/// test "Float::is_pos_inf" {
/// inspect!(@float.infinity.is_pos_inf(), content="true")
/// inspect!((1.0 : Float).is_pos_inf(), content="false")
/// inspect!(@float.neg_infinity.is_pos_inf(), content="false")
/// }
/// ```
pub fn Float::is_pos_inf(self : Float) -> Bool {
self > max_value
}

///|
/// Determines if the floating-point number is negative infinity.
///
/// Parameters:
///
/// * `self` : The floating-point number to be checked.
///
/// Returns a boolean value indicating whether the number is negative infinity.
///
/// Example:
///
/// ```moonbit
/// test "Float::is_neg_inf" {
/// inspect!(@float.neg_infinity.is_neg_inf(), content="true")
/// inspect!((1.0 : Float).is_neg_inf(), content="false")
/// inspect!(@float.infinity.is_neg_inf(), content="false")
/// }
/// ```
pub fn Float::is_neg_inf(self : Float) -> Bool {
self < min_value
}

///|
/// Determines if the floating-point number is NaN (Not a Number).
///
/// Parameters:
///
/// * `self` : The floating-point number to be checked.
///
/// Returns a boolean value indicating whether the number is NaN.
///
/// Example:
///
/// ```moonbit
/// test "Float::is_nan" {
/// inspect!(@float.not_a_number.is_nan(), content="true")
/// inspect!((1.0 : Float).is_nan(), content="false")
/// inspect!(@float.infinity.is_nan(), content="false")
/// }
/// ```
pub fn Float::is_nan(self : Float) -> Bool {
self != self
}
45 changes: 45 additions & 0 deletions float/float_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,48 @@ test "Hash" {
inspect!(a.a.hash(), content="749479517")
inspect!((2.0 : Float).hash(), content="-1861484393")
}

test "Float::is_inf/special_values" {
inspect!(@float.infinity.is_inf(), content="true")
inspect!(@float.neg_infinity.is_inf(), content="true")
inspect!(@float.not_a_number.is_inf(), content="false")
inspect!((0.0 : Float).is_inf(), content="false")
inspect!(@float.max_value.is_inf(), content="false")
inspect!(@float.min_value.is_inf(), content="false")
inspect!(@float.min_positive.is_inf(), content="false")
}

test "@float.Float::is_pos_inf/special_values" {
inspect!(@float.infinity.is_pos_inf(), content="true")
inspect!(@float.neg_infinity.is_pos_inf(), content="false")
inspect!(@float.not_a_number.is_pos_inf(), content="false")
inspect!(@float.max_value.is_pos_inf(), content="false")
inspect!((-@float.max_value).is_pos_inf(), content="false")
inspect!(@float.min_positive.is_pos_inf(), content="false")
inspect!((0.0 : Float).is_pos_inf(), content="false")
inspect!((1.0 : Float).is_pos_inf(), content="false")
inspect!((-1.0 : Float).is_pos_inf(), content="false")
}

test "@float.Float::is_neg_inf" {
inspect!((-1.0 : Float).is_neg_inf(), content="false")
inspect!((0.0 : Float).is_neg_inf(), content="false")
inspect!((1.0 : Float).is_neg_inf(), content="false")
inspect!(@float.neg_infinity.is_neg_inf(), content="true")
inspect!(@float.infinity.is_neg_inf(), content="false")
inspect!(@float.not_a_number.is_neg_inf(), content="false")
inspect!(@float.min_value.is_neg_inf(), content="false")
inspect!((@float.min_value - 1.0e38).is_neg_inf(), content="true")
}

test "@float.Float::is_nan" {
inspect!((0.0 : Float).is_nan(), content="false")
inspect!((1.0 : Float).is_nan(), content="false")
inspect!((-1.0 : Float).is_nan(), content="false")
inspect!(@float.not_a_number.is_nan(), content="true")
inspect!(@float.infinity.is_nan(), content="false")
inspect!(@float.neg_infinity.is_nan(), content="false")
inspect!(@float.max_value.is_nan(), content="false")
inspect!(@float.min_value.is_nan(), content="false")
inspect!(@float.min_positive.is_nan(), content="false")
}

0 comments on commit 1726540

Please sign in to comment.