summaryrefslogtreecommitdiff
path: root/src/math/nearbyint.c
blob: 714c55cadb68ed95a357a3bf900b00fe651b78d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fenv.h>
#include <math.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;
}