Adding good addition and substraction functions
This commit is contained in:
parent
f84cdbc2b3
commit
130525117f
1 changed files with 91 additions and 53 deletions
|
@ -6,6 +6,11 @@ import time
|
||||||
|
|
||||||
def sb_addition(arg1: str, arg2: str) -> str: #will do both addition and substraction
|
def sb_addition(arg1: str, arg2: str) -> str: #will do both addition and substraction
|
||||||
result = ''
|
result = ''
|
||||||
|
# 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
|
||||||
|
return sb_substract(str(arg2), str(arg1).replace('-',''))
|
||||||
|
elif not(str(arg1).startswith('-')) and str(arg2).startswith('-'): # arg1 is positive and arg2 is negative
|
||||||
|
return sb_substract(str(arg1), str(arg2).replace('-',''))
|
||||||
# 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
|
||||||
|
@ -36,41 +41,40 @@ def sb_addition(arg1: str, arg2: str) -> str: #will do both addition and substra
|
||||||
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
|
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
|
||||||
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
|
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
|
||||||
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
|
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
|
||||||
|
#---------
|
||||||
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
|
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
|
||||||
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
|
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
|
||||||
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
|
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
|
||||||
|
|
||||||
print("arg1_integer_part", arg1_integer_part)
|
|
||||||
print("arg1_decimal_part", arg1_decimal_part)
|
|
||||||
print("arg2_integer_part", arg2_integer_part)
|
|
||||||
print("arg2_decimal_part", arg2_decimal_part)
|
|
||||||
# making real addition operation
|
# making real addition operation
|
||||||
|
# when here, it means both numbers are positives OR both numbers are negatives
|
||||||
rem = '0'
|
rem = '0'
|
||||||
if (not(arg1_is_negative) and not(arg2_is_negative)) or (arg1_is_negative and arg2_is_negative): # both numbers are positives OR both numbers are negatives
|
print("ADDITION") #DELETE
|
||||||
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
|
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
|
||||||
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
|
|
||||||
rem = int( eval('((' + arg1_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
|
|
||||||
print("rem", rem)
|
|
||||||
result = '.' + result
|
|
||||||
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') )
|
|
||||||
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_decimal_part[i] + '+' + arg2_decimal_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
|
||||||
print("rem", rem)
|
print("rem", rem)
|
||||||
if str(rem) != '0': result = str(rem) + result
|
result = '.' + result
|
||||||
if arg1_is_negative and arg2_is_negative: result = '-' + result # treat the case both numbers are negative
|
for i in range(max_integer_part_size-1, -1, -1):
|
||||||
elif not(arg1_is_negative) and arg2_is_negative: # first number is positive and second one is negative
|
res = int( eval('(' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ')%10') )
|
||||||
pass
|
result = str(res) + result
|
||||||
elif arg1_is_negative and not(arg2_is_negative): # first number is negative and second one is positive
|
rem = int( eval('((' + arg1_integer_part[i] + '+' + arg2_integer_part[i] + '+' + str(rem) + ') - ' + str(res) + ')/10') )
|
||||||
pass
|
print("rem", rem)
|
||||||
print("result now:", 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
|
||||||
|
while result.startswith('-0'): result = result.replace('-0','-')
|
||||||
|
if '.' in result and result.split('.')[1]==len(result.split('.')[1])*'0': return result.split('.')[0] # no need to return the result decimal part if it's equal to zero
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def sb_substract(arg1: str, arg2: str, are_already_same_size: bool=False) -> str:
|
|
||||||
|
def sb_substract(arg1: str, arg2: str) -> 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)
|
||||||
|
if str(arg1).startswith('-') and not(str(arg2).startswith('-')): # arg1 is negative and arg2 is positive
|
||||||
|
return sb_addition(str(arg1), '-'+str(arg2))
|
||||||
|
elif not(str(arg1).startswith('-')) and str(arg2).startswith('-'): # arg1 is positive and arg2 is negative
|
||||||
|
return sb_addition(str(arg1), str(arg2).replace('-',''))
|
||||||
# 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
|
||||||
|
@ -97,34 +101,45 @@ def sb_substract(arg1: str, arg2: str, are_already_same_size: bool=False) -> str
|
||||||
except:
|
except:
|
||||||
pass#nothing to do
|
pass#nothing to do
|
||||||
if arg2_is_negative: arg2_integer_part = arg2_integer_part.replace('-', '')
|
if arg2_is_negative: arg2_integer_part = arg2_integer_part.replace('-', '')
|
||||||
|
# making integer and decimal parts same size
|
||||||
if not(are_already_same_size):
|
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
|
||||||
# making integer and decimal parts same size
|
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
|
||||||
max_integer_part_size = max( len(arg1_integer_part), len(arg2_integer_part) )
|
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
|
||||||
arg1_integer_part = '0'*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
|
#---------
|
||||||
arg2_integer_part = '0'*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
|
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
|
||||||
|
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
|
||||||
max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
|
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
|
||||||
arg1_decimal_part = arg1_decimal_part + '0'*(max_decimal_part_size-len(arg1_decimal_part))
|
|
||||||
arg2_decimal_part = arg2_decimal_part + '0'*(max_decimal_part_size-len(arg2_decimal_part))
|
|
||||||
# making real substraction operation
|
# making real substraction operation
|
||||||
if arg1_is_negative and not(arg2_is_negative): # first number is negative and second one is positive
|
# when here, it means both numbers are positives OR both numbers are negatives
|
||||||
return sb_addition('-'+arg1_integer_part+'.'+arg1_decimal_part, '-'+arg2_integer_part+'.'+arg2_decimal_part) # second number will become negative when doing arg1-arg2
|
print("SUBSTRACT") #DELETE
|
||||||
if not(arg1_is_negative) and arg2_is_negative: # first number is positive and second one is negative
|
result_needs_negative_sign_in_font = False
|
||||||
return sb_addition(arg1_integer_part+'.'+arg1_decimal_part, arg2_integer_part+'.'+arg2_decimal_part) # second number will become positive when doing arg1-arg2
|
|
||||||
# when not in one of the above cases, substraction operation can be considered as not being a simple addition
|
|
||||||
if arg1_is_negative and arg2_is_negative: # both numbers are negatives
|
|
||||||
# then (-arg1)-(-arg2) becomes arg2-arg1
|
|
||||||
temp = arg1_integer_part
|
|
||||||
arg1_integer_part = arg2_integer_part
|
|
||||||
arg2_integer_part = temp
|
|
||||||
# --------------
|
|
||||||
temp = arg1_decimal_part
|
|
||||||
arg1_decimal_part = arg2_decimal_part
|
|
||||||
arg2_decimal_part = temp
|
|
||||||
rem = '0'
|
|
||||||
print("arg1_integer_part", arg1_integer_part, "arg1_decimal_part", arg1_decimal_part)
|
print("arg1_integer_part", arg1_integer_part, "arg1_decimal_part", arg1_decimal_part)
|
||||||
print("arg2_integer_part", arg2_integer_part, "arg2_decimal_part", arg2_decimal_part)
|
print("arg2_integer_part", arg2_integer_part, "arg2_decimal_part", arg2_decimal_part)
|
||||||
|
print("arg1_is_negative", arg1_is_negative, "arg2_is_negative", arg2_is_negative)
|
||||||
|
if arg1_is_negative and arg2_is_negative:
|
||||||
|
if greater_than(arg2_integer_part, arg1_integer_part, arg2_decimal_part, arg1_decimal_part):
|
||||||
|
temp = arg1_integer_part
|
||||||
|
arg1_integer_part = arg2_integer_part
|
||||||
|
arg2_integer_part = temp
|
||||||
|
#---------
|
||||||
|
temp = arg1_decimal_part
|
||||||
|
arg1_decimal_part = arg2_decimal_part
|
||||||
|
arg2_decimal_part = temp
|
||||||
|
else:
|
||||||
|
result_needs_negative_sign_in_font = True
|
||||||
|
elif not(arg1_is_negative) and not(arg2_is_negative):
|
||||||
|
if greater_than(arg1_integer_part, arg2_integer_part, arg1_decimal_part, arg2_decimal_part):
|
||||||
|
pass # nothing to do
|
||||||
|
else:
|
||||||
|
temp = arg1_integer_part
|
||||||
|
arg1_integer_part = arg2_integer_part
|
||||||
|
arg2_integer_part = temp
|
||||||
|
#---------
|
||||||
|
temp = arg1_decimal_part
|
||||||
|
arg1_decimal_part = arg2_decimal_part
|
||||||
|
arg2_decimal_part = temp
|
||||||
|
result_needs_negative_sign_in_font = True
|
||||||
|
rem = '0'
|
||||||
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
|
if not(arg1_is_full_integer) or not(arg2_is_full_integer):
|
||||||
for i in range(max_decimal_part_size-1, -1, -1):
|
for i in range(max_decimal_part_size-1, -1, -1):
|
||||||
upper_digit_to_consider = int( eval( arg1_decimal_part[i] + '+' + '10 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else ' + arg1_decimal_part[i] ) )
|
upper_digit_to_consider = int( eval( arg1_decimal_part[i] + '+' + '10 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else ' + arg1_decimal_part[i] ) )
|
||||||
|
@ -132,10 +147,33 @@ def sb_substract(arg1: str, arg2: str, are_already_same_size: bool=False) -> str
|
||||||
print("res", str(res))
|
print("res", str(res))
|
||||||
result = str(res) + result
|
result = str(res) + result
|
||||||
rem = int( eval( '1 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else 0' ) )
|
rem = int( eval( '1 if ' + arg1_decimal_part[i] + '<(' + arg2_decimal_part[i] + '+' + str(rem) + ') else 0' ) )
|
||||||
print("result now:", result)
|
result = '.' + result
|
||||||
|
for i in range(max_integer_part_size-1, -1, -1):
|
||||||
|
upper_digit_to_consider = int( eval( arg1_integer_part[i] + '+' + '10 if ' + arg1_integer_part[i] + '<(' + arg2_integer_part[i] + '+' + str(rem) + ') else ' + arg1_integer_part[i] ) )
|
||||||
|
res = int( eval( str(upper_digit_to_consider) + '-(' + arg2_integer_part[i] + '+' + str(rem) + ')' ) )
|
||||||
|
result = str(res) + result
|
||||||
|
rem = int( eval( '1 if ' + arg1_integer_part[i] + '<(' + arg2_integer_part[i] + '+' + str(rem) + ') else 0' ) )
|
||||||
|
print("rem", rem)
|
||||||
|
if str(rem) != '0': result = str(rem) + result
|
||||||
|
if result_needs_negative_sign_in_font: result = '-' + result
|
||||||
|
while result.startswith('-0'): result = result.replace('-0','-')
|
||||||
|
if '.' in result and result.split('.')[1]==len(result.split('.')[1])*'0': return result.split('.')[0] # no need to return the result decimal part if it's equal to zero
|
||||||
|
return result
|
||||||
|
|
||||||
|
def greater_than(arg1_integer_part: str, arg2_integer_part: str, arg1_decimal_part: str='', arg2_decimal_part: str='') -> bool: # verify if arg1 >= arg2
|
||||||
|
# we assume integer parts are same size and decimal parts are also same size
|
||||||
|
assert len(arg1_integer_part)==len(arg2_integer_part) and len(arg1_decimal_part)==len(arg2_decimal_part)
|
||||||
|
for i in range(len(arg1_integer_part)):
|
||||||
|
if eval( arg1_integer_part[i] + '<' + arg2_integer_part[i] ): return False
|
||||||
|
for i in range(len(arg1_decimal_part)):
|
||||||
|
if eval( arg1_decimal_part[i] + '<' + arg2_decimal_part[i] ): return False
|
||||||
|
return True
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
#print( type(sb_addition("-9999999.019999", "-99999.99999993")) )
|
#print("add", sb_addition("-10", "-1.99999993") )
|
||||||
#print( type(sb_substract("9999999.019999", "99999.99999993")) )
|
#print( type(sb_substract("9999999.019999", "99999.99999993")) )
|
||||||
print( type(sb_substract("0.1", "0.9")) )
|
print("sub", sb_substract("12", "78") )
|
||||||
|
#print(greater_than("0","1110", "0","1112", True, True))
|
||||||
|
#print(greater_than("0","0", "1110","1112"))
|
||||||
|
#print(greater_than('9001','9003'))
|
||||||
pass
|
pass
|
Loading…
Reference in a new issue