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
|
use std::fmt::{Display, Formatter};
#[derive(Clone, Copy)]
pub enum Num {
Int(i64),
Float(f64),
}
pub fn int(i: i64) -> Num {
Num::Int(i)
}
pub fn float(f: f64) -> Num {
Num::Float(f)
}
impl Num {
fn as_float(self) -> f64 {
match self {
Num::Int(i) => i as f64,
Num::Float(f) => f,
}
}
fn powi(self, p: u64) -> Num {
if p == 0 {
return int(1);
}
if p == 1 {
return self;
}
if p % 2 == 0 {
return self.mul(self).powi(p / 2);
}
self.mul(self).powi(p / 2).mul(self)
}
pub fn pow(self, other: Num) -> Num {
match other {
Num::Int(i) => {
if let Ok(u) = u64::try_from(i) {
return self.powi(u);
}
if let Ok(i_32) = i32::try_from(i) {
return float(self.as_float().powi(i_32));
}
float(1.).div(self).powi(-i as u64)
}
Num::Float(f) => float(self.as_float().powf(f)),
}
}
pub fn mul(self, other: Num) -> Num {
if let (Num::Int(i1), Num::Int(i2)) = (self, other) {
if let Some(p) = i1.checked_mul(i2) {
return int(p);
}
}
float(self.as_float() * other.as_float())
}
pub fn div(self, other: Num) -> Num {
float(self.as_float() / other.as_float())
}
pub fn int_div(self, other: Num) -> Num {
if let (Num::Int(i1), Num::Int(i2)) = (self, other) {
if let Some(q) = i1.checked_div(i2) {
return int(q);
}
}
int((self.as_float() / other.as_float()) as i64)
}
pub fn modulo(self, other: Num) -> Num {
if let (Num::Int(i1), Num::Int(i2)) = (self, other) {
if let Some(r) = i1.checked_rem(i2) {
if i2 > 0 && r < 0 || i2 < 0 && r > 0 {
return int(r + i2);
}
return int(r);
}
}
let n1 = self.as_float();
let n2 = other.as_float();
let r = n1 % n2;
if n2 > 0. && r < 0. || n2 < 0. && r > 0. {
return float(r + n2);
}
float(r)
}
pub fn add(self, other: Num) -> Num {
if let (Num::Int(i1), Num::Int(i2)) = (self, other) {
if let Some(s) = i1.checked_add(i2) {
return int(s);
}
}
float(self.as_float() + other.as_float())
}
pub fn sub(self, other: Num) -> Num {
if let (Num::Int(i1), Num::Int(i2)) = (self, other) {
if let Some(d) = i1.checked_sub(i2) {
return int(d);
}
}
float(self.as_float() - other.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 >= 0 => {
if i <= 1 {
return int(i);
}
let mut x0 = i / 2;
let mut x1 = (x0 + i / x0) / 2;
while x1 < x0 {
x0 = x1;
x1 = (x0 + i / x0) / 2;
}
if x0 * x0 == i {
return int(x0);
}
}
_ => (),
}
float(self.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) => int(f.floor() as i64),
}
}
pub fn ceil(self) -> Num {
match self {
Num::Int(i) => int(i),
Num::Float(f) => int(f.ceil() as i64),
}
}
pub fn round(self) -> Num {
match self {
Num::Int(i) => int(i),
Num::Float(f) => int(f.round_ties_even() as i64),
}
}
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) => write!(f, "{n}"),
Num::Float(n) => {
if !(-1e30..=1e30).contains(n) {
return write!(f, "{n:.7e}");
}
let mut buf = format!("{n:.7}");
while let Some(b'0') = buf.as_bytes().get(buf.len() - 1) {
buf.truncate(buf.len() - 1);
}
write!(f, "{buf}")
}
}
}
}
|