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
use std::{
	borrow::Borrow,
	fmt,
	ops::{Add, Div, Mul, Sub},
	str::FromStr,
};

use ordered_float::OrderedFloat;

use crate::{lexical, Datatype, XsdDatatype};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Float(OrderedFloat<f32>);

impl Float {
	pub const NEG_INFINITY: Self = Self(OrderedFloat(f32::NEG_INFINITY));
	pub const INFINITY: Self = Self(OrderedFloat(f32::INFINITY));
	pub const MIN: Self = Self(OrderedFloat(f32::MIN));
	pub const MAX: Self = Self(OrderedFloat(f32::MAX));
	pub const NAN: Self = Self(OrderedFloat(f32::NAN));

	#[inline(always)]
	pub fn new(f: f32) -> Self {
		Self(OrderedFloat(f))
	}

	/// Returns `true` if this value is NaN.
	#[inline(always)]
	pub fn is_nan(&self) -> bool {
		self.0 .0.is_nan()
	}

	/// Returns `true` if this number is neither infinite nor NaN.
	#[inline(always)]
	pub fn is_finite(&self) -> bool {
		self.0 .0.is_finite()
	}

	/// Returns `true` if this value is positive infinity or negative infinity, and `false` otherwise.
	#[inline(always)]
	pub fn is_infinite(&self) -> bool {
		self.0 .0.is_infinite()
	}

	/// Returns `true` if `self` has a positive sign, including +0.0, NaNs with
	/// positive sign bit and positive infinity.
	///
	/// Note that IEEE 754 doesn't assign any meaning to the sign bit in case
	/// of a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs
	/// are conserved over arithmetic operations, the result of
	/// `is_positive` on a NaN might produce an unexpected result in some
	/// cases.
	/// See [explanation of NaN as a special value](https://doc.rust-lang.org/nightly/core/primitive.f32.html)
	/// for more info.
	#[inline(always)]
	pub fn is_positive(&self) -> bool {
		self.0 .0.is_sign_positive()
	}

	/// Returns `false` if `self` has a negative sign, including -0.0, NaNs with
	/// negative sign bit and negative infinity.
	///
	/// Note that IEEE 754 doesn't assign any meaning to the sign bit in case
	/// of a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs
	/// are conserved over arithmetic operations, the result of
	/// `is_negative` on a NaN might produce an unexpected result in some
	/// cases.
	/// See [explanation of NaN as a special value](https://doc.rust-lang.org/nightly/core/primitive.f32.html)
	/// for more info.
	#[inline(always)]
	pub fn is_negative(&self) -> bool {
		self.0 .0.is_sign_negative()
	}

	/// Converts this value into a `f32`.
	#[inline(always)]
	pub const fn into_f32(self) -> f32 {
		self.0 .0
	}
}

// <https://www.w3.org/TR/xmlschema11-2/#f-doubleLexmap>
const XSD_CANONICAL_FLOAT: pretty_dtoa::FmtFloatConfig = pretty_dtoa::FmtFloatConfig::default()
	.force_e_notation()
	.capitalize_e(true);

impl fmt::Display for Float {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		pretty_dtoa::ftoa(self.0 .0, XSD_CANONICAL_FLOAT).fmt(f)
	}
}

impl XsdDatatype for Float {
	#[inline(always)]
	fn type_(&self) -> Datatype {
		Datatype::Float
	}
}

impl<'a> From<&'a lexical::Float> for Float {
	fn from(value: &'a lexical::Float) -> Self {
		Self::new(value.into())
	}
}

impl From<lexical::FloatBuf> for Float {
	fn from(value: lexical::FloatBuf) -> Self {
		Self::new(value.into())
	}
}

impl FromStr for Float {
	type Err = lexical::InvalidFloat;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let l = lexical::Float::new(s)?;
		Ok(l.into())
	}
}

impl From<f32> for Float {
	#[inline(always)]
	fn from(value: f32) -> Self {
		Self(OrderedFloat(value))
	}
}

impl From<Float> for f32 {
	#[inline(always)]
	fn from(value: Float) -> Self {
		value.0 .0
	}
}

impl From<Float> for f64 {
	#[inline(always)]
	fn from(value: Float) -> Self {
		value.0 .0 as f64
	}
}

impl AsRef<f32> for Float {
	#[inline(always)]
	fn as_ref(&self) -> &f32 {
		&self.0
	}
}

impl Borrow<f32> for Float {
	#[inline(always)]
	fn borrow(&self) -> &f32 {
		&self.0
	}
}

impl Add for Float {
	type Output = Self;

	#[inline(always)]
	fn add(self, rhs: Self) -> Self::Output {
		Self(OrderedFloat(*self.0 + *rhs.0))
	}
}

impl Sub for Float {
	type Output = Self;

	#[inline(always)]
	fn sub(self, rhs: Self) -> Self::Output {
		Self(OrderedFloat(*self.0 - *rhs.0))
	}
}

impl Mul for Float {
	type Output = Self;

	#[inline(always)]
	fn mul(self, rhs: Self) -> Self::Output {
		Self(OrderedFloat(*self.0 * *rhs.0))
	}
}

impl Div for Float {
	type Output = Self;

	#[inline(always)]
	fn div(self, rhs: Self) -> Self::Output {
		Self(OrderedFloat(*self.0 / *rhs.0))
	}
}