Allow treating ratios as floats (#681)

This commit is contained in:
Eric Biedert 2023-04-11 14:13:01 +02:00 committed by GitHub
parent ef50f1b011
commit c7db709da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 1 deletions

View File

@ -45,6 +45,7 @@ cast_from_value! {
/// ///
/// - Booleans are converted to `0.0` or `1.0`. /// - Booleans are converted to `0.0` or `1.0`.
/// - Integers are converted to the closest 64-bit float. /// - Integers are converted to the closest 64-bit float.
/// - Ratios are divided by 100%.
/// - Strings are parsed in base 10 to the closest 64-bit float. /// - Strings are parsed in base 10 to the closest 64-bit float.
/// Exponential notation is supported. /// Exponential notation is supported.
/// ///
@ -53,6 +54,7 @@ cast_from_value! {
/// #float(false) \ /// #float(false) \
/// #float(true) \ /// #float(true) \
/// #float(4) \ /// #float(4) \
/// #float(40%) \
/// #float("2.7") \ /// #float("2.7") \
/// #float("1e5") /// #float("1e5")
/// ``` /// ```
@ -76,6 +78,7 @@ cast_from_value! {
v: bool => Self(v as i64 as f64), v: bool => Self(v as i64 as f64),
v: i64 => Self(v as f64), v: i64 => Self(v as f64),
v: f64 => Self(v), v: f64 => Self(v),
v: Ratio => Self(v.get()),
v: EcoString => Self(v.parse().map_err(|_| "not a valid float")?), v: EcoString => Self(v.parse().map_err(|_| "not a valid float")?),
} }

View File

@ -179,6 +179,7 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
(Int(a), Angle(b)) => Angle(a as f64 * b), (Int(a), Angle(b)) => Angle(a as f64 * b),
(Float(a), Angle(b)) => Angle(a * b), (Float(a), Angle(b)) => Angle(a * b),
(Ratio(a), Ratio(b)) => Ratio(a * b),
(Ratio(a), Int(b)) => Ratio(a * b as f64), (Ratio(a), Int(b)) => Ratio(a * b as f64),
(Ratio(a), Float(b)) => Ratio(a * b), (Ratio(a), Float(b)) => Ratio(a * b),
(Float(a), Ratio(b)) => Ratio(a * b), (Float(a), Ratio(b)) => Ratio(a * b),
@ -214,8 +215,10 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
Ok(match (lhs, rhs) { Ok(match (lhs, rhs) {
(Int(a), Int(b)) => Float(a as f64 / b as f64), (Int(a), Int(b)) => Float(a as f64 / b as f64),
(Int(a), Float(b)) => Float(a as f64 / b), (Int(a), Float(b)) => Float(a as f64 / b),
(Int(a), Ratio(b)) => Float(a as f64 / b),
(Float(a), Int(b)) => Float(a / b as f64), (Float(a), Int(b)) => Float(a / b as f64),
(Float(a), Float(b)) => Float(a / b), (Float(a), Float(b)) => Float(a / b),
(Float(a), Ratio(b)) => Float(a / b),
(Length(a), Int(b)) => Length(a / b as f64), (Length(a), Int(b)) => Length(a / b as f64),
(Length(a), Float(b)) => Length(a / b), (Length(a), Float(b)) => Length(a / b),

View File

@ -110,6 +110,14 @@ impl Div<f64> for Ratio {
} }
} }
impl Div<Ratio> for f64 {
type Output = Self;
fn div(self, other: Ratio) -> Self {
self / other.get()
}
}
impl Div for Ratio { impl Div for Ratio {
type Output = f64; type Output = f64;

View File

@ -9,6 +9,7 @@
#test(int("150"), 150) #test(int("150"), 150)
#test(int(10 / 3), 3) #test(int(10 / 3), 3)
#test(float(10), 10.0) #test(float(10), 10.0)
#test(float(50% * 30%), 0.15)
#test(float("31.4e-1"), 3.14) #test(float("31.4e-1"), 3.14)
#test(type(float(10)), "float") #test(type(float(10)), "float")
@ -21,7 +22,7 @@
#int(10pt) #int(10pt)
--- ---
// Error: 8-13 expected boolean, integer, float, or string, found function // Error: 8-13 expected boolean, integer, float, ratio, or string, found function
#float(float) #float(float)
--- ---