decimal/lib.rs
1#[macro_use]
2extern crate bitflags;
3extern crate libc;
4#[cfg(feature = "ord_subset")]
5extern crate ord_subset;
6#[cfg(feature = "rustc-serialize")]
7extern crate rustc_serialize;
8#[cfg(feature = "serde")]
9extern crate serde;
10#[cfg(feature = "serde")]
11#[cfg(test)]
12extern crate serde_json;
13
14#[macro_export]
15/// A macro to construct d128 literals.
16///
17/// # Examples:
18/// ```
19/// # #[macro_use]
20/// # extern crate decimal;
21/// # fn main() {
22/// assert!(d128!(NaN).is_nan());
23/// assert!(d128!(0).is_zero());
24/// assert!(d128!(-0.1).is_negative());
25/// # }
26/// ```
27macro_rules! d128 {
28 ($lit:expr) => {{
29 use std::str::FromStr;
30 $crate::d128::from_str(stringify!($lit)).expect("Invalid decimal float literal")
31 }}
32}
33
34mod context;
35mod dec128;
36
37pub use dec128::d128;
38
39#[repr(C)]
40#[derive(Clone, Copy, Debug, PartialEq)]
41pub enum Rounding {
42 Ceiling = 0,
43 Up,
44 HalfUp,
45 /// Round to nearest; if equidistant, round so that the final digit is even.
46 /// This is the only rounding mode supported.
47 HalfEven,
48 HalfDown,
49 Down,
50 Floor,
51 ZeroOrFiveUp,
52}
53
54#[repr(C)]
55#[derive(Clone, Copy, Debug, PartialEq)]
56#[allow(dead_code)]
57pub enum Class {
58 Snan = 0,
59 Qnan,
60 NegInf,
61 NegNormal,
62 NegSubnormal,
63 NegZero,
64 PosZero,
65 PosSubnormal,
66 PosNormal,
67 PosInf,
68}
69
70bitflags! {
71 /// The status of the floating point context. Instead of faulting after every operation errors
72 /// are added to this status. User code can check and/or clear the status fully or partially at
73 /// specific points to check the validity of the computation. Each thread gets its own status.
74 /// The status is initially empty.
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// # #[macro_use]
80 /// # extern crate decimal;
81 /// # use decimal::d128;
82 /// # use decimal::Status;
83 /// # fn main() {
84 /// assert_eq!(d128::get_status(), Status::empty());
85 /// let _ = d128!(1) / &d128!(0);
86 /// assert!(d128::get_status().contains(decimal::Status::DIVISION_BY_ZERO));
87 /// let _ = d128!(0) / &d128!(0);
88 /// assert!(d128::get_status().contains(decimal::Status::DIVISION_UNDEFINED));
89 /// // The previous status flag was not cleared!
90 /// assert!(d128::get_status().contains(decimal::Status::DIVISION_BY_ZERO));
91 /// # }
92 pub struct Status: u32 {
93 /// Conversion syntax error.
94 const CONVERSION_SYNTAX = 0x00000001;
95 /// Division by zero.
96 const DIVISION_BY_ZERO = 0x00000002;
97 /// Division impossible.
98 const DIVISION_IMPOSSIBLE = 0x00000004;
99 /// Division undefined.
100 const DIVISION_UNDEFINED = 0x00000008;
101 // const INSUFFICIENT_STORAGE = 0x00000010;
102 /// The result is inexact.
103 const INEXACT = 0x00000020;
104 // const INVALID_CONTEXT = 0x00000040;
105 /// An invalid operation was requested.
106 const INVALID_OPERATION = 0x00000080;
107 // const LOST_DIGITS = 0x00000100;
108 /// Overflow.
109 const OVERFLOW = 0x00000200;
110 // const CLAMPED = 0x00000400;
111 // const ROUNDED = 0x00000800;
112 // const SUBNORMAL = 0x00001000;
113 /// Underflow.
114 const UNDERFLOW = 0x00002000;
115 }
116}