Skip to content

Commit

Permalink
Add to-string function for floats and ints (#190)
Browse files Browse the repository at this point in the history
Adds a `to-string` function for floats and ints.

For floats, it uses the debug form instead of the `to_string` so that
the decimal place is always printed.
  • Loading branch information
saulshanabrook authored Aug 17, 2023
1 parent 6df6513 commit 3f7c3fe
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ Signed 64-bit integers supporting these primitives:
< > <= >= ; comparisons
min max log2
to-f64
to-string
```

### Sort: f64
Expand All @@ -411,6 +412,7 @@ to-f64
< > <= >= ; comparisons
min max neg
to-i64
to-string
```

### Sort: map
Expand Down
2 changes: 2 additions & 0 deletions src/sort/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impl Sort for F64Sort {

add_primitives!(eg, "to-f64" = |a: i64| -> f64 { a as f64 });
add_primitives!(eg, "to-i64" = |a: f64| -> i64 { a as i64 });
// Use debug instead of to_string so that decimal place is always printed
add_primitives!(eg, "to-string" = |a: f64| -> Symbol { format!("{:?}", a).into() });

}

Expand Down
3 changes: 3 additions & 0 deletions src/sort/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl Sort for I64Sort {

add_primitives!(typeinfo, "min" = |a: i64, b: i64| -> i64 { a.min(b) });
add_primitives!(typeinfo, "max" = |a: i64, b: i64| -> i64 { a.max(b) });

add_primitives!(typeinfo, "to-string" = |a: i64| -> Symbol { a.to_string().into() });

}

fn make_expr(&self, _egraph: &EGraph, value: Value) -> Expr {
Expand Down
2 changes: 2 additions & 0 deletions tests/f64.egg
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
(fail (check (= (+ 1.5 9.2) 10.6)))
(check (= (to-f64 1) 1.0))
(check (= (to-i64 1.0) 1))
(check (= (to-string 1.2) "1.2"))
(check (= (to-string 1.0) "1.0"))
1 change: 1 addition & 0 deletions tests/i64.egg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(check (= (to-string 20) "20"))

0 comments on commit 3f7c3fe

Please sign in to comment.