mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Add abs() function
This commit is contained in:
parent
ff25573224
commit
ee84bf7408
@ -50,6 +50,11 @@ impl Angle {
|
|||||||
pub fn to_unit(self, unit: AngularUnit) -> f64 {
|
pub fn to_unit(self, unit: AngularUnit) -> f64 {
|
||||||
self.to_raw() / unit.raw_scale()
|
self.to_raw() / unit.raw_scale()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The absolute value of the this angle.
|
||||||
|
pub fn abs(self) -> Self {
|
||||||
|
Self::raw(self.to_raw().abs())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Angle {
|
impl Debug for Angle {
|
||||||
|
@ -29,6 +29,11 @@ impl Fractional {
|
|||||||
pub fn is_zero(self) -> bool {
|
pub fn is_zero(self) -> bool {
|
||||||
self.0 == 0.0
|
self.0 == 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The absolute value of the this fractional.
|
||||||
|
pub fn abs(self) -> Self {
|
||||||
|
Self::new(self.get().abs())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Fractional {
|
impl Debug for Fractional {
|
||||||
|
@ -92,6 +92,11 @@ impl Length {
|
|||||||
self.0.into_inner().is_infinite()
|
self.0.into_inner().is_infinite()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The absolute value of the this length.
|
||||||
|
pub fn abs(self) -> Self {
|
||||||
|
Self::raw(self.to_raw().abs())
|
||||||
|
}
|
||||||
|
|
||||||
/// The minimum of this and another length.
|
/// The minimum of this and another length.
|
||||||
pub fn min(self, other: Self) -> Self {
|
pub fn min(self, other: Self) -> Self {
|
||||||
Self(self.0.min(other.0))
|
Self(self.0.min(other.0))
|
||||||
|
@ -42,6 +42,11 @@ impl Relative {
|
|||||||
pub fn is_zero(self) -> bool {
|
pub fn is_zero(self) -> bool {
|
||||||
self.0 == 0.0
|
self.0 == 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The absolute value of the this relative.
|
||||||
|
pub fn abs(self) -> Self {
|
||||||
|
Self::new(self.get().abs())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Relative {
|
impl Debug for Relative {
|
||||||
|
@ -61,6 +61,7 @@ pub fn new() -> Scope {
|
|||||||
std.def_func("repr", repr);
|
std.def_func("repr", repr);
|
||||||
std.def_func("len", len);
|
std.def_func("len", len);
|
||||||
std.def_func("rgb", rgb);
|
std.def_func("rgb", rgb);
|
||||||
|
std.def_func("abs", abs);
|
||||||
std.def_func("min", min);
|
std.def_func("min", min);
|
||||||
std.def_func("max", max);
|
std.def_func("max", max);
|
||||||
std.def_func("lower", lower);
|
std.def_func("lower", lower);
|
||||||
|
@ -45,6 +45,21 @@ pub fn rgb(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `abs`: The absolute value of a numeric value.
|
||||||
|
pub fn abs(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
||||||
|
let Spanned { v, span } = args.expect("numeric value")?;
|
||||||
|
Ok(match v {
|
||||||
|
Value::Int(v) => Value::Int(v.abs()),
|
||||||
|
Value::Float(v) => Value::Float(v.abs()),
|
||||||
|
Value::Length(v) => Value::Length(v.abs()),
|
||||||
|
Value::Angle(v) => Value::Angle(v.abs()),
|
||||||
|
Value::Relative(v) => Value::Relative(v.abs()),
|
||||||
|
Value::Fractional(v) => Value::Fractional(v.abs()),
|
||||||
|
Value::Linear(_) => bail!(span, "cannot take absolute value of a linear"),
|
||||||
|
_ => bail!(span, "expected numeric value"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// `min`: The minimum of a sequence of values.
|
/// `min`: The minimum of a sequence of values.
|
||||||
pub fn min(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
pub fn min(_: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
|
||||||
minmax(args, Ordering::Less)
|
minmax(args, Ordering::Less)
|
||||||
|
@ -1,6 +1,24 @@
|
|||||||
// Test math functions.
|
// Test math functions.
|
||||||
// Ref: false
|
// Ref: false
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test `abs` function.
|
||||||
|
#test(abs(-3), 3)
|
||||||
|
#test(abs(3), 3)
|
||||||
|
#test(abs(-0.0), 0.0)
|
||||||
|
#test(abs(0.0), -0.0)
|
||||||
|
#test(abs(-3.14), 3.14)
|
||||||
|
#test(abs(-12pt), 12pt)
|
||||||
|
#test(abs(50%), 50%)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 6-16 cannot take absolute value of a linear
|
||||||
|
#abs(10pt + 50%)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 6-17 expected numeric value
|
||||||
|
#abs("no number")
|
||||||
|
|
||||||
---
|
---
|
||||||
// Test `min` and `max` functions.
|
// Test `min` and `max` functions.
|
||||||
#test(min(2, -4), -4)
|
#test(min(2, -4), -4)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user