fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println("3.125 can be perfectly represented in base 2 :");
  13. System.out.println(getDigits(3.125));
  14. System.out.println("But 3.1415 can't :");
  15. System.out.println(getDigits(3.1415));
  16. }
  17.  
  18. public static List<Integer> getDigits(double d) {
  19. List<Integer> result = new ArrayList<>();
  20. while (d > 0) {
  21. int mostSignificantDigit = (int) d;
  22. result.add(mostSignificantDigit);
  23. d = (d - mostSignificantDigit) * 10;
  24. }
  25. return result;
  26. }
  27. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
3.125 can be perfectly represented in base 2 :
[3, 1, 2, 5]
But 3.1415 can't :
[3, 1, 4, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 1, 1, 8, 8, 3, 9, 7, 6, 1, 8, 8, 2, 5, 5, 4, 7, 3, 9, 7, 1, 3, 6, 6, 8, 8, 2, 3, 2, 4, 2, 1, 8, 7, 5]