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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use std::ops;
use std::cmp;
use std::str::FromStr;
use serde::{Serialize, Deserialize, Serializer};
const CONVERSION_CONST: i64 = 10000;
type InnerType = i64;
#[derive(Clone, Copy)]
pub struct YololNumber(InnerType);
impl YololNumber
{
pub fn new(inner: InnerType) -> YololNumber
{
YololNumber(inner)
}
pub fn from_split(main: InnerType, decimal: InnerType) -> YololNumber
{
YololNumber(YololNumber::to_inner(main) + decimal)
}
fn to_inner(num: InnerType) -> InnerType
{
num.saturating_mul(CONVERSION_CONST)
}
fn from_inner(num: InnerType) -> InnerType
{
num / CONVERSION_CONST
}
pub fn is_negative(self) -> bool
{
self.0.is_negative()
}
pub fn floor(self) -> YololNumber
{
YololNumber::from(self.0 / CONVERSION_CONST)
}
pub fn ceiling(self) -> YololNumber
{
let first_decimal = self.0 % CONVERSION_CONST;
let adjustment = CONVERSION_CONST.saturating_sub(first_decimal);
YololNumber::new(self.0.saturating_add(adjustment))
}
pub fn clamp(self, min: InnerType, max: InnerType) -> YololNumber
{
if self.0 < min.saturating_mul(CONVERSION_CONST)
{
YololNumber::from(min)
}
else if self.0 > max.saturating_mul(CONVERSION_CONST)
{
YololNumber::from(max)
}
else
{
self
}
}
pub fn pow(self, other: Self) -> Self
{
let float_self = (self.0 as f64) / (CONVERSION_CONST as f64);
let float_other = (other.0 as f64) / (CONVERSION_CONST as f64);
let pow = float_self.powf(float_other);
let int_pow = if pow.abs() > (std::i64::MAX as f64)
{
std::i64::MAX.saturating_mul(pow.signum() as i64)
}
else
{
pow as i64
};
let new_inner = int_pow.saturating_mul(CONVERSION_CONST);
YololNumber::new(new_inner)
}
pub fn abs(self) -> YololNumber
{
YololNumber::new(self.0.saturating_abs())
}
pub fn sqrt(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let output = float_value.sqrt();
YololNumber::from(output as i64)
}
pub fn sin(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.to_radians();
YololNumber::from(rads.sin() as i64)
}
pub fn cos(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.to_radians();
YololNumber::from(rads.cos() as i64)
}
pub fn tan(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.to_radians();
YololNumber::from(rads.tan() as i64)
}
pub fn arcsin(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.asin();
YololNumber::from(rads.to_degrees() as i64)
}
pub fn arccos(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.acos();
YololNumber::from(rads.to_degrees() as i64)
}
pub fn arctan(self) -> YololNumber
{
let float_value = (self.0 as f64) / (CONVERSION_CONST as f64);
let rads = float_value.atan();
YololNumber::from(rads.to_degrees() as i64)
}
}
impl From<InnerType> for YololNumber
{
fn from(input: InnerType) -> YololNumber
{
let num = YololNumber::to_inner(input);
YololNumber(num)
}
}
impl From<&InnerType> for YololNumber
{
fn from(input: &InnerType) -> YololNumber
{
let num = YololNumber::to_inner(*input);
YololNumber(num)
}
}
impl From<YololNumber> for InnerType
{
fn from(input: YololNumber) -> InnerType
{
YololNumber::from_inner(input.0)
}
}
impl From<&YololNumber> for InnerType
{
fn from(input: &YololNumber) -> InnerType
{
YololNumber::from_inner(input.0)
}
}
impl FromStr for YololNumber
{
type Err = String;
fn from_str(string: &str) -> Result<Self, Self::Err>
{
let (left_string, right_string) = if string.contains('.')
{
let split: Vec<&str> = string.split('.').collect();
if split.len() != 2
{
return Err(format!("[YololNumber::from_str] Input string had {} decimal points!", split.len()));
}
(split[0], split[1])
}
else
{
(string, "")
};
if !left_string.chars().all(|c| c.is_ascii_digit())
{
return Err("[YololNumber::from_str] Chars to left of decimal point aren't all numbers!".to_owned())
}
if !right_string.is_empty() && !right_string.chars().all(|c| c.is_ascii_digit())
{
return Err("[YololNumber::from_str] Chars to right of decimal point aren't all numbers!".to_owned())
}
let parse_error_handler = |error: std::num::ParseIntError| {
use std::num::IntErrorKind;
match error.kind()
{
IntErrorKind::Empty |
IntErrorKind::Zero => 0,
IntErrorKind::Overflow => std::i64::MAX,
IntErrorKind::Underflow => std::i64::MIN,
IntErrorKind::InvalidDigit => panic!("[YololNumber::from_str] String to i64 parse error: somehow encountered a letter in the characters collected for a yolol number!"),
_ => panic!("[YololNumber::from_str] Unknown String to i64 parse error when converting yolol number!")
}
};
let left_num: i64 = left_string.parse::<i64>().unwrap_or_else(parse_error_handler);
let right_num: i64 = match right_string.len()
{
0 => 0,
len @ 1..=3 => {
let shift: i64 = (10i64).pow(4 - (len as u32));
let num = right_string[0..len].parse::<i64>().unwrap_or_else(parse_error_handler);
num * shift
},
_ => {
match right_string[0..4].parse::<i64>()
{
Ok(num) => num,
Err(_) => return Err("[YololNumber::from_str] Failure to parse 4 decimals into number!".to_owned())
}
}
};
let yolol_num = YololNumber::from_split(left_num, right_num);
Ok(yolol_num)
}
}
impl std::fmt::Display for YololNumber
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result
{
let main_digits: InnerType = self.into();
let sign = self.0.signum();
let ones = (self.0 % 10) * sign;
let tens = ((self.0/10) % 10) * sign;
let hundreds = ((self.0/100) % 10) * sign;
let thousands = ((self.0/1000) % 10) * sign;
let format = if ones != 0
{
format!("{}.{}{}{}{}", main_digits, thousands, hundreds, tens, ones)
}
else if tens != 0
{
format!("{}.{}{}{}", main_digits, thousands, hundreds, tens)
}
else if hundreds != 0
{
format!("{}.{}{}", main_digits, thousands, hundreds)
}
else if thousands != 0
{
format!("{}.{}", main_digits, thousands)
}
else
{
format!("{}", main_digits)
};
write!(f, "{}", format)
}
}
impl std::fmt::Debug for YololNumber
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(f, "{}", self)
}
}
impl Serialize for YololNumber
{
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
{
serializer.serialize_str(&self.to_string())
}
}
use serde::{Deserializer, de::Visitor};
struct YololNumberVisitor;
impl<'de> Visitor<'de> for YololNumberVisitor
{
type Value = YololNumber;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(f, "a string containing only numerical characters, possibly with a decimal point")
}
fn visit_str<E>(self, input: &str) -> Result<Self::Value, E>
where E: serde::de::Error
{
match input.parse::<YololNumber>()
{
Ok(num) => Ok(num),
Err(error) => Err(E::custom(error))
}
}
}
impl<'de> Deserialize<'de> for YololNumber
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
deserializer.deserialize_str(YololNumberVisitor)
}
}
impl cmp::PartialEq for YololNumber
{
fn eq(&self, other: &Self) -> bool
{
self.0 == other.0
}
}
impl cmp::Eq for YololNumber {}
impl cmp::Ord for YololNumber
{
fn cmp(&self, other: &YololNumber) -> cmp::Ordering
{
self.0.cmp(&other.0)
}
}
impl cmp::PartialOrd for YololNumber
{
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering>
{
Some(self.cmp(other))
}
}
impl ops::Add for YololNumber
{
type Output = Self;
fn add(self, other: Self) -> Self
{
YololNumber::new(self.0.saturating_add(other.0))
}
}
impl ops::Sub for YololNumber
{
type Output = Self;
fn sub(self, other: Self) -> Self
{
YololNumber::new(self.0.saturating_sub(other.0))
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl ops::Mul for YololNumber
{
type Output = Self;
fn mul(self, other: Self) -> Self
{
let output_sign = self.0.signum() * other.0.signum();
let output = match self.0.checked_mul(other.0)
{
Some(num) => num / CONVERSION_CONST,
None => if output_sign == -1 { std::i64::MIN } else { std::i64::MAX }
};
YololNumber::new(output)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl ops::Div for YololNumber
{
type Output = Self;
fn div(self, other: Self) -> Self
{
let output = (self.0 * CONVERSION_CONST).checked_div(other.0).unwrap_or(0);
YololNumber::new(output)
}
}
impl ops::Rem for YololNumber
{
type Output = Self;
fn rem(self, other: Self) -> Self
{
YololNumber::new(self.0.checked_rem(other.0).unwrap_or(0))
}
}
impl std::ops::Add<String> for YololNumber
{
type Output = String;
fn add(self, other: String) -> String
{
self.to_string() + other.as_str()
}
}
impl std::ops::Add<YololNumber> for String
{
type Output = String;
fn add(self, other: YololNumber) -> String
{
self + other.to_string().as_str()
}
}
impl std::ops::Neg for YololNumber
{
type Output = YololNumber;
fn neg(self) -> YololNumber
{
YololNumber::new(self.0.saturating_neg())
}
}
impl std::ops::Not for YololNumber
{
type Output = YololNumber;
fn not(self) -> YololNumber
{
if self.0 == 0
{
YololNumber::new(1 * CONVERSION_CONST)
}
else
{
YololNumber::new(0)
}
}
}