| 1 | module big |
| 2 | |
| 3 | struct JS.BigInt {} |
| 4 | |
| 5 | #const jsNumber = Number; |
| 6 | |
| 7 | pub struct Number { |
| 8 | } |
| 9 | |
| 10 | pub fn new() Number { |
| 11 | return Number{} |
| 12 | } |
| 13 | |
| 14 | pub fn from_int(i int) Number { |
| 15 | n := Number{} |
| 16 | #n.value = BigInt(+i) |
| 17 | |
| 18 | return n |
| 19 | } |
| 20 | |
| 21 | pub fn from_u64(u u64) Number { |
| 22 | n := Number{} |
| 23 | #n.value = BigInt(u.val) |
| 24 | |
| 25 | return n |
| 26 | } |
| 27 | |
| 28 | pub fn from_hex_string(input string) Number { |
| 29 | n := Number{} |
| 30 | #n.value = BigInt(input.val) |
| 31 | |
| 32 | return n |
| 33 | } |
| 34 | |
| 35 | pub fn from_string(input string) Number { |
| 36 | n := Number{} |
| 37 | #n.value = BigInt(input.val) |
| 38 | |
| 39 | return n |
| 40 | } |
| 41 | |
| 42 | pub fn (n &Number) int() int { |
| 43 | r := 0 |
| 44 | #r.val = jsNumber(n.val.value) |
| 45 | |
| 46 | return r |
| 47 | } |
| 48 | |
| 49 | pub fn (n &Number) str() string { |
| 50 | s := '' |
| 51 | #s.str = n.val.value + "" |
| 52 | |
| 53 | return s |
| 54 | } |
| 55 | |
| 56 | pub fn (a &Number) + (b &Number) Number { |
| 57 | c := Number{} |
| 58 | #c.value = a.val.value + b.val.value |
| 59 | |
| 60 | return c |
| 61 | } |
| 62 | |
| 63 | pub fn (a &Number) - (b &Number) Number { |
| 64 | c := Number{} |
| 65 | #c.value = a.val.value - b.val.value |
| 66 | |
| 67 | return c |
| 68 | } |
| 69 | |
| 70 | pub fn (a &Number) / (b &Number) Number { |
| 71 | c := Number{} |
| 72 | #c.value = a.val.value / b.val.value |
| 73 | |
| 74 | return c |
| 75 | } |
| 76 | |
| 77 | pub fn (a &Number) * (b &Number) Number { |
| 78 | c := Number{} |
| 79 | #c.value = a.val.value * b.val.value |
| 80 | |
| 81 | return c |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | pub fn (a &Number) % (b &Number) Number { |
| 86 | c := Number{} |
| 87 | # c.value = a.val.value % b.val.value |
| 88 | return c |
| 89 | }*/ |
| 90 | |
| 91 | pub fn div_mod(a &Number, b &Number) (Number, Number) { |
| 92 | c := Number{} |
| 93 | d := Number{} |
| 94 | #c.value = a.val.value / b.val.value |
| 95 | #d.value = a.val.value % b.val.value |
| 96 | |
| 97 | return c, d |
| 98 | } |
| 99 | |
| 100 | pub fn cmp(a &Number, b &Number) int { |
| 101 | res := 0 |
| 102 | |
| 103 | #if (a.val.value < b.val.value) res.val = -1 |
| 104 | #else if (a.val.value > b.val.value) res.val = 1 |
| 105 | #else res.val = 0 |
| 106 | |
| 107 | return res |
| 108 | } |
| 109 | |
| 110 | pub fn (a &Number) is_zero() bool { |
| 111 | res := false |
| 112 | #res.val = a.val.value == BigInt(0) |
| 113 | |
| 114 | return res |
| 115 | } |
| 116 | |
| 117 | pub fn (mut a Number) inc() { |
| 118 | #a.val.value = a.val.value + BigInt(1) |
| 119 | } |
| 120 | |
| 121 | pub fn (mut a Number) dec() { |
| 122 | #a.val.value = a.val.value - BigInt(1) |
| 123 | } |
| 124 | |
| 125 | pub fn (a &Number) isqrt() Number { |
| 126 | b := Number{} |
| 127 | #let x0 = a.val.value >> 1n |
| 128 | #if (x0) { |
| 129 | #let x1 = (x0 + a.val.value / x0) >> 1n |
| 130 | #while (x1 < x0) { |
| 131 | #x0 = x1 |
| 132 | #x1 = (x0 + a.val.value / x0) >> 1n |
| 133 | #} |
| 134 | #b.value = x0 |
| 135 | #} else { b.value = a.val.value; } |
| 136 | |
| 137 | return b |
| 138 | } |
| 139 | |
| 140 | pub fn bitwise_and(a &Number, b &Number) Number { |
| 141 | c := Number{} |
| 142 | #c.value = a.val.value & b.val.value |
| 143 | |
| 144 | return c |
| 145 | } |
| 146 | |
| 147 | pub fn bitwise_or(a &Number, b &Number) Number { |
| 148 | c := Number{} |
| 149 | #c.value = a.val.value | b.val.value |
| 150 | |
| 151 | return c |
| 152 | } |
| 153 | |
| 154 | pub fn bitwise_xor(a &Number, b &Number) Number { |
| 155 | c := Number{} |
| 156 | #c.value = a.val.value ^ b.val.value |
| 157 | |
| 158 | return c |
| 159 | } |
| 160 | |
| 161 | pub fn (a &Number) left_shift(amount int) Number { |
| 162 | c := Number{} |
| 163 | #c.value = a.val.value << BigInt(+amount) |
| 164 | |
| 165 | return c |
| 166 | } |
| 167 | |
| 168 | pub fn (a &Number) right_shift(amount int) Number { |
| 169 | c := Number{} |
| 170 | #c.value = a.val.value << BigInt(+amount) |
| 171 | |
| 172 | return c |
| 173 | } |
| 174 | |
| 175 | pub fn (a &Number) clone() Number { |
| 176 | b := Number{} |
| 177 | #b.value = a.val.value |
| 178 | |
| 179 | return b |
| 180 | } |
| 181 | |
| 182 | pub fn factorial(nn &Number) Number { |
| 183 | mut n := nn.clone() |
| 184 | mut a := nn.clone() |
| 185 | n.dec() |
| 186 | mut i := 1 |
| 187 | for !n.is_zero() { |
| 188 | res := a * n |
| 189 | n.dec() |
| 190 | a = res |
| 191 | i++ |
| 192 | } |
| 193 | return a |
| 194 | } |
| 195 | |
| 196 | pub fn factorial_int(n int) Number { |
| 197 | return factorial(from_int(n)) |
| 198 | } |
| 199 | |