Afifii's blog

By Afifii, history, 7 years ago, In English

In a problem, when I evaluate the following expressions directly in the "main" function, the solution gets accepted.

ll sum1 = a[i] + com[n]-com[n-k] + com[i-1]-com[i-1-k];
ll sum2 = a[i] + com[n]-com[n-(k-1)] + com[i-1]-com[i-1-(k-1)];
double av1 = sum1/(double)(2*k+1);
double av2 = sum2/(double)(2*(k-1)+1);

But, when I instead use a function to evaluate the same expressions, the solution fails at test case 16.

double av1 = ValAt(i, k);
double av2 = ValAt(i, k-1);

The function:

double ValAt(int i, int k)
{
    ll sum = a[i] + com[n]-com[n-k] + com[i-1]-com[i-1-k];
    return sum/(double)(2*k+1);
}

Does any one have a rational explanation to this? Here are the two submissions: 28570229 , 28570252

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Afifii (previous revision, new revision, compare).

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

I sent your solution and got AC changing double to long double, look. I think when you are assigning av to result of function it loses some precision and when you directly assign av to result it doesn't.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    So, why should I loose precision when I am returning a value from a function that has the same return type? That is the strange point. Moreover, when I declared av1 and av2 as global variables and changed their values from the function, the solution also failed!

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      At the beginning of section 8 expressions:

      The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.