1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::Float;
use core::cmp::Ordering::{self, *};
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity, NegativeZero, Zero};
use malachite_base::num::conversion::traits::IntegerMantissaAndExponent;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode;

// This differs from the `precision` function provided by `PrimitiveFloat`. That function returns
// the smallest precision necessary to represent the float, whereas this function returns the
// maximum precision of any float in the same binade. If the float is non-finite or zero, 1 is
// returned.
pub_test! {alt_precision<T: PrimitiveFloat>(x: T) -> u64 {
    if x.is_finite() && x != T::ZERO {
        let (mantissa, exponent) = x.raw_mantissa_and_exponent();
        if exponent == 0 {
            mantissa.significant_bits()
        } else {
            T::MANTISSA_WIDTH + 1
        }
    } else {
        1
    }
}}

impl Float {
    /// Converts a primitive float to a [`Float`]. If the [`Float`] is nonzero and finite, it has
    /// the specified precision. If rounding is needed, the specified rounding mode is used. An
    /// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to,
    /// or greater than the original value. (Although a NaN is not comparable to any [`Float`],
    /// converting a NaN to a NaN will also return `Equal`, indicating an exact conversion.)
    ///
    /// If you're only using `Nearest`, try using [`Float::from_primitive_float_prec`] instead.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.sci_exponent().abs())`.
    ///
    /// # Examples
    /// See [here](super::from_primitive_float#from_primitive_float_prec_round).
    #[inline]
    pub fn from_primitive_float_prec_round<T: PrimitiveFloat>(
        x: T,
        prec: u64,
        rm: RoundingMode,
    ) -> (Float, Ordering) {
        assert_ne!(prec, 0);
        if x.is_nan() {
            (Float::NAN, Equal)
        } else if !x.is_finite() {
            if x.is_sign_positive() {
                (Float::INFINITY, Equal)
            } else {
                (Float::NEGATIVE_INFINITY, Equal)
            }
        } else if x == T::ZERO {
            if x.is_sign_positive() {
                (Float::ZERO, Equal)
            } else {
                (Float::NEGATIVE_ZERO, Equal)
            }
        } else {
            let (m, e) = x.integer_mantissa_and_exponent();
            if x.is_sign_positive() {
                let (f, o) = Float::from_unsigned_prec_round(m, prec, rm);
                (f << e, o)
            } else {
                let (abs, o) = Float::from_unsigned_prec_round(m, prec, -rm);
                (-(abs << e), o.reverse())
            }
        }
    }

    /// Converts a primitive float to a [`Float`]. If the [`Float`] is nonzero and finite, it has
    /// the specified precision. An [`Ordering`] is also returned, indicating whether the returned
    /// value is less than, equal to, or greater than the original value. (Although a NaN is not
    /// comparable to any [`Float`], converting a NaN to a NaN will also return `Equal`, indicating
    /// an exact conversion.)
    ///
    /// Rounding may occur, in which case `Nearest` is used by default. To specify a rounding mode
    /// as well as a precision, try [`Float::from_primitive_float_prec_round`].
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.sci_exponent().abs())`.
    ///
    /// # Examples
    /// See [here](super::from_primitive_float#from_primitive_float_prec).
    #[inline]
    pub fn from_primitive_float_prec<T: PrimitiveFloat>(x: T, prec: u64) -> (Float, Ordering) {
        assert_ne!(prec, 0);
        if x.is_nan() {
            (Float::NAN, Equal)
        } else if !x.is_finite() {
            if x.is_sign_positive() {
                (Float::INFINITY, Equal)
            } else {
                (Float::NEGATIVE_INFINITY, Equal)
            }
        } else if x == T::ZERO {
            if x.is_sign_positive() {
                (Float::ZERO, Equal)
            } else {
                (Float::NEGATIVE_ZERO, Equal)
            }
        } else {
            let (m, e) = x.integer_mantissa_and_exponent();
            if x.is_sign_positive() {
                let (f, o) = Float::from_unsigned_prec(m, prec);
                (f << e, o)
            } else {
                let (abs, o) = Float::from_unsigned_prec(m, prec);
                (-(abs << e), o.reverse())
            }
        }
    }
}

macro_rules! impl_from_primitive_float {
    ($t: ident) => {
        impl From<$t> for Float {
            /// Converts a primitive float to a [`Float`].
            ///
            /// If the primitive float is finite and nonzero, the precision of the [`Float`] is
            /// equal to the maximum precision of any primitive float in the same binade (for normal
            /// `f32`s this is 24, and for normal `f64`s it is 53). If you want to specify a
            /// different precision, try [`Float::from_primitive_float_prec`]. This may require
            /// rounding, which uses `Nearest` by default. To specify a rounding mode as well as a
            /// precision, try [`Float::from_primitive_float_prec_round`].
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n)$
            ///
            /// $M(n) = O(n)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is `x.sci_exponent().abs()`.
            ///
            /// # Examples
            /// See [here](super::from_primitive_float#from).
            #[inline]
            fn from(x: $t) -> Float {
                if x.is_nan() {
                    Float::NAN
                } else if !x.is_finite() {
                    if x.is_sign_positive() {
                        Float::INFINITY
                    } else {
                        Float::NEGATIVE_INFINITY
                    }
                } else if x == 0.0 {
                    if x.is_sign_positive() {
                        Float::ZERO
                    } else {
                        Float::NEGATIVE_ZERO
                    }
                } else {
                    let (m, e) = x.integer_mantissa_and_exponent();
                    let abs = Float::from_unsigned_prec(m, alt_precision(x)).0 << e;
                    if x.is_sign_positive() {
                        abs
                    } else {
                        -abs
                    }
                }
            }
        }
    };
}
apply_to_primitive_floats!(impl_from_primitive_float);