malachite_nz/natural/arithmetic/sign.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::natural::Natural;
10use core::cmp::Ordering::{self, *};
11use malachite_base::num::arithmetic::traits::Sign;
12
13impl Sign for Natural {
14 /// Compares a [`Natural`] to zero.
15 ///
16 /// Returns `Greater` or `Equal` depending on whether the [`Natural`] is positive or zero,
17 /// respectively.
18 ///
19 /// # Worst-case complexity
20 /// Constant time and additional memory.
21 ///
22 /// # Examples
23 /// ```
24 /// use core::cmp::Ordering::*;
25 /// use malachite_base::num::arithmetic::traits::Sign;
26 /// use malachite_base::num::basic::traits::Zero;
27 /// use malachite_nz::natural::Natural;
28 ///
29 /// assert_eq!(Natural::ZERO.sign(), Equal);
30 /// assert_eq!(Natural::from(123u32).sign(), Greater);
31 /// ```
32 fn sign(&self) -> Ordering {
33 if *self == 0 { Equal } else { Greater }
34 }
35}