Advertisement
francescom

Untitled

May 6th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. def convInt(B, n):
  2. #@param B int, Base a cui voglio arrivare
  3. #@param n int, numero che voglio convertire da base 10
  4. #@return int, intero convertito
  5.   if n==0:
  6.     return 0
  7.   l=[]
  8.   while n>0:
  9.     ci=n%B
  10.     n=n/B
  11.     l.append(ci)
  12.   l.reverse()
  13.   z=''
  14.   for i in range(0, len(l)):
  15.     m=l[i]
  16.     z=z+str(m)
  17.   return int(z)
  18.  
  19. def convFraz(B, f, maxApprox):
  20. #@param B int, base a cui voglio arrivare
  21. #@param f float
  22. #@param maxApproxint, numero massimo di cifre frazionarie, valore modificabile
  23. #@return int, intero convertito
  24.   if f==0:
  25.     return 0
  26.   i=1
  27.   z=''
  28.   while f>0 and i<=maxAppox:
  29.     ci=int(f*B)
  30.     f=float(f*B)-ci
  31.     z=z+str(ci)
  32.     i=i+1
  33.   return int(z)
  34.  
  35. def convFloat(B, z, maxApprox):
  36.   #@param z float
  37.   #@param B int, base a cui voglio arrivare
  38.   #@return float, (parte intera convertita).(parte frazionaria convertita)
  39.   n=int(z)
  40.   f=z-int(z)
  41.   h=str(convInt(B, n))+'.'+str(convFraz(B, f, maxApprox))
  42.   return float(h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement