Блог пользователя wil93

Автор wil93, 13 лет назад, По-английски

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?

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
13 лет назад, # |
Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

deleted

»
13 лет назад, # |
Rev. 2   Проголосовать: нравится +16 Проголосовать: не нравится

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).