summaryrefslogtreecommitdiff
path: root/src/math/nearbyint.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/nearbyint.c')
-rw-r--r--src/math/nearbyint.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/math/nearbyint.c b/src/math/nearbyint.c
new file mode 100644
index 00000000..781769fb
--- /dev/null
+++ b/src/math/nearbyint.c
@@ -0,0 +1,20 @@
+#include <fenv.h>
+#include "libm.h"
+
+/*
+rint may raise inexact (and it should not alter the fenv otherwise)
+nearbyint must not raise inexact
+
+(according to ieee754r section 7.9 both functions should raise invalid
+when the input is signaling nan, but c99 does not define snan so saving
+and restoring the entire fenv should be fine)
+*/
+
+double nearbyint(double x) {
+ fenv_t e;
+
+ fegetenv(&e);
+ x = rint(x);
+ fesetenv(&e);
+ return x;
+}