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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
use num::bigint::Sign;
use num::{BigInt, FromPrimitive, Integer, Signed, ToPrimitive, Zero};
use std::fmt::{Display, Formatter};
fn int_to_float(i: &BigInt) -> f64 {
match i.sign() {
Sign::Minus => return -int_to_float(&-i),
Sign::NoSign => return 0.,
_ => (),
}
let mut exponent = i.bits() - 1;
if exponent > 1023 {
return f64::INFINITY;
}
let mut fraction;
if exponent <= 52 {
// The number can be represented exactly without rounding.
fraction = i.to_u64().unwrap() << (52 - exponent);
} else {
// We need to round.
let power = BigInt::from(1) << (exponent - 52);
let (rounded_fraction, rem) = i.div_rem(&power);
fraction = rounded_fraction.to_u64().unwrap();
let half = power >> 1;
// Round up?
if rem > half || rem == half && fraction % 2 == 1 {
fraction += 1;
// Check for overflow.
if fraction == 1 << 53 {
exponent += 1;
fraction >>= 1;
if exponent > 1023 {
return f64::INFINITY;
}
}
}
}
// Build the float from exponent and fraction
// https://en.wikipedia.org/wiki/IEEE_754
f64::from_bits(((exponent + 1023) << 52) + (fraction & !(1 << 52)))
}
#[derive(Debug, PartialEq)]
pub enum Num {
Int(BigInt),
Float(f64),
}
fn int(i: BigInt) -> Num {
if i.bits() > 1 << 30 {
return Num::Float(int_to_float(&i));
}
Num::Int(i)
}
fn float(f: f64) -> Num {
Num::Float(f)
}
impl Num {
fn as_float(&self) -> f64 {
match self {
Num::Int(i) => int_to_float(i),
&Num::Float(f) => f,
}
}
fn square(self) -> Num {
match self {
Num::Int(i) => int(i.pow(2)),
Num::Float(f) => float(f * f),
}
}
fn powi(self, p: BigInt) -> Num {
let mut n = self;
let mut p = p;
let mut acc = int(BigInt::from(1));
loop {
if p.is_odd() {
acc = acc.mul(&n);
}
p >>= 1;
if p.is_zero() {
return acc;
}
n = n.square();
}
}
pub fn pow(self, other: Num) -> Num {
match other {
Num::Int(i) => {
if !i.is_negative() {
return self.powi(i);
}
if let Ok(i_32) = i32::try_from(&i) {
return float(self.as_float().powi(i_32));
}
float(1.).div(self).powi(-i)
}
Num::Float(f) => float(self.as_float().powf(f)),
}
}
pub fn mul(self, other: &Num) -> Num {
match (self, other) {
(Num::Int(i1), Num::Int(i2)) => int(i1 * i2),
(n1, n2) => float(n1.as_float() * n2.as_float()),
}
}
pub fn div(self, other: Num) -> Num {
float(self.as_float() / other.as_float())
}
pub fn int_div(self, other: Num) -> Num {
match (self, other) {
(Num::Int(i1), Num::Int(i2)) if !i2.is_zero() => int(i1.div_floor(&i2)),
(n1, n2) => {
let f = (n1.as_float() / n2.as_float()).floor();
let Some(i) = BigInt::from_f64(f) else {
return float(f);
};
int(i)
}
}
}
pub fn modulo(self, other: Num) -> Num {
match (self, other) {
(Num::Int(i1), Num::Int(i2)) if !i2.is_zero() => int(i1.mod_floor(&i2)),
(n1, n2) => float(n1.as_float() % n2.as_float()),
}
}
pub fn add(self, other: Num) -> Num {
match (self, other) {
(Num::Int(i1), Num::Int(i2)) => int(i1 + i2),
(n1, n2) => float(n1.as_float() + n2.as_float()),
}
}
pub fn sub(self, other: Num) -> Num {
match (self, other) {
(Num::Int(i1), Num::Int(i2)) => int(i1 - i2),
(n1, n2) => float(n1.as_float() - n2.as_float()),
}
}
pub fn sin(self) -> Num {
float(self.as_float().sin())
}
pub fn cos(self) -> Num {
float(self.as_float().cos())
}
pub fn tan(self) -> Num {
float(self.as_float().tan())
}
pub fn asin(self) -> Num {
float(self.as_float().asin())
}
pub fn acos(self) -> Num {
float(self.as_float().acos())
}
pub fn atan(self) -> Num {
float(self.as_float().atan())
}
pub fn sqrt(self) -> Num {
match self {
Num::Int(i) if !i.is_negative() => {
let s = i.sqrt();
if &s * &s == i {
return int(s);
}
float(int_to_float(&i).sqrt())
}
n => float(n.as_float().sqrt()),
}
}
pub fn log(self) -> Num {
float(self.as_float().ln())
}
pub fn log10(self) -> Num {
float(self.as_float().log10())
}
pub fn log2(self) -> Num {
float(self.as_float().log2())
}
pub fn floor(self) -> Num {
match self {
Num::Int(i) => int(i),
Num::Float(f) => {
let f = f.floor();
if let Some(i) = BigInt::from_f64(f) {
int(i)
} else {
float(f)
}
}
}
}
pub fn ceil(self) -> Num {
match self {
Num::Int(i) => int(i),
Num::Float(f) => {
let f = f.ceil();
if let Some(i) = BigInt::from_f64(f) {
int(i)
} else {
float(f)
}
}
}
}
pub fn round(self) -> Num {
match self {
Num::Int(i) => int(i),
Num::Float(f) => {
let f = f.round_ties_even();
if let Some(i) = BigInt::from_f64(f) {
int(i)
} else {
float(f)
}
}
}
}
pub fn abs(self) -> Num {
match self {
Num::Int(i) => int(i.abs()),
Num::Float(f) => float(f.abs()),
}
}
}
impl Display for Num {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Num::Int(n) if n.bits() <= 100 => write!(f, "{n}"),
n => {
let n = n.as_float();
if (-(1i128 << 100) as f64) < n && n < (1i128 << 100) as f64 {
return write!(f, "{}", format!("{n:.7}").trim_end_matches('0'));
}
write!(f, "{n:.7e}")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn int(i: i128) -> Num {
super::int(BigInt::from(i))
}
#[test]
fn int_to_float_small() {
for n in -(1 << 16)..1 << 16 {
assert_eq!(
int_to_float(&BigInt::from(n)),
n as f64,
"int_to_float({}) is not equal to {}",
n,
n as f64
);
}
}
#[test]
fn int_to_float_medium() {
let n = BigInt::from(2).pow(1023) + BigInt::from(2).pow(1022);
assert_eq!(int_to_float(&n), n.to_f64().unwrap());
}
#[test]
fn int_to_float_round_up() {
assert_eq!(
int_to_float(&BigInt::from(0x7f_ffff_ffff_ffffu64)),
0x80_0000_0000_0000u64 as f64
);
}
#[test]
fn int_to_float_round_up_easy() {
assert_eq!(
int_to_float(&BigInt::from(0x40_0000_0000_0003u64)),
0x40_0000_0000_0004u64 as f64
);
}
#[test]
fn int_to_float_round_up_overflow() {
assert_eq!(
int_to_float(&BigInt::from(0x1ff_ffff_ffff_fff9u64)),
0x200_0000_0000_0000u64 as f64
);
}
#[test]
fn int_to_float_round_down() {
assert_eq!(
int_to_float(&BigInt::from(0x40_0000_0000_0001u64)),
0x40_0000_0000_0000u64 as f64
);
}
#[test]
fn int_to_float_round_to_even_down() {
assert_eq!(
int_to_float(&BigInt::from(0x40_0000_0000_0002u64)),
0x40_0000_0000_0000u64 as f64
);
}
#[test]
fn int_to_float_round_to_even_up() {
assert_eq!(
int_to_float(&BigInt::from(0x40_0000_0000_0006u64)),
0x40_0000_0000_0008u64 as f64
);
}
#[test]
fn int_to_float_large() {
assert_eq!(
int_to_float(
&((BigInt::from(0x1f_ffff_ffff_ffffu64) << 971) + (BigInt::from(1) << 970) - 1)
),
f64::MAX
);
}
#[test]
fn int_to_float_overflow() {
assert_eq!(
int_to_float(
&((BigInt::from(0x1f_ffff_ffff_ffffu64) << 971) + (BigInt::from(1) << 970))
),
f64::INFINITY
);
}
#[test]
fn pow_positive_int() {
assert_eq!(int(2).pow(int(16)), int(65536));
}
#[test]
fn pow_negative_int() {
assert_eq!(int(2).pow(int(-3)), float(0.125));
}
#[test]
fn pow_float() {
assert_eq!(int(4).pow(float(0.5)), float(2.));
}
#[test]
fn pow_overflow() {
assert_eq!(int(2).pow(int(1 << 126)), float(f64::INFINITY));
}
#[test]
fn pow_underflow() {
assert_eq!(int(2).pow(int(-(1 << 126))), float(0.));
}
#[test]
fn modulo_pos() {
assert_eq!(int(5).modulo(int(3)), int(2));
}
#[test]
fn modulo_pos_neg() {
assert_eq!(int(5).modulo(int(-3)), int(-1));
}
#[test]
fn modulo_neg_pos() {
assert_eq!(int(-5).modulo(int(3)), int(1));
}
#[test]
fn modulo_neg() {
assert_eq!(int(-5).modulo(int(-3)), int(-2));
}
#[test]
fn modulo_float() {
assert_eq!(float(5.).modulo(int(3)), float(2.));
}
#[test]
fn sqrt() {
for n in 0..65536 {
assert_eq!(
int(n * n).sqrt(),
int(n),
"int({}).sqrt() is not equal to {}",
n * n,
n
);
}
}
#[test]
fn sqrt_big() {
assert_eq!(int(1 << 126).sqrt(), int(1 << 63));
}
}
|