diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Number/Extensions.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Number/Extensions.enso index ab965f9978a9b..e0804712151d7 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Number/Extensions.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Number/Extensions.enso @@ -302,3 +302,23 @@ Parse_Error.to_display_text : Text Parse_Error.to_display_text = "Could not parse " + this.text.to_text + " as a double." +## A constant holding the floating-point positive infinity. +Number.positive_infinity : Decimal +Number.positive_infinity = Double.POSITIVE_INFINITY + +## A constant holding the floating-point negative infinity. +Number.negative_infinity : Decimal +Number.negative_infinity = Double.NEGATIVE_INFINITY + +## A constant holding the floating-point Not-a-Number value. +Number.nan : Decimal +Number.nan = Double.NaN + +## Checks if the given number is the floating-point Not-a-Number value. + + This is needed, because the NaN value will return `False` even when being + compared with itself, so `x == Number.nan` would not work. +Number.is_nan : Boolean +Number.is_nan = case this of + Decimal -> Double.isNaN this + _ -> False diff --git a/test/Tests/src/Data/Numbers_Spec.enso b/test/Tests/src/Data/Numbers_Spec.enso index 1a2dd9935a9e5..51323d20b6eab 100644 --- a/test/Tests/src/Data/Numbers_Spec.enso +++ b/test/Tests/src/Data/Numbers_Spec.enso @@ -262,6 +262,16 @@ spec = almost_max_long_times_three_decimal.ceil.to_decimal . should_equal almost_max_long_times_three_plus_1.to_decimal almost_max_long_times_three_plus_1.ceil . should_equal almost_max_long_times_three_plus_1 + Test.specify "should expose a NaN value" <| + Number.nan.is_nan . should_be_true + 0.is_nan . should_be_false + Number.positive_infinity.is_nan . should_be_false + Number.negative_infinity.is_nan . should_be_false + + Number.nan==Number.nan . should_be_false + Number.nan==0 . should_be_false + Number.nan!=Number.nan . should_be_true + Test.specify "should support inexact equality comparisons" <| 1.0001 . equals 1.0002 epsilon=0.01 . should_be_true 1.0001 . equals 1.0002 epsilon=0.0000001 . should_be_false @@ -269,15 +279,15 @@ spec = 1 . equals 2 . should_be_false 1 . equals (0+1) . should_be_true - inf = 1/0 - inf . equals inf . should_be_true + Number.positive_infinity . equals Number.positive_infinity . should_be_true + + Number.negative_infinity . equals Number.negative_infinity . should_be_true + Number.negative_infinity . equals Number.positive_infinity . should_be_false - neg_inf = -inf - neg_inf . equals neg_inf . should_be_true - neg_inf . equals inf . should_be_false + Number.negative_infinity . should_equal (-Number.positive_infinity) + Number.negative_infinity . equals (-Number.positive_infinity) . should_be_true - nan = 0.log 0 - nan . equals nan . should_be_false - nan . equals 0 . should_be_false + Number.nan . equals Number.nan . should_be_false + Number.nan . equals 0 . should_be_false main = Test.Suite.run_main here.spec