1 //! Rounding-scheme identifiers.
2 
3 #![allow(dead_code)]
4 
5 /// Rounding type for float-parsing.
6 ///
7 /// Defines the IEEE754 rounding scheme to be used during float parsing.
8 /// In general, this should be set to `NearestTieEven`, the default
9 /// recommended rounding scheme by IEEE754 for binary and decimal
10 /// operations.
11 ///
12 /// # FFI
13 ///
14 /// For interfacing with FFI-code, this may be approximated by:
15 /// ```text
16 /// const int32_t NEAREST_TIE_EVEN = 0;
17 /// const int32_t NEAREST_TIE_AWAY_ZERO = 1;
18 /// const int32_t TOWARD_POSITIVE_INFINITY = 2;
19 /// const int32_t TOWARD_NEGATIVE_INFINITY = 3;
20 /// const int32_t TOWARD_ZERO = 4;
21 /// ```
22 ///
23 /// # Safety
24 ///
25 /// Assigning any value outside the range `[1-4]` to value of type
26 /// RoundingKind may invoke undefined-behavior.
27 #[repr(i32)]
28 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
29 pub enum RoundingKind {
30     /// Round to the nearest, tie to even.
31     NearestTieEven = 0,
32     /// Round to the nearest, tie away from zero.
33     NearestTieAwayZero = 1,
34     /// Round toward positive infinity.
35     TowardPositiveInfinity = 2,
36     /// Round toward negative infinity.
37     TowardNegativeInfinity = 3,
38     /// Round toward zero.
39     TowardZero = 4,
40 
41     // Hide the internal implementation details, for how we implement
42     // TowardPositiveInfinity, TowardNegativeInfinity, and TowardZero.
43 
44     /// Round to increase the magnitude of the float.
45     /// For example, for a negative number, this rounds to negative infinity,
46     /// for a positive number, to positive infinity.
47     #[doc(hidden)]
48     Upward = -1,
49 
50     /// Round to decrease the magnitude of the float.
51     /// This always rounds toward zero.
52     #[doc(hidden)]
53     Downward = -2,
54 }
55 
56 /// Determine if we are rounding to the nearest value, then tying away.
57 #[inline]
58 pub(crate) fn is_nearest(kind: RoundingKind) -> bool {
59     kind == RoundingKind::NearestTieEven || kind == RoundingKind::NearestTieAwayZero
60 }
61 
62 /// Determine if we are rounding to the nearest value, then tying away.
63 #[inline]
64 pub(crate) fn is_toward(kind: RoundingKind) -> bool {
65     !is_nearest(kind)
66 }
67