Adding division function
This commit is contained in:
parent
fb83c71de6
commit
88775ec09f
1 changed files with 60 additions and 11 deletions
|
@ -65,6 +65,7 @@ def unoverlap(arg: str, apply_format: bool=True) -> Sequence[str]:
|
||||||
arg1 = '' #for stocking unoverlapped values
|
arg1 = '' #for stocking unoverlapped values
|
||||||
arg2 = ''
|
arg2 = ''
|
||||||
arg_as_string = str(arg)
|
arg_as_string = str(arg)
|
||||||
|
# MUST TRY TO PAD THE STRING HERE IN CASE good_format were applied on it during operations
|
||||||
arg_is_full_integer = True
|
arg_is_full_integer = True
|
||||||
try:
|
try:
|
||||||
arg_integer_part, arg_decimal_part = arg_as_string.split(".")
|
arg_integer_part, arg_decimal_part = arg_as_string.split(".")
|
||||||
|
@ -143,12 +144,12 @@ def sb_addition(arg1: str, arg2: str, apply_format: bool=True) -> str:
|
||||||
for i in range(max_decimal_part_size-1, -1, -1):
|
for i in range(max_decimal_part_size-1, -1, -1):
|
||||||
res = int( eval('(' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ')%10') )
|
res = int( eval('(' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ')%10') )
|
||||||
result = str(res) + result
|
result = str(res) + result
|
||||||
rem = int( eval('((' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
|
rem = int( eval('((' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ') - ' + str(res) + ')//10') )
|
||||||
result = '.' + result
|
result = '.' + result
|
||||||
for i in range(max_integer_part_size-1, -1, -1):
|
for i in range(max_integer_part_size-1, -1, -1):
|
||||||
res = int( eval('(' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ')%10') )
|
res = int( eval('(' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ')%10') )
|
||||||
result = str(res) + result
|
result = str(res) + result
|
||||||
rem = int( eval('((' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
|
rem = int( eval('((' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ') - ' + str(res) + ')//10') )
|
||||||
if str(rem) != '0': result = str(rem) + result
|
if str(rem) != '0': result = str(rem) + result
|
||||||
if arg1_is_negative and arg2_is_negative: result = '-' + result # treat the case both numbers are negative
|
if arg1_is_negative and arg2_is_negative: result = '-' + result # treat the case both numbers are negative
|
||||||
if apply_format: result = good_format(result)
|
if apply_format: result = good_format(result)
|
||||||
|
@ -293,12 +294,12 @@ def sb_multiply(arg1: str, arg2: str, apply_format: bool=True) -> str:
|
||||||
for i in range(len(up_number_in_multiplication)-1, -1, -1):
|
for i in range(len(up_number_in_multiplication)-1, -1, -1):
|
||||||
res = int( eval( '((' + down_number_in_multiplication[j] + '*' + up_number_in_multiplication[i] + ')+' + str(rem) + ')%10' ) )
|
res = int( eval( '((' + down_number_in_multiplication[j] + '*' + up_number_in_multiplication[i] + ')+' + str(rem) + ')%10' ) )
|
||||||
current_intermediate_number = str(res) + current_intermediate_number
|
current_intermediate_number = str(res) + current_intermediate_number
|
||||||
rem = int( eval( '(((' + down_number_in_multiplication[j] + '*' + up_number_in_multiplication[i] + ')+' + str(rem) + ') - ' + str(res) + ')/10') )
|
rem = int( eval( '(((' + down_number_in_multiplication[j] + '*' + up_number_in_multiplication[i] + ')+' + str(rem) + ') - ' + str(res) + ')//10') )
|
||||||
if rem != '0': current_intermediate_number = str(rem) + current_intermediate_number
|
if rem != '0': current_intermediate_number = str(rem) + current_intermediate_number
|
||||||
intermediate_numbers.append(current_intermediate_number)
|
intermediate_numbers.append(current_intermediate_number)
|
||||||
result = intermediate_numbers[0]
|
result = intermediate_numbers[0]
|
||||||
for k in range(1, len(intermediate_numbers)):
|
for k in range(1, len(intermediate_numbers)):
|
||||||
result = sb_addition(result, intermediate_numbers[k])
|
result = sb_addition(result, intermediate_numbers[k], apply_format=False)
|
||||||
if not(arg1_is_full_integer) and arg2_is_full_integer:
|
if not(arg1_is_full_integer) and arg2_is_full_integer:
|
||||||
result = result[:-len(arg1_decimal_part)]+'.'+result[len(result)-len(arg1_decimal_part):]
|
result = result[:-len(arg1_decimal_part)]+'.'+result[len(result)-len(arg1_decimal_part):]
|
||||||
elif arg1_is_full_integer and not(arg2_is_full_integer):
|
elif arg1_is_full_integer and not(arg2_is_full_integer):
|
||||||
|
@ -306,7 +307,6 @@ def sb_multiply(arg1: str, arg2: str, apply_format: bool=True) -> str:
|
||||||
elif not(arg1_is_full_integer) and not(arg2_is_full_integer): #was or
|
elif not(arg1_is_full_integer) and not(arg2_is_full_integer): #was or
|
||||||
result = result[:-len(arg1_decimal_part+arg2_decimal_part)]+'.'+result[len(result)-len(arg1_decimal_part+arg2_decimal_part):] #here to modifiy
|
result = result[:-len(arg1_decimal_part+arg2_decimal_part)]+'.'+result[len(result)-len(arg1_decimal_part+arg2_decimal_part):] #here to modifiy
|
||||||
if (arg1_is_negative and not(arg2_is_negative)) or (not(arg1_is_negative) and arg2_is_negative): result = '-' + result
|
if (arg1_is_negative and not(arg2_is_negative)) or (not(arg1_is_negative) and arg2_is_negative): result = '-' + result
|
||||||
print("result inside function", result)
|
|
||||||
if apply_format: result = good_format(result)
|
if apply_format: result = good_format(result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -318,13 +318,61 @@ def good_format(result: str) -> str:
|
||||||
while '.' in result and result.endswith('0'): result = result[:-1]
|
while '.' in result and result.endswith('0'): result = result[:-1]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def sb_division(arg1: str, arg2: str) -> str:
|
|
||||||
|
def is_well_written(arg: str) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def sb_division(arg1: str, arg2: str, digits_after_decimal_point: int=5, apply_format: bool=True) -> str:
|
||||||
|
assert good_format(arg2)!='0'
|
||||||
|
result = ''
|
||||||
|
#---------
|
||||||
arg1_as_string = str(arg1)
|
arg1_as_string = str(arg1)
|
||||||
arg2_as_string = str(arg2)
|
arg2_as_string = str(arg2)
|
||||||
|
#---------
|
||||||
|
arg1_is_negative = False
|
||||||
|
if arg1_as_string.startswith('-'):
|
||||||
|
arg1_is_negative = True
|
||||||
|
arg1_as_string = arg1_as_string.replace('-','')
|
||||||
|
#---------
|
||||||
|
arg2_is_negative = False
|
||||||
|
if arg2_as_string.startswith('-'):
|
||||||
|
arg2_is_negative = True
|
||||||
|
arg2_as_string = arg2_as_string.replace('-','')
|
||||||
|
#---------
|
||||||
while '.' in arg2_as_string:
|
while '.' in arg2_as_string:
|
||||||
arg1_as_string = good_format( sb_multiply(arg1_as_string,'10') )
|
arg1_as_string = good_format( sb_multiply(arg1_as_string,'10') )
|
||||||
arg2_as_string = good_format( sb_multiply(arg2_as_string,'10') )
|
arg2_as_string = good_format( sb_multiply(arg2_as_string,'10') )
|
||||||
print("arg1_as_string", arg1_as_string, "arg2_as_string", arg2_as_string)
|
#---------
|
||||||
|
arg1_integer_part = arg1_as_string
|
||||||
|
arg1_decimal_part = ''
|
||||||
|
arg1_is_full_integer = True
|
||||||
|
try:
|
||||||
|
arg1_integer_part, arg1_decimal_part = arg1_as_string.split(".")
|
||||||
|
if arg1_decimal_part==len(arg1_decimal_part)*'0': arg1_is_full_integer = True
|
||||||
|
else: arg1_is_full_integer = False
|
||||||
|
except:
|
||||||
|
pass#nothing to do
|
||||||
|
#=========
|
||||||
|
arg1_as_string = arg1_integer_part+arg1_decimal_part
|
||||||
|
#current_number_to_consider = arg1_as_string[0]
|
||||||
|
current_number_to_consider = '0'
|
||||||
|
#for digit in arg1_as_string[1:]:
|
||||||
|
for digit in arg1_as_string+'0'*(digits_after_decimal_point):
|
||||||
|
if current_number_to_consider=='0':
|
||||||
|
current_number_to_consider = digit
|
||||||
|
else:
|
||||||
|
current_number_to_consider = current_number_to_consider + digit
|
||||||
|
#---------
|
||||||
|
if eval( current_number_to_consider + '<' + arg2_as_string ):
|
||||||
|
result = result + '0'
|
||||||
|
else:
|
||||||
|
result = result + str( eval(current_number_to_consider+'//'+arg2_as_string) )
|
||||||
|
current_number_to_consider = str( eval(current_number_to_consider+'-(('+current_number_to_consider+'//'+arg2_as_string+')*'+arg2_as_string+')') )
|
||||||
|
result = result[0:len(arg1_integer_part)]+'.'+result[len(arg1_integer_part):]
|
||||||
|
if (arg1_is_negative and not(arg2_is_negative)) or (not(arg1_is_negative) and arg2_is_negative): result = '-' + result
|
||||||
|
if apply_format: result = good_format(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
#when encoding, verify overlap_result being even as
|
#when encoding, verify overlap_result being even as
|
||||||
|
|
||||||
|
@ -344,8 +392,9 @@ if __name__=='__main__':
|
||||||
#print("cmplx:", sb_multiply( sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','6.98') ),'0.1459'))
|
#print("cmplx:", sb_multiply( sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','6.98') ),'0.1459'))
|
||||||
#print("format", good_format('10.0'))
|
#print("format", good_format('10.0'))
|
||||||
#print("overlap",overlap('-12512.252','125'))
|
#print("overlap",overlap('-12512.252','125'))
|
||||||
ov_r = overlap('-12512.252','-125')
|
#ov_r = overlap('-12512.252','-125')
|
||||||
print(unoverlap(ov_r))
|
#print(unoverlap(ov_r))
|
||||||
print("mult", sb_multiply('-125.32564','100000.0'))
|
#print("mult", sb_multiply('-125.32564','100000.0'))
|
||||||
#sb_division('1251452.236523','125.32564')
|
print(sb_division('0.0001','-12.25', digits_after_decimal_point=15))
|
||||||
|
print(sb_multiply('-10.00000','-0.0001'))
|
||||||
pass
|
pass
|
Loading…
Reference in a new issue