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

Автор kira_lite, история, 3 недели назад, По-английски

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

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

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

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.

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

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