0

I was working on some problem in codeforces just for practice and the implementation I came up with is based on using the Math.pow() method in Java:

import java.io.*;
import java.util.*;

public class TestClass {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        long t = sc.nextLong();

        //note that "MyScanner" is a pre-defined class for buffered input 
        //just to make it easier to call the methods
        
        while (t-- > 0){
            String m = sc.next();
            long l = m.length(), n = Long.parseLong(m);
            out.println(n - Math.pow(10, l-1));
        }
        out.close();    
    }
}

I/O example: the input can be too big, that's why I used the long datatype.

Input:

7  
1  
2  
178  
20  
999999999  
9000  
987654321  

Output:

0  
1  
78  
10  
899999999  
8000  
887654321  

but instead I get this:

0.0  
1.0  
78.0  
10.0  
8.99999999E8  
8000.0  
8.87654321E8  

as you can see, my solution seems to be correct, but the output was in different form.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Colton Walker
  • 620
  • 6
  • 14
  • They're the same values, but using scientific/engineering notation. – jonrsharpe Jul 10 '22 at 17:57
  • @jonrsharpe yes i know that, but unfortunately, the output was considered as wrong – Colton Walker Jul 10 '22 at 17:58
  • What is the point of creating a `PrintWriter` for `System.out` that is already a `PrintWriter`? Why didn't you just use `System.out.println("foo");`? – cyberbrain Jul 10 '22 at 18:17
  • @cyberbrain i believe this has nothing to do with the main question. – Colton Walker Jul 10 '22 at 18:25
  • That is just the natural "toString" of doubles. Java considers this easier to read and uses it as the default. You can disable that if you really dont want it. Instead of just printing out the double, you would basically use `printf` with the appropriate specifiers, or `DecimalFormatter`. The linked duplicate shows you how to do that, so you get the "expected output" instead. – Zabuzard Jul 10 '22 at 19:00
  • 1
    You are right, the PrintWriter thing does not cause your issue. But @cyberbrain mentioned it, because it is a flaw in your code and you should change it regardless. – Zabuzard Jul 10 '22 at 19:02

2 Answers2

2

The method you use has the following signature: static double pow(double a, double b). This means, the parameters will automatically be cast to double and it's return value is also a double.

There is no overload for a long return value so you will either have to cast the result manually to long with this: n - (long)Math.pow(10, l-1).

Edit: or use another solution like the BigInteger one, but be aware that BigInteger (and BigDecimal) only accepts int as exponent, not long and not BigInteger.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
0

Math.pow won't guarantee 100% accuracy on large integer values beyond some threshold (see this question for details) because floats and doubles cannot accurately represent all integers supported by the long type. Therefore, I would recommend you to use a BigInteger, which also has a pow function and will give you correctly formatted results out of the box.

Dici
  • 25,226
  • 7
  • 41
  • 82