Skip to content

Commit c7db709

Browse files
authored
Allow treating ratios as floats (typst#681)
1 parent ef50f1b commit c7db709

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

library/src/compute/construct.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cast_from_value! {
4545
///
4646
/// - Booleans are converted to `0.0` or `1.0`.
4747
/// - Integers are converted to the closest 64-bit float.
48+
/// - Ratios are divided by 100%.
4849
/// - Strings are parsed in base 10 to the closest 64-bit float.
4950
/// Exponential notation is supported.
5051
///
@@ -53,6 +54,7 @@ cast_from_value! {
5354
/// #float(false) \
5455
/// #float(true) \
5556
/// #float(4) \
57+
/// #float(40%) \
5658
/// #float("2.7") \
5759
/// #float("1e5")
5860
/// ```
@@ -76,6 +78,7 @@ cast_from_value! {
7678
v: bool => Self(v as i64 as f64),
7779
v: i64 => Self(v as f64),
7880
v: f64 => Self(v),
81+
v: Ratio => Self(v.get()),
7982
v: EcoString => Self(v.parse().map_err(|_| "not a valid float")?),
8083
}
8184

src/eval/ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
179179
(Int(a), Angle(b)) => Angle(a as f64 * b),
180180
(Float(a), Angle(b)) => Angle(a * b),
181181

182+
(Ratio(a), Ratio(b)) => Ratio(a * b),
182183
(Ratio(a), Int(b)) => Ratio(a * b as f64),
183184
(Ratio(a), Float(b)) => Ratio(a * b),
184185
(Float(a), Ratio(b)) => Ratio(a * b),
@@ -214,8 +215,10 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
214215
Ok(match (lhs, rhs) {
215216
(Int(a), Int(b)) => Float(a as f64 / b as f64),
216217
(Int(a), Float(b)) => Float(a as f64 / b),
218+
(Int(a), Ratio(b)) => Float(a as f64 / b),
217219
(Float(a), Int(b)) => Float(a / b as f64),
218220
(Float(a), Float(b)) => Float(a / b),
221+
(Float(a), Ratio(b)) => Float(a / b),
219222

220223
(Length(a), Int(b)) => Length(a / b as f64),
221224
(Length(a), Float(b)) => Length(a / b),

src/geom/ratio.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ impl Div<f64> for Ratio {
110110
}
111111
}
112112

113+
impl Div<Ratio> for f64 {
114+
type Output = Self;
115+
116+
fn div(self, other: Ratio) -> Self {
117+
self / other.get()
118+
}
119+
}
120+
113121
impl Div for Ratio {
114122
type Output = f64;
115123

tests/typ/compute/calc.typ

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#test(int("150"), 150)
1010
#test(int(10 / 3), 3)
1111
#test(float(10), 10.0)
12+
#test(float(50% * 30%), 0.15)
1213
#test(float("31.4e-1"), 3.14)
1314
#test(type(float(10)), "float")
1415

@@ -21,7 +22,7 @@
2122
#int(10pt)
2223

2324
---
24-
// Error: 8-13 expected boolean, integer, float, or string, found function
25+
// Error: 8-13 expected boolean, integer, float, ratio, or string, found function
2526
#float(float)
2627

2728
---

0 commit comments

Comments
 (0)