mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 09:50:59 -04:00
add some exs
This commit is contained in:
parent
0d5e21b867
commit
9b4c8df7f7
7 changed files with 85 additions and 1 deletions
26
interview_cake/math/float_bin_num.py
Normal file
26
interview_cake/math/float_bin_num.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
|
||||
representation. If the Number cannot be represented accurately in binary, with at
|
||||
most 32 chars, print error:
|
||||
'''
|
||||
|
||||
def get_float_rep(num):
|
||||
if num >= 1 or num <= 0: return 'Error 1'
|
||||
result = '.'
|
||||
while num:
|
||||
if len(result) >= 32: return 'Error 2', result
|
||||
r = num*2
|
||||
if r >= 1:
|
||||
result += '1'
|
||||
num = r - 1
|
||||
else:
|
||||
result += '0'
|
||||
num = r
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print get_float_rep(0.72) #('Error 2', '.1011100001010001111010111000010')
|
||||
print get_float_rep(0.1) # ('Error 2', '.0001100110011001100110011001100')
|
||||
print get_float_rep(0.5) #'.1'
|
Loading…
Add table
Add a link
Reference in a new issue