Skip to content

Commit

Permalink
Add numbers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Apr 11, 2022
1 parent 330b40e commit f627191
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 18 additions & 8 deletions test/Tests/src/Data/Numbers_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,32 @@ 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

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

0 comments on commit f627191

Please sign in to comment.