Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. # fracbase.py
  2. import sys, math
  3.  
  4. target = float(sys.argv[1])
  5. base = float(sys.argv[2])
  6.  
  7.  
  8. def decomp(tarnum, nbase):
  9. if nbase <= 1:
  10. return 'ERROR Base must be > 1'
  11. print('nRepresenting', tarnum, 'in base', nbase, ' Log is', math.log(tarnum, nbase), 'n')
  12. powr = int(math.log(tarnum, nbase))
  13. rep = []
  14. while powr > -13:
  15. tnratio = tarnum / nbase**powr
  16. rep.append(int(tnratio))
  17. print('There are', tnratio, nbase, 'to the power of', powr, 'in', tarnum, 'so', rep)
  18. tarnum -= int(tnratio) * nbase**powr
  19. if tarnum == 0:
  20. if powr > 0:
  21. for i in range(powr):
  22. rep.append(0)
  23. return rep
  24. if powr == 0:
  25. rep.append('.')
  26. powr -= 1
  27. return rep
  28.  
  29.  
  30. print(decomp(target, base))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement