diff --git a/Mathematical_Algorithms/src/Decimal_To_Binary.py b/Mathematical_Algorithms/src/Decimal_To_Binary.py new file mode 100644 index 00000000..89d99701 --- /dev/null +++ b/Mathematical_Algorithms/src/Decimal_To_Binary.py @@ -0,0 +1,40 @@ +'''PYTHON 3 +Author: S Sandeep Pillai (github.com/Corruption13) +Decimal to Binary converter. + +This is a complete decimal to binary converted which accepts any fractional or whole decimal values; The fractional accuracy can be set below. +''' +decimal_accuracy = 7 + + +def dtbconverter(num): # Function inputs a float value and returns a list as output + # Reasoning for list instead of integer: to avoid integer overflow error. + + whole= [] # The part before decimal point + fractional = ['.'] # The part after decimal point + + decimal = round(num%1, decimal_accuracy) # Extract fractional number part of decimal + w_num = int(num) # Extract whole number part of decimal. + + i=0 # Some fractional decimal numbers have infinite binary values, so we limit this loop below. + + #Loop to find binary of decimal part + while(decimal!=1 and i