Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convInt(B, n):
- #@param B int, Base a cui voglio arrivare
- #@param n int, numero che voglio convertire da base 10
- #@return int, intero convertito
- if n==0:
- return 0
- l=[]
- while n>0:
- ci=n%B
- n=n/B
- l.append(ci)
- l.reverse()
- z=''
- for i in range(0, len(l)):
- m=l[i]
- z=z+str(m)
- return int(z)
- def convFraz(B, f, maxApprox):
- #@param B int, base a cui voglio arrivare
- #@param f float
- #@param maxApproxint, numero massimo di cifre frazionarie, valore modificabile
- #@return int, intero convertito
- if f==0:
- return 0
- i=1
- z=''
- while f>0 and i<=maxAppox:
- ci=int(f*B)
- f=float(f*B)-ci
- z=z+str(ci)
- i=i+1
- return int(z)
- def convFloat(B, z, maxApprox):
- #@param z float
- #@param B int, base a cui voglio arrivare
- #@return float, (parte intera convertita).(parte frazionaria convertita)
- n=int(z)
- f=z-int(z)
- h=str(convInt(B, n))+'.'+str(convFraz(B, f, maxApprox))
- return float(h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement