fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.Math;
  5. import java.lang.*;
  6. import java.io.*;
  7. import java.math.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Scanner in = new Scanner(System.in);
  15. while(in.hasNextDouble()) {
  16. double x = in.nextDouble();
  17. double dx = in.nextDouble();
  18.  
  19. int dxSignificatDigit = (int)Math.round(Math.floor(Math.log10(Math.abs(dx))));
  20. int xSignificatDigit = (int)Math.round(Math.ceil(Math.log10(Math.abs(x))));
  21. MathContext context = new MathContext(Math.max(xSignificatDigit-dxSignificatDigit+1,1));
  22. MathContext dContext = new MathContext(2);
  23. BigDecimal decX = new BigDecimal(x, context);
  24. BigDecimal decDx = new BigDecimal(dx, dContext);
  25.  
  26. System.out.println(decX + " ±" + decDx);
  27. }
  28. }
  29. }
Success #stdin #stdout 0.12s 380800KB
stdin
100.7 10
100.7 11
100.7 9.9
1212.3 1
1212.3 2.3
12123.3231 0.03
0.0001 0.5
0.0005 0.0000032
stdout
101  ±10
101  ±11
100.7  ±9.9
1212.3  ±1
1212.3  ±2.3
12123.323  ±0.030
0.0001  ±0.5
0.0005000  ±0.0000032