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::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use handle::Handle;
use integral::guide::Guide;
pub struct FloatPoint_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<FloatPoint_>() }
pub fn is_prism(prism: AnchoredLine) -> bool { prism[0] == prism_unit() }
pub fn find_prism(h: Handle) -> Option<AnchoredLine> { h.find_prism(prism_unit()) }
pub fn is_float(h: Handle) -> bool { find_prism(h).is_some() }
pub fn new(x: f64) -> Unit {
let guide = blank();
store(guide.root, x);
guide.store().segment().unit()
}
pub fn blank() -> Guide {
let needed = 1 + Guide::units() + if cfg!(target_pointer_width = "32") { 2 } else { 1 };
let s = Segment::new(needed);
let prism = s.line_at(0);
prism.set(0, prism_unit());
let guide = Guide::new(prism);
guide
}
pub fn parse(negate: bool, whole: &[u8], part: &[u8], promote: bool) -> Handle {
use std::str::from_utf8;
let b = format!("{}.{}", from_utf8(whole).unwrap(), from_utf8(part).unwrap());
if promote { unimplemented!() }
let mut x = b.parse::<f64>().unwrap();
if negate { x = -x; }
let guide = {
let g = blank();
if promote { g.set_big() } else { g }
};
store(guide.root, x);
guide.store().segment().unit().handle()
}
pub fn parse_exp(negate: bool, whole: &[u8], part: &[u8],
exp_negate: bool, exp: &[u8], promote: bool) -> Handle {
use std::str::from_utf8;
let b = format!("{}.{}e{}{}", from_utf8(whole).unwrap(), from_utf8(part).unwrap(),
if exp_negate { "-" } else { "" }, from_utf8(exp).unwrap());
if promote { unimplemented!() }
let mut x = b.parse::<f64>().unwrap();
if negate { x = -x; }
let guide = {
let g = blank();
if promote { g.set_big() } else { g }
};
store(guide.root, x);
guide.store().segment().unit().handle()
}
pub fn inf() -> Handle {
use std::f64::INFINITY;
new(INFINITY).handle()
}
pub fn neg_inf() -> Handle {
use std::f64::NEG_INFINITY;
new(NEG_INFINITY).handle()
}
pub fn not_a_number() -> Handle {
use std::f64::NAN;
new(NAN).handle()
}
pub fn store(line: AnchoredLine, x: f64) {
use memory::unit::f64_into_u64;
let x = f64_into_u64(x);
if cfg!(target_pointer_width = "32") {
line.set(0, Unit::from(x as u32));
line.set(1, Unit::from((x >> 32) as u32));
} else {
line.set(0, Unit::from(x));
}
}
pub fn hydrate(line: AnchoredLine) -> f64 {
let x = if cfg!(target_pointer_width = "32") {
let low: u32 = line[0].into();
let hi: u32 = line[1].into();
((hi as u64) << 32) | (low as u64)
} else {
line[0].u64()
};
use memory::unit::f64_from_u64;
f64_from_u64(x)
}
impl Dispatch for FloatPoint_ { }
impl Identification for FloatPoint_ {
fn type_name(&self) -> &'static str { "FloatPoint" }
}
impl Distinguish for FloatPoint_ {
fn hash(&self, prism: AnchoredLine) -> u32 {
let guide = Guide::hydrate(prism);
if guide.has_hash() { return guide.hash; }
let h = {
use hash::hash_64;
let x = hydrate(guide.root);
use memory::unit::f64_into_u64;
hash_64(f64_into_u64(x), 8)
};
guide.set_hash(h).store_hash().hash
}
fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
let o = other.handle();
if let Some(o_float) = find_prism(o) {
let guide = Guide::hydrate(prism);
let guide2 = Guide::hydrate(o_float);
let x = hydrate(guide.root);
let y = hydrate(guide2.root);
return x == y
}
false
}
fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
let o = other.handle();
if let Some(o_float) = find_prism(o) {
let guide = Guide::hydrate(prism);
let guide2 = Guide::hydrate(o_float);
let x = hydrate(guide.root);
let y = hydrate(guide2.root);
return x.partial_cmp(&y)
}
if o.is_ref() {
let o_prism_unit = o.logical_value()[0];
Some(prism_unit().cmp(&o_prism_unit))
} else {
Some(Ordering::Greater)
}
}
}
impl Aggregate for FloatPoint_ { }
impl Sequential for FloatPoint_ { }
impl Associative for FloatPoint_ { }
impl Reversible for FloatPoint_ {}
impl Sorted for FloatPoint_ {}
impl Notation for FloatPoint_ {
fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
let guide = Guide::hydrate(prism);
let x = hydrate(guide.root);
if x.is_finite() {
if x.floor() == x {
write!(f, "{}.0", x)
} else {
write!(f, "{}", x)
}
} else if x.is_nan() {
write!(f, "##NaN")
} else if x.is_sign_positive() {
write!(f, "##Inf")
} else {
write!(f, "##-Inf")
}
}
}
impl Numeral for FloatPoint_ { }
impl Callable for FloatPoint_ { }
#[cfg(test)]
mod tests {
use super::*;
}