.\" $NetBSD: rint.3,v 1.12 2003/08/07 16:44:48 agc Exp $
.\" $DragonFly: src/lib/libm/man/rint.3,v 1.1 2005/07/26 21:15:20 joerg Exp $
.\"
-.Dd March 10, 1994
+.Dd November 28, 2009
.Dt RINT 3
.Os
.Sh NAME
+.Nm nearbyint ,
+.Nm nearbyintf ,
.Nm rint ,
.Nm rintf
.Nd round to integral value in floating-point format
.Sh SYNOPSIS
.In math.h
.Ft double
+.Fn nearbyint "double x"
+.Ft float
+.Fn nearbyintf "float x"
+.Ft double
.Fn rint "double x"
.Ft float
.Fn rintf "float x"
nearest to
.Fa x
according to the prevailing rounding mode.
+.Pp
+The
+.Fn nearbyint
+and
+.Fn nearbyintf
+functions perform the same operation, except that they do not raise
+an inexact exception.
.Sh SEE ALSO
.Xr abs 3 ,
.Xr ceil 3 ,
s_finitef.c s_floor.c s_floorf.c s_fmax.c s_fmaxf.c s_fmaxl.c \
s_fmin.c s_fminf.c s_fminl.c s_frexpf.c s_frexpl.c s_ilogb.c \
s_ilogbf.c s_ldexpf.c s_log1p.c s_log1pf.c s_logb.c s_logbf.c \
- s_modff.c s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c \
+ s_modff.c s_nearbyint.c s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c \
s_round.c s_roundf.c s_scalbn.c s_scalbnf.c \
s_signgam.c s_significand.c s_significandf.c s_sin.c s_sinf.c \
s_tan.c s_tanf.c s_tanh.c s_tanhf.c
--- /dev/null
+/*-
+ * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+#include <fenv.h>
+#include <math.h>
+
+/*
+ * We save and restore the floating-point environment to avoid raising
+ * an inexact exception. We can get away with using fesetenv()
+ * instead of feclearexcept()/feupdateenv() to restore the environment
+ * because the only exception defined for rint() is overflow, and
+ * rounding can't overflow as long as emax >= p.
+ */
+#define DECL(type, fn, rint) \
+type \
+fn(type x) \
+{ \
+ type ret; \
+ fenv_t env; \
+ \
+ fegetenv(&env); \
+ ret = rint(x); \
+ fesetenv(&env); \
+ return (ret); \
+}
+
+DECL(double, nearbyint, rint)
+DECL(float, nearbyintf, rintf)