Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 8 additions & 1 deletion src/sort/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
/// 64-bit floating point numbers supporting these primitives:
/// - Arithmetic: `+`, `-`, `*`, `/`, `%`, `^`, `neg`, `abs`
/// - Comparisons: `<`, `>`, `<=`, `>=`
/// - Other: `min`, `max`, `to-i64`, `to-string`
/// - Other: `min`, `max`, `abs`, `exp`, `log`, `sqrt`, `to-i64`, `to-string`
Comment thread
saulshanabrook marked this conversation as resolved.
Outdated
#[derive(Debug)]
pub struct F64Sort;

Expand Down Expand Up @@ -35,6 +35,13 @@ impl BaseSort for F64Sort {
add_literal_prim!(eg, "min" = |a: F, b: F| -> F { a.min(b) });
add_literal_prim!(eg, "max" = |a: F, b: F| -> F { a.max(b) });
add_literal_prim!(eg, "abs" = |a: F| -> F { F::from(a.abs()) });
add_literal_prim!(eg, "exp" = |a: F| -> F { F::from(OrderedFloat(a.exp())) });
add_literal_prim!(eg, "log" = |a: F| -?> F {
(*a > OrderedFloat(0.0)).then(|| F::from(OrderedFloat(a.ln())))
});
add_literal_prim!(eg, "sqrt" = |a: F| -?> F {
(*a >= OrderedFloat(0.0)).then(|| F::from(OrderedFloat(a.sqrt())))
});

// `to-f64` should be in `i64.rs`, but `F64Sort` wouldn't exist yet
add_literal_prim!(eg, "to-f64" = |a: i64| -> F { F::from(OrderedFloat(a as f64)) });
Expand Down
5 changes: 5 additions & 0 deletions tests/f64.egg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
(check (< 1.5 9.2))
(check (>= 9.2 1.5))
(check (= (^ 9.0 2.5) 243.0))
(check (= (exp 0.0) 1.0))
(check (= (log 1.0) 0.0))
(check (= (sqrt 4.0) 2.0))
(fail (check (= (^ 4.0 2.5) 31.99)))
(fail (log 0.0))
(fail (sqrt -1.0))
(fail (check (< 9.2 1.5)))
(fail (check (= (+ 1.5 9.2) 10.6)))
(check (= (to-f64 1) 1.0))
Expand Down
Loading