apnakaamkar's blog

By apnakaamkar, history, 3 years ago, In English

Can anyone tell me why I am getting different values of lhs and rhs? I expected them to produce similar results.

double lhs=y*log10(x);
double rhs=x*log10(y);

**

double lhs=log10(x);
double rhs=log10(y);
lhs*=y;
rhs*=x;

I am getting wrong answers for x=6,y=6 while submitting on codeforces but I am getting correct on VSCode.

  • Vote: I like it
  • -19
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +12 Vote: I do not like it

How are you doing comparison directly like this? Never do comparisons without some tolerance in precision. There might be a precision error as small as $$$10^{-15}$$$ or even smaller which makes both not equal to each other. You can do something like:

Code

You are lucky that the other code passed with direct comparisons.