Adding minor changes to functions

This commit is contained in:
chabisik 2022-03-26 21:50:36 +01:00
parent 9767e0039d
commit fb83c71de6

View file

@ -61,7 +61,7 @@ def overlap(arg1: str, arg2: str) -> str:
return overlap_result return overlap_result
def unoverlap(arg: str) -> Sequence[str]: 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)
@ -91,12 +91,11 @@ def unoverlap(arg: str) -> Sequence[str]:
for index in range(len(arg_decimal_part)): for index in range(len(arg_decimal_part)):
if index%2==0: arg1 += arg_decimal_part[index] if index%2==0: arg1 += arg_decimal_part[index]
else: arg2 += arg_decimal_part[index] else: arg2 += arg_decimal_part[index]
arg1 = good_format(arg1) if apply_format: arg1 = good_format(arg1); arg2 = good_format(arg2)
arg2 = good_format(arg2)
return arg1, arg2 return arg1, arg2
def sb_addition(arg1: str, arg2: str) -> str: def sb_addition(arg1: str, arg2: str, apply_format: bool=True) -> str:
result = '' result = ''
# the addition will be a substraction if (arg1 is negative and arg2 is positive) or (arg1 is positive and arg2 is negative) # the addition will be a substraction if (arg1 is negative and arg2 is positive) or (arg1 is positive and arg2 is negative)
if str(arg1).startswith('-') and not(str(arg2).startswith('-')): # arg1 is negative and arg2 is positive if str(arg1).startswith('-') and not(str(arg2).startswith('-')): # arg1 is negative and arg2 is positive
@ -152,11 +151,11 @@ def sb_addition(arg1: str, arg2: str) -> str:
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
result = good_format(result) if apply_format: result = good_format(result)
return result return result
def sb_substract(arg1: str, arg2: str) -> str: def sb_substract(arg1: str, arg2: str, apply_format: bool=True) -> str:
result = '' # will be arg1-arg2 result = '' # will be arg1-arg2
# the substraction will be an addition if (arg1 is negative and arg2 is positive) or (arg1 is positive and arg2 is negative) # the substraction will be an addition if (arg1 is negative and arg2 is positive) or (arg1 is positive and arg2 is negative)
if str(arg1).startswith('-') and not(str(arg2).startswith('-')): # arg1 is negative and arg2 is positive if str(arg1).startswith('-') and not(str(arg2).startswith('-')): # arg1 is negative and arg2 is positive
@ -238,11 +237,11 @@ def sb_substract(arg1: str, arg2: str) -> str:
rem = int( eval( '1 if ' + arg1_integer_part[i] + '<(' + arg2_integer_part[i] + '+' + str(rem) + ') else 0' ) ) rem = int( eval( '1 if ' + arg1_integer_part[i] + '<(' + arg2_integer_part[i] + '+' + str(rem) + ') else 0' ) )
if str(rem) != '0': result = str(rem) + result if str(rem) != '0': result = str(rem) + result
if result_needs_negative_sign_in_font: result = '-' + result if result_needs_negative_sign_in_font: result = '-' + result
result = good_format(result) if apply_format: result = good_format(result)
return result return result
def sb_multiply(arg1: str, arg2: str) -> str: def sb_multiply(arg1: str, arg2: str, apply_format: bool=True) -> str:
# transform arg1 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero) # transform arg1 to string if not already in string and test if it's a full integer (without decimal part or decimal part equals zero)
arg1_as_string = str(arg1) arg1_as_string = str(arg1)
arg1_integer_part = arg1_as_string arg1_integer_part = arg1_as_string
@ -300,10 +299,15 @@ def sb_multiply(arg1: str, arg2: str) -> str:
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])
if not(arg1_is_full_integer) or not(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):]
elif arg1_is_full_integer and not(arg2_is_full_integer):
result = result[:-len(arg2_decimal_part)]+'.'+result[len(result)-len(arg2_decimal_part):]
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
result = good_format(result) print("result inside function", result)
if apply_format: result = good_format(result)
return result return result
def good_format(result: str) -> str: def good_format(result: str) -> str:
@ -315,8 +319,14 @@ def good_format(result: str) -> str:
return result return result
def sb_division(arg1: str, arg2: str) -> str: def sb_division(arg1: str, arg2: str) -> str:
pass arg1_as_string = str(arg1)
arg2_as_string = str(arg2)
while '.' in arg2_as_string:
arg1_as_string = good_format( sb_multiply(arg1_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)
#when encoding, verify overlap_result being even as
if __name__=='__main__': if __name__=='__main__':
#print("add", sb_addition("2222222222222222222222222222222222222222.55", "2.55") ) #print("add", sb_addition("2222222222222222222222222222222222222222.55", "2.55") )
@ -326,14 +336,16 @@ if __name__=='__main__':
#print(greater_than("0","1110", "0","1112", True, True)) #print(greater_than("0","1110", "0","1112", True, True))
#print(greater_than("0","0", "1110","1112")) #print(greater_than("0","0", "1110","1112"))
#print(greater_than('9001','9003')) #print(greater_than('9001','9003'))
print("mul", sb_multiply('123','123.0')) #print("mul", sb_multiply('123','123.0'))
#======================= #=======================
#print("sb_addition('14','19977.67')", sb_addition('14','19977.67')) #print("sb_addition('14','19977.67')", sb_addition('14','19977.67'))
#print("sb_substract('559.1','6.98')", sb_substract('559.1','6.98')) #print("sb_substract('559.1','6.98')", sb_substract('559.1','6.98'))
#print("sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','-6.98') )",sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','-6.98') )) #print("sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','-6.98') )",sb_multiply( sb_addition('14','19977.67') , sb_substract('559.1','-6.98') ))
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('-00001.000')) #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'))
#sb_division('1251452.236523','125.32564')
pass pass