-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfnum.h
executable file
·117 lines (91 loc) · 2.44 KB
/
infnum.h
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
#pragma once
#include<cstdint>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
namespace infnum {
typedef uint64_t u64;
typedef int64_t i64;
class infnum {
private:
void removeLeadingZeros();
infnum add(infnum other) const;
infnum subtract(infnum other) const;
infnum longShiftLeft(int count) const;
infnum longShiftRight(int count) const;
public:
std::vector<u64> data = {0, 0}; //64 bits binary expansion
bool sign = 0;
template<typename T>
infnum(T t){
if(std::is_integral_v<T>) {
if(t < 0) {
sign = 1;
t *= -1;
}
std::memcpy(&data[1], &t, sizeof(t));
}
else if(std::is_floating_point_v<T>) {
int exp;
double val = std::frexp(t, &exp);
for(int i = 63; i >= 0; --i) {
val *= 2;
if(val >= 1) {
data[0] += (1ULL << i);
val -= 1;
}
}
*this <<= exp;
}
}
infnum() {}
infnum(std::initializer_list<u64> v) {data = v;}
infnum(std::string str);
infnum(const char* str);
template<typename T>
void operator=(T t) { *this = infnum(t); }
bool operator==(infnum other) const;
bool operator!=(infnum other) const;
bool operator>(infnum other) const;
bool operator>=(infnum other) const;
bool operator<(infnum other) const;
bool operator<=(infnum other) const;
infnum operator+(infnum other) const;
infnum operator-(infnum other) const;
infnum operator*(infnum other) const;
infnum operator/(infnum other) const;
infnum operator%(infnum other) const;
infnum operator^(infnum other) const;
infnum operator-();
void operator+=(infnum other);
void operator-=(infnum other);
void operator*=(infnum other);
void operator/=(infnum other);
void operator%=(infnum other);
void operator^=(infnum other);
infnum operator>>(int count) const;
infnum operator<<(int count) const;
void operator>>=(int count);
void operator<<=(int count);
std::size_t size() const;
u64& operator[](std::size_t index);
u64 operator[](std::size_t index) const;
};
infnum round(infnum x);
infnum floor(infnum x);
infnum ceil(infnum x);
infnum abs(infnum x);
bool isInteger(infnum x);
infnum min(infnum x, infnum y);
infnum min(std::vector<infnum> v);
infnum max(infnum x, infnum y);
infnum max(std::vector<infnum> v);
infnum exp(infnum x);
infnum ln(infnum x);
i64 mostSignificantBit(infnum x);
const infnum PI({2611923443488327889ULL, 3ULL});
const infnum E({13249961062380153449ULL, 2ULL});
}
std::ostream& operator<<(std::ostream& o, const infnum::infnum& n);