wil93's blog

By wil93, 12 years ago, In English

Hi, I'm facing issues using the standard complex class from C++, in this task: The angle between the vectors.

I need to compute the length of vectors, okay: If I use the abs() function or sqrt(norm()) (same thing) I get 3 WAs out of 64 tests. If I compute it manually, I get AC.

This code gets AC, because I compute manually the lengths. This code gets 3 WAs, because I use abs().

Since I can't see input files I don't understand what is the problem.

Any idea?

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
12 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

deleted

»
12 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

Let's look at sources of abs() (gcc 4.6.2):

  template<typename _Tp>
    inline _Tp
    __complex_abs(const complex<_Tp>& __z)
    {
      _Tp __x = __z.real();
      _Tp __y = __z.imag();
      const _Tp __s = std::max(abs(__x), abs(__y));
      if (__s == _Tp())  // well ...
        return __s;
      __x /= __s; 
      __y /= __s;
      return __s * sqrt(__x * __x + __y * __y);
    }

So, it does almost the same as you except some kind of normalization in order to avoid overflow.

So, I think problem is in precision and rounding — it seems like e-olimp.com use strict comparing of float numbers (0.1234549999999 after rounding becomes 0.12345, 0.1234550000000 becomes 0.12346 and e-olimp.com considers these numbers different, but real difference is 1e-13).