Codeforces and Polygon may be unavailable from May 23, 4:00 (UTC) to May 23, 8:00 (UTC) due to technical maintenance. ×

kira_lite's blog

By kira_lite, history, 12 days ago, In English

I observed a very strange behaviour in one of the recent problems of the sqrt() function, where a negative number was passed on to sqrt() function and it worked properly in one way but not the other. I have 2 submissions- one which gives an AC while the other shows Memory out of bounds, which it should have thrown ideally.

The only difference b/w the 2 submissions is that i am having a long long value to subtract from in the AC submission, but i dont see much difference because of it. If someone can provide a brief of why its happening, it'll be a great lesson !

Thanks in advance

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

»
12 days ago, # |
  Vote: I like it 0 Vote: I do not like it

The result of a cast of a floating point number to an integer is undefined/unspecified for values not in the range of the integer variable (±1 for truncation).

Clause 6.3.1.4:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

If the implementation defines __STDC_IEC_559__, then for conversions from a floating-point type to an integer type other than _BOOL:

if the floating value is infinite or NaN or if the integral part of the floating value exceeds the range of the integer type, then the "invalid" floating- point exception is raised and the resulting value is unspecified.

  • »
    »
    10 days ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    yeah but if the resulting value is undefined, then how does it justify the AC for that solution ?