summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorSébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com>2024-01-12 14:38:58 +0100
committerGitHub <noreply@github.com>2024-01-12 13:38:58 +0000
commit1834ebc52918754dee51afffd8b55e82613eb687 (patch)
tree9fbffc2634472811ce914279e4b433606a7900ec /crates
parentc298cf61f2360eac0ec6a260fad11425b405f49f (diff)
Added `int.signum`, `float.signum`, `float.is-nan`, and `float.is-infinite` (#3118)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst/src/foundations/float.rs47
-rw-r--r--crates/typst/src/foundations/int.rs16
2 files changed, 63 insertions, 0 deletions
diff --git a/crates/typst/src/foundations/float.rs b/crates/typst/src/foundations/float.rs
index 4b38bdf9..6bcebda8 100644
--- a/crates/typst/src/foundations/float.rs
+++ b/crates/typst/src/foundations/float.rs
@@ -47,6 +47,53 @@ impl f64 {
) -> f64 {
value.0
}
+
+ /// Checks if a float is not a number.
+ ///
+ /// In IEEE 754, more than one bit pattern represents a NaN. This function
+ /// returns `true` if the float is any of those bit patterns.
+ ///
+ /// ```example
+ /// #float.is-nan(0) \
+ /// #float.is-nan(1) \
+ /// #float.is-nan(calc.nan)
+ /// ```
+ #[func]
+ pub fn is_nan(self) -> bool {
+ f64::is_nan(self)
+ }
+
+ /// Checks if a float is infinite.
+ ///
+ /// For floats, there is positive and negative infinity. This function
+ /// returns `true` if the float is either positive or negative infinity.
+ ///
+ /// ```example
+ /// #float.is-infinite(0) \
+ /// #float.is-infinite(1) \
+ /// #float.is-infinite(calc.inf)
+ /// ```
+ #[func]
+ pub fn is_infinite(self) -> bool {
+ f64::is_infinite(self)
+ }
+
+ /// Calculates the sign of a floating point number.
+ ///
+ /// - If the number is positive (including `{+0.0}`), returns `{1.0}`.
+ /// - If the number is negative (including `{-0.0}`), returns `{-1.0}`.
+ /// - If the number is [`{calc.nan}`]($calc.nan), returns
+ /// [`{calc.nan}`]($calc.nan).
+ ///
+ /// ```example
+ /// #(5.0).signum() \
+ /// #(-5.0).signum() \
+ /// #(0.0).signum() \
+ /// ```
+ #[func]
+ pub fn signum(self) -> f64 {
+ f64::signum(self)
+ }
}
impl Repr for f64 {
diff --git a/crates/typst/src/foundations/int.rs b/crates/typst/src/foundations/int.rs
index 88b0413a..602cdd5a 100644
--- a/crates/typst/src/foundations/int.rs
+++ b/crates/typst/src/foundations/int.rs
@@ -49,6 +49,22 @@ impl i64 {
) -> i64 {
value.0
}
+
+ /// Calculates the sign of an integer.
+ ///
+ /// - If the number is positive, returns `{1}`.
+ /// - If the number is negative, returns `{-1}`.
+ /// - If the number is zero, returns `{0}`.
+ ///
+ /// ```example
+ /// #(5).signum() \
+ /// #(-5).signum() \
+ /// #(0).signum() \
+ /// ```
+ #[func]
+ pub fn signum(self) -> i64 {
+ i64::signum(self)
+ }
}
impl Repr for i64 {