rint.c 330 字节
Newer Older
1 2
#include <math.h>
#include <stdint.h>
R
Rich Felker 已提交
3 4 5

double rint(double x)
{
6 7 8 9
	union {double f; uint64_t i;} u = {x};
	int e = u.i>>52 & 0x7ff;
	int s = u.i>>63;
	double_t y;
R
Rich Felker 已提交
10

11 12 13 14 15 16 17 18 19
	if (e >= 0x3ff+52)
		return x;
	if (s)
		y = (double)(x - 0x1p52) + 0x1p52;
	else
		y = (double)(x + 0x1p52) - 0x1p52;
	if (y == 0)
		return s ? -0.0 : 0;
	return y;
R
Rich Felker 已提交
20
}