Re: float to int conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2013-04-22 10:12:05 +0000, Warlich, Christof wrote:
> After all, this reminds be a bit of a hack lawver: A legal loophole
> shamelessly capitalizing upon ;-). At least, it violates the golden
> principle of "least surprise", being said to be an essential part of
> good software design.

> Anyhow, as I really need to deal with such situations: Is there a way
> to catch such conversion errors without cluttering the code with checks
> that test if still being within the range of a conversion or not? Skimming
> the paper linked by David suggests that it should be possible to
> install trap handlers for this purpose. Does anyone know how to do this
> with gcc?

I'm not sure, but indeed, there seems to be a bug, though your
program doesn't do the right test.

#include <stdio.h>
#include <fenv.h>

int main (void)
{
  volatile float c = (volatile int) 0x7fffffbf;
  printf ("inexact = %d\n", fetestexcept (FE_INEXACT));
  c /= 123;
  printf ("inexact = %d\n", fetestexcept (FE_INEXACT));
  return 0;
}

outputs on Linux/x86_64 without optimization:

inexact = 0
inexact = 32

Is there any reason why the inexact flag isn't set for the
int-to-float conversion?

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux