Precision Comparison of C++ 11 atan2 and Java 8 Math.atan2

Hi All,

There was two different solution one written in c++, other in Java. both using Standard library atan2 function.

Problem : http://codeforces.com/contest/598/problem/C

c++: http://codeforces.com/contest/598/submission/20762399 status : AC

java: http://codeforces.com/contest/598/submission/31950657 status : Failed on test 127

There are only 3/4 AC in java which uses BigDecimal. Is there is any difference in standard atan2 function in Java & c++ ? if yes, is there any other way to handle precision without using BigDecimal.

Thanks in Advance.

That’s odd… the AC code above gives WA when I run it: nXndu1
However all other accepted C++ solutions I found differ from your Java solution in that they use long double and not double. The difference is not in the library functions but in the datatypes used. long double has better precision and that’s probably the reason it gets AC. Since there is no long double type in Java, I suppose BigDecimal is the only way out.

1 Like

In java for storing real numbers of large length , I think only BigDecimal is available.

java long is equivalent long double in cpp. REF: double IEEE 754 floating point ( 64 bits 1.23456e300d, -1.23456e-300d, 1e1d )

yes, It’s solvable by BigDecimal, are there is other ways to handle it with 8Byte double variable in Java.

I am not sure but I think for this only BigDecimal works

No it’s not. If you see the Wikipedia link I have provided it says

As with C’s other floating-point types, it may not necessarily map to an IEEE format.

And also

With the GNU C Compiler, long double is 80-bit extended precision on x86 processors regardless of the physical storage used for the type (which can be either 96 or 128 bits).

So long double uses 80 bits whereas double uses 64 bits.

1 Like