blob: 092e9ffae56e96b7d24003ea9d34adb9b4ea6e0d [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001#include <fenv.h>
nsz9560b6b2012-03-13 19:51:14 +01002#include <math.h>
Rich Felkerb69f6952012-03-13 01:17:53 -04003
nsz91c28f62012-03-20 22:49:19 +01004float nearbyintf(float x)
5{
6#ifdef FE_INEXACT
Szabolcs Nagy033a9d62012-11-13 13:34:45 +01007 #pragma STDC FENV_ACCESS ON
nsz91c28f62012-03-20 22:49:19 +01008 int e;
Rich Felkerb69f6952012-03-13 01:17:53 -04009
nsz91c28f62012-03-20 22:49:19 +010010 e = fetestexcept(FE_INEXACT);
11#endif
Rich Felkerb69f6952012-03-13 01:17:53 -040012 x = rintf(x);
nsz91c28f62012-03-20 22:49:19 +010013#ifdef FE_INEXACT
14 if (!e)
15 feclearexcept(FE_INEXACT);
16#endif
Rich Felkerb69f6952012-03-13 01:17:53 -040017 return x;
18}