-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhigh_precision_calculations.py
504 lines (293 loc) · 12 KB
/
high_precision_calculations.py
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 28 18:46:12 2019
@author: artmenlope
"""
def num_decims(num):
"""
Calculates the number of decimals.
"""
if ("." in num):
numA, numB = num.split(".")
num_decms = len(numB)
elif ("." not in num):
num_decms = 0
return num_decms
def max_num_decims(num1, num2):
"""
Given two numbers 'num1' and 'num2', returns
the maximum number of decimals found in them.
"""
num_max_decs = 0
if ("." in num1) or ("." in num2):
if ("." in num1) and ("." not in num2):
num1A, num1B = num1.split(".")
num_max_decs = len(num1B)
elif ("." in num2) and ("." not in num1):
num2A, num2B = num2.split(".")
num_max_decs = len(num2B)
elif ("." in num1) and ("." in num2):
num1A, num1B = num1.split(".")
num2A, num2B = num2.split(".")
num_max_decs = max(len(num1B), len(num2B))
return num_max_decs
def num_zeros_init(num):
"""
Counts the number of zeros at the beginning
of the number 'num'.
"""
iszero = True
num_zeros = 0
i = 0
while iszero == True and (i != len(num)-1):
if num[i] == "0":
num_zeros += 1
elif num[i] != "0":
iszero = False
i += 1
return num_zeros
def num_zeros_end(num):
"""
Counts the number of zeros at the end
of the number 'num'.
"""
iszero = True
num_zeros = 0
i = len(num)-1
while (iszero == True) and (i != 0):
if num[i] == "0":
num_zeros += 1
elif num[i] != "0":
iszero = False
i -= 1
return num_zeros
def del_decim_zeros(num):
"""
If the number 'num' has decimals, deletes
the excess of zeros at the end of it.
"""
if "." in num:
num_zeros = num_zeros_end(num)
if num_zeros != 0:
num = num[:-num_zeros]
if num[-1] == ".":
num = num[:-1]
return num
else:
return num
def decim_modif_str(num1, num2):
"""
Explanations:
Makes the step of multiplying by 10 to
eliminate decimals but with 'strings'.
Makes the length of both numbers 'num1' and 'num2' equal
while eliminating their decimal point.
Removes the decimal points and fills in with zeros.
"""
if ("." in num1) or ("." in num2):
if ("." in num1) and ("." not in num2):
num1A, num1B = num1.split(".")
num1 = num1.replace(".","")
num2 += "0"*len(num1B)
elif ("." in num2) and ("." not in num1):
num2A, num2B = num2.split(".")
num2 = num2.replace(".","")
num1 += "0"*len(num2B)
elif ("." in num1) and ("." in num2): # error inside this elif?
num1A, num1B = num1.split(".")
num2A, num2B = num2.split(".")
num1 = num1.replace(".","")
num2 = num2.replace(".","")
num_zeros = abs(len(num1B)-len(num2B))
if len(num1B) < len(num2B):
num1 += "0" * num_zeros
elif len(num1B) > len(num2B):
num2 += "0" * num_zeros
return num1, num2
def make_pairs(txt):
"""
Takes a string ('txt') and separates it into
pairs of characters returning a list with those pairs.
"""
lista = []
string = ""
count = 0
if len(txt)%2 != 0 :
count = 1
for i in range(len(txt)):
string += txt[i]
count += 1
if count == 2:
lista.append(string)
string = ""
count = 0
return lista
def sum_str(num1, num2):
"""
Adds two numbers in 'string' format. (Accepts negative numbers)
"""
# Check the maximum number of decimals.
max_num_decs = max_num_decims(num1, num2)
# We remove the decimal point and fill in with zeros.
num1, num2 = decim_modif_str(num1, num2)
# Do the summation.
summ = str(int(num1) + int(num2))
# Put the decimal point.
if max_num_decs != 0:
result = summ[:-max_num_decs] + "." + summ[-max_num_decs:]
elif max_num_decs == 0:
result = summ
return result
def subt_str(num1, num2):
"""Subtracts two numbers in 'string' format. (Accepts negative numbers)"""
# Check the maximum number of decimals.
max_num_decs = max_num_decims(num1, num2)
# We remove the decimal point and fill in with zeros.
num1, num2 = decim_modif_str(num1, num2)
# Do the subtraction.
subt = str(int(num1) - int(num2))
# Put the decimal point.
if max_num_decs != 0:
result = subt[:-max_num_decs] + "." + subt[-max_num_decs:]
elif max_num_decs == 0:
result = subt
return result
def multiply_str(num1,num2):
"""
Multiply two numbers 'num1' and 'num2' in string
format using the long multiplication algorithm.
"""
max_num_decs = max_num_decims(num1, num2)
#total_num_decs = num_decims(num1) + num_decims(num2)
num1, num2 = decim_modif_str(num1, num2)
# Calculate the steps.
steps = []
for i in range(0,len(num2)):
if (int(num1) * int(num2[::-1][i]) != 0):
steps.append( str(int(num1)*int(num2[::-1][i])) + "0"*i )
else:
steps.append( "0"*len(num1) + "0"*i )
# Add the steps.
sum_steps = "0"
for i in range(0,len(num2)):
sum_steps = sum_str(sum_steps,steps[i])
if len(sum_steps) != len(steps[-1]):
n_zeros = abs(len(sum_steps)-len(steps[-1]))
sum_steps = "0"*n_zeros + sum_steps
# Put the decimal point.
if max_num_decs != 0:
result = sum_steps[:-2*max_num_decs] + "." + sum_steps[-2*max_num_decs:]
elif max_num_decs == 0:
result = sum_steps
result = del_decim_zeros(result)
return result
def divide_str(dividend, divider, decimal_prec=16):
"""
This function uses the long division algorithm to divide two numbers.
max_iter = maximum number of iterations; equivalent to the maximum number of
decimals plus one (dec# = max_iter-1).
The result is in the format "string".
"""
max_iter = decimal_prec + 1
remainder = 1 # != 0
i = 0
# Check for decimals.
dividend, divider = decim_modif_str(dividend, divider)
dividend, divider = int(dividend), int(divider)
while (remainder != 0) and (i < max_iter):
if dividend >= divider: # Dividend greater than the divider.
result = dividend//divider
if dividend%divider == 0: # Exact division.
if i == 0:
quotient = str(result)
remainder = 0
else:
quotient = quotient + str(result)
remainder = 0
else: # Not exact division.
remainder = dividend%divider
if i == 0:
quotient = str(result) + "."
else:
quotient = quotient + str(result)
dividend = remainder
elif dividend < divider: # Dividend less than the divider.
if i == 0:
quotient = "0."
dividend = int(str(dividend)+"0")
result = dividend//divider
if dividend%divider == 0: # Exact division.
quotient = quotient + str(result)
remainder = 0
else: # Not exact division.
remainder = dividend%divider
quotient = quotient + str(result)
dividend = int(remainder)
i += 1
return quotient
def sqrt_str(num, decimal_prec = 16):
"""
Calculation of the square root of a number 'num'
in string format. The decimal precision is set
by "decimal_prec". The result is given in string
format. The result is obtained using a digit-by-digit
calculation algorithm.
"""
#initialization
if "." in num:
numA, numB = num.split(".")
num_bef_rad = len(numA) # Number of digits before the decimal point in the radicand.
if len(numB)%2 != 0: # If there's not an even number of decimals.
numB = numB + "0"
pairsList = make_pairs(numA + numB)
elif "." not in num:
num_bef_rad = len(num) # Number of digits before the decimal point in the radicand.
pairsList = make_pairs(num)
num_bef_res = int((num_bef_rad+1)/2) # Number of digits before the decimal point in the result.
max_iter = num_bef_res + decimal_prec + 1 # Maximum number of iterations.
root = ""
remainder = 1 # != 0
# Calculations.
i = 0 # Iteration number.
while (remainder != 0) and (i < max_iter):
if i == 0:
# Step 1
radicand = int(pairsList[i])
# Step 2 not necessary at first.
# Step 3
root_term = int(radicand**0.5)
# Step 4
root += str(root_term)
remainder = radicand - root_term**2
else:
# Step 1
if i < len(pairsList):
radicand = int(str(remainder) + pairsList[i])
else:
radicand = int(str(remainder) + "00")
# Step 2
stair_aux = multiply_str(root,"2")
# Step 3
root_term = 1
while radicand - int(stair_aux + str(root_term))*root_term >= 0 :
root_term += 1
root_term -= 1
# Step 4
root += str(root_term)
remainder = radicand - int(stair_aux + str(root_term))*root_term
i += 1
# Results and retouching.
root = root[:num_bef_res] + "." + root[num_bef_res:]
if root[-1] == ".":
root = root[:-1]
return root
def power_str(num, powr):
"""
Calculate the power of a number in string format.
Returns num**powr.
'powr' must be a positive integer.
"""
orig_num = num
for i in range(powr-1):
num = multiply_str(num, orig_num)
return num