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
184
185
186
187
188
189
190
191
192
193
194
use std::{
borrow::Borrow,
fmt,
ops::{Add, Deref, DerefMut, Div, Mul, Sub},
str::FromStr,
};
use ordered_float::OrderedFloat;
use crate::{lexical, Datatype, XsdDatatype};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Double(OrderedFloat<f64>);
impl Double {
pub const NEG_INFINITY: Self = Self(OrderedFloat(f64::NEG_INFINITY));
pub const POS_INFINITY: Self = Self(OrderedFloat(f64::INFINITY));
pub const MIN: Self = Self(OrderedFloat(f64::MIN));
pub const MAX: Self = Self(OrderedFloat(f64::MAX));
pub const NAN: Self = Self(OrderedFloat(f64::NAN));
#[inline(always)]
pub fn new(f: f64) -> Self {
Self(OrderedFloat(f))
}
#[inline(always)]
pub fn is_nan(&self) -> bool {
self.0 .0.is_nan()
}
#[inline(always)]
pub fn is_finite(&self) -> bool {
self.0 .0.is_finite()
}
#[inline(always)]
pub fn is_infinite(&self) -> bool {
self.0 .0.is_infinite()
}
#[inline(always)]
pub fn is_positive(&self) -> bool {
self.0 .0.is_sign_positive()
}
#[inline(always)]
pub fn is_negative(&self) -> bool {
self.0 .0.is_sign_negative()
}
#[inline(always)]
pub const fn into_f64(self) -> f64 {
self.0 .0
}
}
const XSD_CANONICAL_DOUBLE: pretty_dtoa::FmtFloatConfig = pretty_dtoa::FmtFloatConfig::default()
.force_e_notation()
.capitalize_e(true);
impl fmt::Display for Double {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
pretty_dtoa::dtoa(self.0 .0, XSD_CANONICAL_DOUBLE).fmt(f)
}
}
impl XsdDatatype for Double {
fn type_(&self) -> Datatype {
Datatype::Double
}
}
impl<'a> From<&'a lexical::Double> for Double {
fn from(value: &'a lexical::Double) -> Self {
Self::new(value.into())
}
}
impl From<lexical::DoubleBuf> for Double {
fn from(value: lexical::DoubleBuf) -> Self {
Self::new(value.into())
}
}
impl FromStr for Double {
type Err = lexical::InvalidDouble;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = lexical::Double::new(s)?;
Ok(l.into())
}
}
impl From<f32> for Double {
fn from(value: f32) -> Self {
Self(OrderedFloat(value as f64))
}
}
impl From<f64> for Double {
fn from(value: f64) -> Self {
Self(OrderedFloat(value))
}
}
impl From<Double> for f64 {
fn from(value: Double) -> Self {
value.0 .0
}
}
impl AsRef<f64> for Double {
fn as_ref(&self) -> &f64 {
&self.0
}
}
impl Borrow<f64> for Double {
fn borrow(&self) -> &f64 {
&self.0
}
}
impl Deref for Double {
type Target = f64;
fn deref(&self) -> &f64 {
&self.0
}
}
impl DerefMut for Double {
fn deref_mut(&mut self) -> &mut f64 {
&mut self.0
}
}
impl Add for Double {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(OrderedFloat(*self.0 + *rhs.0))
}
}
impl Sub for Double {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(OrderedFloat(*self.0 - *rhs.0))
}
}
impl Mul for Double {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(OrderedFloat(*self.0 * *rhs.0))
}
}
impl Div for Double {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(OrderedFloat(*self.0 / *rhs.0))
}
}