Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Fix is-int and is-float; add tests #147

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/main/eo/org/eolang/math/number.eo
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
[] > is-int
^.n > value!
if. > @
value.eq 0
^.is-int-zero
TRUE
if.
value.eq 0.0
^.is-float-zero
FALSE
eq.
div.
Expand All @@ -55,17 +55,38 @@
[] > is-float
^.n > value!
if. > @
value.eq 0.0
^.is-float-zero
TRUE
if.
value.eq 0
^.is-int-zero
FALSE
eq.
div.
value
value
1.0

# Checking if number is integer 0
[] > is-int-zero
^.n > value!
eq. > @
value.as-bytes
value.neg.as-bytes

# Checking if number is floatint point 0.0 or -0.0
[] > is-float-zero
^.n > value!
if. > @
eq.
value.as-bytes
0.0.as-bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 let's make a minor optimization and replace 0.0.as-bytes with 00-00-00.... The same story with -0.0.as-bytes below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 or at least you can cache 0.0.as-bytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eq.
value.neg.as-bytes
-0.0.as-bytes
eq.
value.neg.as-bytes
0.0.as-bytes

# Checking if number is NaN
[] > is-nan
not. > @
Expand Down
96 changes: 96 additions & 0 deletions src/test/eo/org/eolang/math/number-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,90 @@
$.equal-to FALSE
"check is nan of not nan"

[] > check-is-float-zero-of-float-zero
assert-that > @
(number 0.0).is-float-zero
$.equal-to TRUE
"check is float zero of float zero"

[] > check-is-float-zero-of-float-neg-zero
assert-that > @
(number -0.0).is-float-zero
$.equal-to TRUE
"check is float zero of float negative zero"

[] > check-is-float-zero-of-positive-float
assert-that > @
(number 4.26).is-float-zero
$.equal-to FALSE
"check is float zero of positive float"

[] > check-is-float-zero-of-negative-float
assert-that > @
(number -2.07).is-float-zero
$.equal-to FALSE
"check is float zero of negative float"

[] > check-is-float-zero-of-int-zero
assert-that > @
(number 0).is-float-zero
$.equal-to FALSE
"check is float zero of int zero"

[] > check-is-float-zero-of-positive-int
assert-that > @
(number 414).is-float-zero
$.equal-to FALSE
"check is float zero of positive int"

[] > check-is-float-zero-of-negative-int
assert-that > @
(number -201).is-float-zero
$.equal-to FALSE
"check is float zero of negative int"

[] > check-is-int-zero-of-float-zero
assert-that > @
(number 0.0).is-int-zero
$.equal-to FALSE
"check is int zero of float zero"

[] > check-is-int-zero-of-float-neg-zero
assert-that > @
(number -0.0).is-int-zero
$.equal-to FALSE
"check is int zero of float negative zero"

[] > check-is-int-zero-of-positive-float
assert-that > @
(number 4.26).is-int-zero
$.equal-to FALSE
"check is int zero of positive float"

[] > check-is-int-zero-of-negative-float
assert-that > @
(number -2.07).is-int-zero
$.equal-to FALSE
"check is int zero of negative float"

[] > check-is-int-zero-of-int-zero
assert-that > @
(number 0).is-int-zero
$.equal-to TRUE
"check is int zero of int zero"

[] > check-is-int-zero-of-positive-int
assert-that > @
(number 414).is-int-zero
$.equal-to FALSE
"check is int zero of positive int"

[] > check-is-int-zero-of-negative-int
assert-that > @
(number -201).is-int-zero
$.equal-to FALSE
"check is int zero of negative int"

[] > check-is-int-of-int-zero
assert-that > @
(number 0).is-int
Expand All @@ -81,6 +165,18 @@
$.equal-to TRUE
"check is-float of float zero"

[] > check-is-float-of-float-neg-zero
assert-that > @
(number -0.0).is-float
$.equal-to TRUE
"check is-float of float negative zero"

[] > check-is-int-of-float-neg-zero
assert-that > @
(number -0.0).is-int
$.equal-to FALSE
"check is-int of float negative zero"

[] > check-is-int-positive-float
assert-that > @
(number 123.35).is-int
Expand Down