-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion_math.py
103 lines (79 loc) · 3.22 KB
/
conversion_math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Decimal Conversion Functions
def decimal_to_binary(num):
# Split into integer and fractional parts
integer_part = int(num) # takes just the integer part of the number
fractional_part = num - integer_part # subtracts off the integer just leaving the decimal
# Convert integer part using the built-in bin function
binary_integer = bin(integer_part)[2:]
# Convert fractional part
binary_fraction = '.'
while fractional_part:
fractional_part *= 2
bit = int(fractional_part)
if bit == 1:
fractional_part -= bit
binary_fraction += str(bit)
# Limiting the length of the fractional part to avoid infinite loop
if len(binary_fraction) > 10:
break
return binary_integer + binary_fraction
def decimal_to_octal(num):
integer_part = int(num)
fractional_part = num - integer_part
# Convert integer part using the built-in oct function
octal_integer = oct(integer_part)[2:]
# Convert fractional part
octal_fraction = '.'
while fractional_part:
fractional_part *= 8
digit = int(fractional_part)
fractional_part -= digit
octal_fraction += str(digit)
# Limiting the length of the fractional part
if len(octal_fraction) > 10:
break
return octal_integer + octal_fraction
def decimal_to_hex(num):
integer_part = int(num)
fractional_part = num - integer_part
# Convert integer part using the built-in hex function
hex_integer = hex(integer_part)[2:].upper()
# Convert fractional part
hex_fraction = '.'
while fractional_part:
fractional_part *= 16
digit = int(fractional_part)
fractional_part -= digit
hex_digit = hex(digit)[2:].upper()
hex_fraction += hex_digit
# Limiting the length of the fractional part
if len(hex_fraction) > 10:
break
return hex_integer + hex_fraction
# Binary Conversion Functions
def binary_to_decimal(binary_str):
if '.' in binary_str:
integer_part, fractional_part = binary_str.split('.')
else:
integer_part, fractional_part = binary_str, '0'
decimal_integer = int(integer_part, 2)
decimal_fraction = sum(int(bit) * 2 ** -(index + 1) for index, bit in enumerate(fractional_part))
return decimal_integer + decimal_fraction
# Octal Conversion Functions
def octal_to_decimal(octal_str):
if '.' in octal_str:
integer_part, fractional_part = octal_str.split('.')
else:
integer_part, fractional_part = octal_str, '0'
decimal_integer = int(integer_part, 8)
decimal_fraction = sum(int(digit) * 8 ** -(index + 1) for index, digit in enumerate(fractional_part))
return decimal_integer + decimal_fraction
# Hex Conversion Functions
def hex_to_decimal(hex_str):
if '.' in hex_str:
integer_part, fractional_part = hex_str.split('.')
else:
integer_part, fractional_part = hex_str, '0'
decimal_integer = int(integer_part, 16)
decimal_fraction = sum(int(digit, 16) * 16 ** -(index + 1) for index, digit in enumerate(fractional_part))
return decimal_integer + decimal_fraction