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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
//! From C++20 (7.3.10, p. 93): "A prvalue of a floating-point type can be
//! converted to a prvalue of an integer type. The conversion truncates; that
//! is, the fractional part is discarded. The behavior is undefined if the
//! truncated value cannot be represented in the destination type."
//!
//! <https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions>
//! Casting from a float to an integer will round the float towards zero.
//! - `NaN` will return `0`
//! - Values larger than the maximum integer value, including `INFINITY`, will
//! saturate to the maximum value of the integer type.
//! - Values smaller than the minimum integer value, including `NEG_INFINITY`,
//! will saturate to the minimum value of the integer type.
use crate::assign::Assign;
use crate::{Arbi, Digit};
#[allow(clippy::unnecessary_cast)]
const BASE_DBL: f64 = 2.0 * ((1 as Digit) << (Digit::BITS - 1)) as f64;
const BASE_DBL_RECIPROCAL: f64 = 1.0 / BASE_DBL;
impl Assign<f64> for Arbi {
/// Assign a floating-point value to an `Arbi`.
///
/// ## Panic
/// Panics when attempting to convert a `NaN` or infinity.
///
/// ## Note
/// In Rust, when casting a primitive float to a primitive integer type,
/// `NaN`s are converted to `0` and values with large magnitudes and
/// infinities are saturated to the maximum and minimum values of the
/// integer type.
///
/// In contrast, this function panics in these scenarios.
fn assign(&mut self, d: f64) {
let mut d = d;
if d.is_nan() || d.is_infinite() {
panic!(
"Conversion error: NaN or infinity cannot be converted to an \
integer."
);
}
if d > -1.0 && d < 1.0 {
self.assign(0);
return;
}
let neg: bool = d < 0.0;
if neg {
d = -d;
}
let mut n_digits: usize = 1;
while BASE_DBL <= d {
// Equiv. to `d /= BASE_DBL;`, but multiplication is generally
// cheaper.
d *= BASE_DBL_RECIPROCAL;
n_digits += 1;
}
self.vec.resize(n_digits, 0);
for i in (0..n_digits).rev() {
self.vec[i] = d as Digit;
d = (d - self.vec[i] as f64) * BASE_DBL;
}
self.neg = neg;
self.trim();
}
}
impl Assign<&f64> for Arbi {
/// Assign a floating-point value to an `Arbi`.
///
/// ## Panic
/// Panics when attempting to convert a `NaN` or infinity.
///
/// ## Note
/// In Rust, when casting a primitive float to a primitive integer type,
/// `NaN`s are converted to `0` and values with large magnitudes and
/// infinities are saturated to the maximum and minimum values of the
/// integer type.
///
/// In contrast, this function panics in these scenarios.
fn assign(&mut self, d: &f64) {
self.assign(*d)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::test::BASE10;
fn test_doub<T>(value: f64, exp: T)
where
T: PartialEq + core::fmt::Debug,
Arbi: PartialEq<T>,
{
// Test construct from
assert_eq!(Arbi::from(value), exp, "{:?} != {:?}", value, exp);
// Test assign from
let mut x = Arbi::new();
x.assign(value);
assert_eq!(x, exp, "{:?} != {:?}", value, exp);
}
#[test]
fn test_f64_zero() {
test_doub(0.0, 0.0);
test_doub(-0.0, 0.0);
}
#[test]
fn test_f64_min_positive() {
test_doub(f64::MIN_POSITIVE, 0.0);
}
#[test]
fn test_f64_min() {
test_doub(f64::MIN, f64::MIN);
}
#[test]
fn test_f64_max() {
test_doub(f64::MAX, f64::MAX);
}
#[test]
#[should_panic]
fn test_f64_nan() {
test_doub(f64::NAN, 0.0);
}
#[test]
#[should_panic]
fn test_f64_infinity() {
test_doub(f64::INFINITY, 0.0);
}
#[test]
#[should_panic]
fn test_f64_neg_infinity() {
test_doub(f64::NEG_INFINITY, 0.0);
}
#[test]
fn test_f64_subnormal() {
use crate::util::test::float::SUBNORMAL_DOUBLE;
test_doub(SUBNORMAL_DOUBLE, 0.0);
}
#[test]
fn test_f64_max_int() {
test_doub(
9007199254740992.0,
Arbi::from_str_base("9007199254740992", BASE10).unwrap(),
);
}
#[test]
fn test_f64_max_int_neg() {
test_doub(
-9007199254740992.0,
Arbi::from_str_base("-9007199254740992", BASE10).unwrap(),
);
}
#[test]
fn test_misc() {
test_doub(9876.54321, 9876);
test_doub(-9876.54321, -9876);
test_doub(0.987654321, 0);
test_doub(0.999999999, 0);
test_doub(-0.999999999, 0);
test_doub(1e-109, 0);
test_doub(
1e109,
Arbi::from_str_base(
"9999999999999999818508707188399807864717650964328171247958398\
369899072554380053298205803424393137676263358464",
BASE10,
)
.unwrap(),
);
}
#[test]
fn smoke() {
use crate::util::test::float::{MAX_INT, MAX_INT_NEG};
use crate::util::test::{
get_seedable_rng, get_uniform_die, Distribution,
};
let (mut rng, _) = get_seedable_rng();
let die_0 = get_uniform_die(MAX_INT_NEG, MAX_INT);
let die_1 = get_uniform_die(-100.0, 100.0);
for _ in 0..i16::MAX {
let mut random_double: f64;
let mut int_val: i64;
random_double = die_0.sample(&mut rng);
int_val = random_double as i64;
test_doub(random_double, int_val as f64);
random_double = die_1.sample(&mut rng);
int_val = random_double as i64;
test_doub(random_double, int_val as f64);
}
}
}