i want convert this number to binary so i can convert it to floating point later, the algorithm i know takes the part after the point of a number, for example if i put 9.32 it takes 0.32 and divides it by 0.5 on the function puntoflot() when its divided by 0.5 it takes the first number and puts it on z then takes the decimals left and keeps dividing it, you can figure it out there in the code.... idk if this algorithm is good any help would be apreciated
the valorbinario() function works awesomely with a big number like 9.32 BUT with a number like this 9.10938291*10**-31 it doesnt run valorbinario() at all because it says this error
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
valorbinario(9.10938291*10**-31)
File "C:/Users/Emmanuel/Desktop/asdfad.py", line 26, in valorbinario
x=puntoflot(numeroderecha)
File "C:/Users/Emmanuel/Desktop/asdfad.py", line 11, in puntoflot
dere=int(x[2:])
ValueError: invalid literal for int() with base 10: '18765820000004e-32'
the puntoflot() recieves the part after the point for example 9.28 it takes 0.28 form valorbinario() valorbinario takes a float number and does alot of things, it substracts the decimals and puts it on the function puntoflot() in order to convert it to binary then converts the natural number in this case 9 to binary and then returns the whole number in this case '1001.010001111010111011001100'
so how can i do this?? if you can come up with a better algortihm to convert to binary i would be happy but if not, how can i fix this??
ok this is my code
you run valorbinario() first with a number the other function idk why i made it a function.... im just starting to learn code
def puntoflot(y):
i=0
z=""
while i<=23:
x=str(y/0.5)
z=z+str(x[0])
dere=int(x[2:])
y=float("0."+str(dere))
i+=1
return z
def valorbinario(numero):
numero=str(numero)
pos=numero.index(".")
numeroderecha=float("0."+str(numero[pos+1:]))
numeroizquierda=numero[:pos]
y=bin(int(numeroizquierda))
x=puntoflot(numeroderecha)
return y[2:]+"."+str(x)