Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 213 additions & 59 deletions Decimal_To_Binary.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,219 @@
# patch-255
decimal_accuracy = 7


def dtbconverter(num):
whole = []
fractional = ["."]

decimal = round(num % 1, decimal_accuracy)
w_num = int(num)

i = 0
while decimal != 1 and i < decimal_accuracy:
decimal = decimal * 2
fractional.append(int(decimal // 1))
decimal = round(decimal % 1, decimal_accuracy)
if decimal == 0:
break
i += 1

while w_num != 0:
whole.append(w_num % 2)
w_num = w_num // 2
whole.reverse()

i = 0
while i < len(whole):
print(whole[i], end="")
i += 1
i = 0
while i < len(fractional):
print(fractional[i], end="")
i += 1


number = float(input("Enter Any base-10 Number: "))

dtbconverter(number)


# i think this code have not proper comment and noe this is easy to understand
"""
=======
Program: Decimal to Binary converter.

THis program accepts fractional values, the accuracy can be set below:
Decimal to Binary Converter

This program converts base-10 decimal numbers (including fractional values)
to their binary representation. The conversion handles both integer and
fractional parts with configurable precision.

Features:
- Converts both integer and fractional decimal numbers to binary
- Configurable precision for fractional part conversion
- Handles negative numbers
- Type annotations for better code clarity
- Robust error handling for various input scenarios
"""


# Function to convert decimal number
# to binary using recursion
def DecimalToBinary(num):
if num > 1:
DecimalToBinary(num // 2)
print(num % 2, end="")
decimal_accuracy = 7 # Precision for fractional part conversion


def decimal_to_binary(number: float) -> str:
"""
Convert a decimal number to its binary representation.

Args:
number (float): The base-10 number to convert to binary

Returns:
str: Binary representation of the input number
"""
# Handle special cases
if number == 0:
return "0"

# Handle negative numbers
is_negative = number < 0
number = abs(number)

# Separate integer and fractional parts
integer_part = int(number)
fractional_part = round(number - integer_part, decimal_accuracy)

# Convert integer part to binary
integer_binary = _convert_integer_part(integer_part)

# Convert fractional part to binary
fractional_binary = _convert_fractional_part(fractional_part)

# Combine parts and add sign if needed
result = integer_binary + fractional_binary
return f"-{result}" if is_negative else result


def _convert_integer_part(integer: int) -> str:
"""
Convert integer part to binary using division method.

Args:
integer (int): Integer part of the number

Returns:
str: Binary representation of the integer part
"""
if integer == 0:
return "0"

binary_digits = []
num = integer

while num > 0:
binary_digits.append(str(num % 2)) # Get remainder
num = num // 2 # Integer division

# Reverse the list to get correct binary order
binary_digits.reverse()
return "".join(binary_digits)


def _convert_fractional_part(fraction: float) -> str:
"""
Convert fractional part to binary using multiplication method.

Args:
fraction (float): Fractional part of the number (0 <= fraction < 1)

Returns:
str: Binary representation of the fractional part
"""
if fraction == 0:
return ""

binary_digits = ["."]
current_fraction = fraction
iterations = 0

# Convert fractional part until it becomes 0 or reaches maximum precision
while current_fraction > 0 and iterations < decimal_accuracy:
# Multiply by 2 and take integer part
current_fraction *= 2
integer_part = int(current_fraction)
binary_digits.append(str(integer_part))

# Keep only the fractional part for next iteration
current_fraction -= integer_part
current_fraction = round(current_fraction, decimal_accuracy)
iterations += 1

return "".join(binary_digits)


def decimal_to_binary_recursive(number: int) -> str:
"""
Alternative recursive implementation for integer conversion only.

Note: This version only works with integers and doesn't handle fractional parts.

Args:
number (int): Integer number to convert to binary

Returns:
str: Binary representation of the integer
"""
if number > 1:
return decimal_to_binary_recursive(number // 2) + str(number % 2)
return str(number)


def get_user_input() -> float:
"""
Safely get user input with comprehensive error handling.

Returns:
float: Validated decimal number from user input

Raises:
KeyboardInterrupt: If user interrupts the program
EOFError: If no input is available (non-interactive environment)
"""
max_attempts = 3

for attempt in range(max_attempts):
try:
user_input = input("Enter any base-10 number (or 'quit' to exit): ").strip()

if user_input.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
exit(0)

return float(user_input)

except ValueError:
print(f"Error: '{user_input}' is not a valid number. Please try again.")
if attempt < max_attempts - 1:
print(f"{max_attempts - attempt - 1} attempts remaining.")
else:
print("Maximum attempts reached. Using default value 0.")
return 0.0
except (EOFError, KeyboardInterrupt):
raise


def main() -> None:
"""Main function to run the decimal to binary converter."""
print("=== Decimal to Binary Converter ===")
print(f"Fractional precision: {decimal_accuracy} bits")
print("Enter 'quit' to exit the program")
print("-" * 40)

try:
while True:
try:
# Get input from user
number = get_user_input()

# Convert to binary using main method
binary_result = decimal_to_binary(number)

# Display results
print(f"\nDecimal number: {number}")
print(f"Binary representation: {binary_result}")

# For integer inputs, show recursive method as well
if number.is_integer() and number >= 0:
recursive_result = decimal_to_binary_recursive(int(number))
print(f"Recursive method (integer only): {recursive_result}")

print("-" * 40)

except (KeyboardInterrupt, EOFError):
print("\n\nProgram terminated by user. Goodbye!")
break
except Exception as e:
print(f"\nAn unexpected error occurred: {e}")
print("Please try again with a different number.")
print("-" * 40)

except (KeyboardInterrupt, EOFError):
print("\n\nProgram terminated. Goodbye!")


def run_example() -> None:
"""
Run example conversions for demonstration purposes.
Useful when running in non-interactive environments.
"""
print("=== Example Conversions (Non-interactive Mode) ===")
examples = [10.0, 15.75, -3.125, 0.5, 255.255]

for example in examples:
binary_result = decimal_to_binary(example)
print(f"Decimal: {example:8} -> Binary: {binary_result}")

print("\nTo use interactive mode, run the program in a terminal.")


# Driver Code
if __name__ == "__main__":
# decimal value
dec_val = 24

# Calling function
DecimalToBinary(dec_val)
# master
try:
main()
except (EOFError, KeyboardInterrupt):
print("\n\nNo interactive input available. Running in example mode.")
run_example()
Loading