Just for fun ,today i tried to implement code for decimal division
similar to approximation way
Here is my code:
Code (python):
from collections import defaultdict
import math
decimalDivident: int = 378
decimalDivisor: int = 24
#array/table
table = [0] * 10
#intermediateResult
intermediateResult=0
#tableBound
tableLowerBound=decimalDivisor
tableUpperBound=0
#expression params
factor = 1
counter=1
#division
divisionUpperBound=0
divisionLowerBound=0
interval =0
interation =0
while True:
if interation ==0:
intermediateResult = tableLowerBound * (factor * 10)
table[counter-1] = intermediateResult
if table[counter-1] < decimalDivident:
divisionLowerBound = factor * (10 ** counter)
tableLowerBound = table[counter-1]
tableUpperBound =0
else:
tableUpperBound = table[counter-1]
divisionUpperBound = factor * (10 ** counter)
if tableUpperBound > 0:
if tableUpperBound == decimalDivident:
print(f"Division is {divisionUpperBound}")
elif divisionLowerBound == decimalDivident:
print(f"Division is {divisionLowerBound}")
else:
print(f"{decimalDivident} lies between {tableLowerBound} and {tableUpperBound}")
print(f"Division lies between {round(divisionLowerBound,4)} and {round(divisionUpperBound,4)}")
#first iteration ended
interation =interation +1
# reset counter & factor
counter=1
#break
counter=counter+1
elif interation == 1:
interval = (tableUpperBound -tableLowerBound)/9
intermediateResult = tableLowerBound + interval
table[counter-1] = intermediateResult
if table[counter-1] < decimalDivident:
divisionLowerBound = counter * 10
tableLowerBound = table[counter-1]
tableUpperBound =0
else:
tableUpperBound = table[counter-1]
divisionUpperBound = counter * 10
if tableUpperBound > 0:
# reset counter & factor
counter=1
if tableUpperBound == decimalDivident:
print(f"Division is {divisionUpperBound}")
elif divisionLowerBound == decimalDivident:
print(f"Division is {divisionLowerBound}")
else:
print(f"{decimalDivident} lies between {tableLowerBound} and {tableUpperBound}")
print(f"Division lies between {round(divisionLowerBound,4)} and {round(divisionUpperBound,4)}")
#iteration incremented
interation =interation +1
else:
factor = factor +1
counter=counter+1
elif interation == 2:
interval = decimalDivisor
intermediateResult = tableLowerBound + interval
table[counter-1] = intermediateResult
if table[counter-1] < decimalDivident:
divisionLowerBound = 10 + counter
tableLowerBound = table[counter-1]
tableUpperBound =0
else:
tableUpperBound = table[counter-1]
divisionUpperBound = 10 + counter
if tableUpperBound > 0:
if tableUpperBound == decimalDivident:
print(f"Division is {divisionUpperBound}")
elif divisionLowerBound == decimalDivident:
print(f"Division is {divisionLowerBound}")
else:
print(f"{decimalDivident} lies between {tableLowerBound} and {tableUpperBound}")
print(f"Division lies between {round(divisionLowerBound,4)} and {round(divisionUpperBound,4)}")
#iteration incremented
interation =interation +1
# reset counter & factor
interval = decimalDivisor/10 #2.4
divisionInterval = (divisionUpperBound-divisionLowerBound)/10 # 0.1
counter=1
#break
else:
factor = factor +1
counter=counter+1
elif interation == 3:#360.384 | 15,16
intermediateResult = tableLowerBound + interval
table[counter-1] = intermediateResult
if table[counter-1] < decimalDivident:
divisionLowerBound = divisionLowerBound + divisionInterval
tableLowerBound = table[counter-1]
tableUpperBound =0
else:
tableUpperBound = table[counter-1]
divisionUpperBound =divisionLowerBound + divisionInterval
if tableUpperBound > 0:
tableLowerBound = round(tableLowerBound)
tableUpperBound = round(tableUpperBound)
if tableUpperBound == decimalDivident:
print(f"Division is {divisionUpperBound}")
elif divisionLowerBound == decimalDivident:
print(f"Division is {divisionLowerBound}")
else:
print(f"{decimalDivident} lies between {tableLowerBound} and {tableUpperBound}")
print(f"Division lies between {round(divisionLowerBound,4)} and {round(divisionUpperBound,4)}")
#iteration incremented
interation =interation +1
interval = decimalDivisor/100 #0.24
divisionInterval = (divisionUpperBound-divisionLowerBound)/10 # 0.1
# reset counter & factor
counter=1
#break
else:
factor = factor +1
counter=counter+1
elif interation == 4:#377.379 | 15.7,16.8
print(f"interval={round(interval,3)},divisionInterval={round(divisionInterval,3)}")
intermediateResult = tableLowerBound + interval
table[counter-1] = intermediateResult
if table[counter-1] < decimalDivident:
divisionLowerBound = divisionLowerBound + divisionInterval
tableLowerBound = table[counter-1]
tableUpperBound =0
else:
tableUpperBound = table[counter-1]
divisionUpperBound =divisionLowerBound + divisionInterval
if tableUpperBound > 0:
tableLowerBound = round(tableLowerBound)
tableUpperBound = round(tableUpperBound)
if tableUpperBound == decimalDivident:
print(f"Division is {divisionUpperBound}")
elif divisionLowerBound == decimalDivident:
print(f"Division is {divisionLowerBound}")
else:
print(f"{decimalDivident} lies between {tableLowerBound} and {tableUpperBound}")
print(f"Division lies between {round(divisionLowerBound,4)} and {round(divisionUpperBound,4)}")
#iteration incremented
interation =interation +1
# reset counter & factor
counter=1
#break
else:
factor = factor +1
counter=counter+1
No comments:
Post a Comment