malachite_float/arithmetic/sign.rs
1// Copyright © 2025 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::Float;
10use crate::InnerFloat::{Finite, Infinity, Zero};
11use core::cmp::Ordering::{self, *};
12use malachite_base::num::arithmetic::traits::Sign;
13
14impl Sign for Float {
15 /// Returns the sign of a [`Float`].
16 ///
17 /// Returns `Greater` if the sign is positive and `Less` if the sign is negative. Never returns
18 /// `Equal`. $\infty$ and positive zero have a positive sign, and $-\infty$ and negative zero
19 /// have a negative sign.
20 ///
21 /// # Worst-case complexity
22 /// Constant time and additional memory.
23 ///
24 /// # Panics
25 /// Panics if `self` is NaN.
26 ///
27 /// # Examples
28 /// ```
29 /// use malachite_base::num::arithmetic::traits::Sign;
30 /// use malachite_base::num::basic::traits::{
31 /// Infinity, NegativeInfinity, NegativeOne, NegativeZero, One, Zero,
32 /// };
33 /// use malachite_float::Float;
34 /// use std::cmp::Ordering::*;
35 ///
36 /// assert_eq!(Float::INFINITY.sign(), Greater);
37 /// assert_eq!(Float::NEGATIVE_INFINITY.sign(), Less);
38 /// assert_eq!(Float::ZERO.sign(), Greater);
39 /// assert_eq!(Float::NEGATIVE_ZERO.sign(), Less);
40 /// assert_eq!(Float::ONE.sign(), Greater);
41 /// assert_eq!(Float::NEGATIVE_ONE.sign(), Less);
42 /// ```
43 fn sign(&self) -> Ordering {
44 if let Float(Infinity { sign } | Zero { sign } | Finite { sign, .. }) = self {
45 if *sign { Greater } else { Less }
46 } else {
47 panic!()
48 }
49 }
50}