Skip to content

Commit

Permalink
Add cmpf to float.mc
Browse files Browse the repository at this point in the history
  • Loading branch information
br4sco committed Apr 6, 2024
1 parent 1070a5a commit d06349c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions stdlib/float.mc
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,28 @@ utest absf 1. with 1. using eqf
utest absf -1. with 1. using eqf
utest absf -0. with 0. using eqf
utest isNaN (absf nan) with true


-- `cmpf l r` compares `l` and `r`. `cmpf l r = -1` if l < r, `cmpf l r = 1` if
-- l > r, and `cmpf l r = 0` if l = r. `nan` is considered smaller than negative
-- infinity and equal to itself.
let cmpf: Float -> Float -> Int = lam l. lam r.
let lIsNaN = isNaN l in
let rIsNaN = isNaN r in
if and lIsNaN rIsNaN then 0
else if lIsNaN then -1
else if rIsNaN then 1
else
if ltf l r then -1 else if gtf l r then 1 else 0

utest cmpf 0. 0. with 0
utest cmpf 1. 0. with 1
utest cmpf 0. 1. with -1
utest cmpf inf inf with 0
utest cmpf (negf inf) (negf inf) with 0
utest cmpf inf inf with 0
utest cmpf (negf inf) 0. with -1
utest cmpf 0. (negf inf) with 1
utest cmpf nan nan with 0
utest cmpf nan (negf inf) with -1
utest cmpf (negf inf) nan with 1

0 comments on commit d06349c

Please sign in to comment.