aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/math/big/big.js.v
blob: 0ed469ca8bd6a324d58c7384443cecdd4c1f8b05 (plain)
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
module big

struct JS.BigInt {}

#const jsNumber = Number;

pub struct Number {
}

pub fn new() Number {
	return Number{}
}

pub fn from_int(i int) Number {
	n := Number{}
	#n.value = BigInt(+i)

	return n
}

pub fn from_u64(u u64) Number {
	n := Number{}
	#n.value = BigInt(u.val)

	return n
}

pub fn from_hex_string(input string) Number {
	n := Number{}
	#n.value = BigInt(input.val)

	return n
}

pub fn from_string(input string) Number {
	n := Number{}
	#n.value = BigInt(input.val)

	return n
}

pub fn (n &Number) int() int {
	r := 0
	#r.val = jsNumber(n.val.value)

	return r
}

pub fn (n &Number) str() string {
	s := ''
	#s.str = n.val.value + ""

	return s
}

pub fn (a &Number) + (b &Number) Number {
	c := Number{}
	#c.value = a.val.value + b.val.value

	return c
}

pub fn (a &Number) - (b &Number) Number {
	c := Number{}
	#c.value = a.val.value - b.val.value

	return c
}

pub fn (a &Number) / (b &Number) Number {
	c := Number{}
	#c.value = a.val.value / b.val.value

	return c
}

pub fn (a &Number) * (b &Number) Number {
	c := Number{}
	#c.value = a.val.value * b.val.value

	return c
}

/*
pub fn (a &Number) % (b &Number) Number {
	c := Number{}
	# c.value = a.val.value % b.val.value
	return c
}*/

pub fn divmod(a &Number, b &Number) (Number, Number) {
	c := Number{}
	d := Number{}
	#c.value = a.val.value / b.val.value
	#d.value = a.val.value % b.val.value

	return c, d
}

pub fn cmp(a &Number, b &Number) int {
	res := 0

	#if (a.val.value < b.val.value) res.val = -1
	#else if (a.val.value > b.val.value) res.val = 1
	#else res.val = 0

	return res
}

pub fn (a &Number) is_zero() bool {
	res := false
	#res.val = a.val.value == BigInt(0)

	return res
}

pub fn (mut a Number) inc() {
	#a.val.value = a.val.value + BigInt(1)
}

pub fn (mut a Number) dec() {
	#a.val.value = a.val.value - BigInt(1)
}

pub fn (a &Number) isqrt() Number {
	b := Number{}
	#let x0 = a.val.value >> 1n
	#if (x0) {
	#let x1 = (x0 + a.val.value / x0) >> 1n
	#while (x1 < x0) {
	#x0 = x1
	#x1 = (x0 + a.val.value / x0) >> 1n
	#}
	#b.value = x0
	#} else { b.value = a.val.value; }

	return b
}

pub fn b_and(a &Number, b &Number) Number {
	c := Number{}
	#c.value = a.val.value & b.val.value

	return c
}

pub fn b_or(a &Number, b &Number) Number {
	c := Number{}
	#c.value = a.val.value | b.val.value

	return c
}

pub fn b_xor(a &Number, b &Number) Number {
	c := Number{}
	#c.value = a.val.value ^ b.val.value

	return c
}

pub fn (a &Number) lshift(nbits int) Number {
	c := Number{}
	#c.value = a.val.value << BigInt(+nbits)

	return c
}

pub fn (a &Number) rshift(nbits int) Number {
	c := Number{}
	#c.value = a.val.value << BigInt(+nbits)

	return c
}

pub fn (a &Number) clone() Number {
	b := Number{}
	#b.value = a.val.value

	return b
}

pub fn factorial(nn &Number) Number {
	mut n := nn.clone()
	mut a := nn.clone()
	n.dec()
	mut i := 1
	for !n.is_zero() {
		res := a * n
		n.dec()
		a = res
		i++
	}
	return a
}

pub fn fact(n int) Number {
	return factorial(from_int(n))
}