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
use *;
// TODO add examples to docs after the impls (for ints, etc) are done
/// Used to do value-to-value conversions using the rules prescribed for that conversion in the
/// [posit standard], which may *round* the input (see below). It is the reciprocal of
/// [`RoundInto`].
///
/// The interface is identical to the standard [`From`], but for the [conversions described in the
/// posit standard]; therefore — unlike that which is the
/// [convention for the `From` trait](core::convert::From#when-to-implement-from) —
/// these conversions are _not necessarily lossless_, and may round if necessary, according the
/// [definition in the posit standard].
///
/// The exact meaning of these conversions depends on the types involved; for the exact description
/// of what each particular conversion does, **consult the documentation for specific
/// implementations of `round_from`**.
///
/// Many of the usage guidelines for [`From`] also apply to [`RoundFrom`]: if you do implement it
/// for your types, prefer implementing [`RoundFrom`] over [`RoundInto`] because implementing
/// [`RoundFrom`] automatically provides one with an implementation of [`RoundInto`], and prefer
/// using [`RoundInto`] over [`RoundFrom`] when specifying trait bounds on a generic function.
/// There's also a blanket implementation of `RoundFrom<T> for T`, and `RoundFrom<T> for U`
/// implies `RoundInto<U> for T`.
///
/// # Rounding
///
/// "Rounding" in this crate stands for the [definition in the posit standard]. In short:
///
/// - If the value is greater in absolute value than the biggest posit, round to it (i.e., never
/// overflow).
/// - If the value is smaller in absolute value than the smallest positive posit, round to it
/// (i.e., never underflow).
/// - Otherwise, round to the nearest bit pattern, or in case of a tie, to the even bit pattern.
///
/// # Examples
///
/// Rounding from ints, floats:
/// ```
/// # use fast_posit::*;
/// assert!(p16::round_from(1) == p16::round_from(1.00000001));
/// assert!(p32::round_from(1) < p32::round_from(1.00000001));
///
/// assert_eq!(p32::round_from(f64::NAN), p32::NAR);
/// ```
///
/// Rounding to ints, floats:
/// ```
/// # use fast_posit::*;
/// assert_eq!(f32::round_from(p16::MIN_POSITIVE), 1.3877788e-17);
/// assert_eq!(i64::round_from(p8::MAX), 1 << 24);
///
/// assert!(f64::round_from(p32::NAR).is_nan());
/// ```
///
/// [posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.6
/// [conversions described in the posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.6
/// [definition in the posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.4
/// Used to do value-to-value conversions using the rules prescribed for that conversion in the
/// [posit standard], which may *round* the input (see below). It is the reciprocal of
/// [`RoundFrom`].
///
/// The interface is identical to the standard [`Into`], but for the [conversions described in the
/// posit standard]; therefore — unlike that which is the
/// [convention for the `From` and `Into` traits](core::convert::From#when-to-implement-from) —
/// these conversions are _not necessarily lossless_, and may round if necessary, according the
/// [definition in the posit standard].
///
/// The exact meaning of these conversions depends on the types involved; for the exact description
/// of what each particular conversion does, **consult the documentation for specific
/// implementations of `round_from`**.
///
/// Many of the usage guidelines for [`Into`] also apply to [`RoundInto`]: if you do implement it
/// for your types, prefer implementing [`RoundFrom`] over [`RoundInto`] because implementing
/// [`RoundFrom`] automatically provides one with an implementation of [`RoundInto`], and prefer
/// using [`RoundInto`] over [`RoundFrom`] when specifying trait bounds on a generic function.
/// There's also a blanket implementation of `RoundInto<T> for T`, and `RoundFrom<T> for U`
/// implies `RoundInto<U> for T`.
///
/// # Rounding
///
/// "Rounding" in this crate stands for the [definition in the posit standard]. In short:
///
/// - If the value is greater in absolute value than the biggest posit, round to it (i.e., never
/// overflow).
/// - If the value is smaller in absolute value than the smallest positive posit, round to it
/// (i.e., never underflow).
/// - Otherwise, round to the nearest bit pattern, or in case of a tie, to the even bit pattern.
///
/// # Examples
///
/// Rounding from ints, floats:
/// ```
/// # use fast_posit::*;
/// assert_eq!(p16::ONE.next(), 1.0004883_f64.round_into());
/// assert_eq!(p32::ONE.next(), 1.0000000075_f64.round_into());
///
/// assert_eq!(p32::NAR, f64::NAN.round_into());
/// ```
///
/// Rounding to ints, floats:
/// ```
/// # use fast_posit::*;
/// assert_eq!(5.960464477539063e-8, p8::MIN_POSITIVE.round_into());
/// assert_eq!(1_i64 << 56, p16::MAX.round_into());
///
/// assert!(f64::is_nan(p32::NAR.round_into()));
/// ```
///
/// [posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.6
/// [conversions described in the posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.6
/// [definition in the posit standard]: https://posithub.org/docs/posit_standard-2.pdf#section.4