xref: /openbsd-src/lib/libm/src/s_round.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_round.c,v 1.7 2016/09/12 19:47:02 guenther Exp $	*/
265850ae2Sbrad 
365850ae2Sbrad /*-
465850ae2Sbrad  * Copyright (c) 2003, Steven G. Kargl
565850ae2Sbrad  * All rights reserved.
665850ae2Sbrad  *
765850ae2Sbrad  * Redistribution and use in source and binary forms, with or without
865850ae2Sbrad  * modification, are permitted provided that the following conditions
965850ae2Sbrad  * are met:
1065850ae2Sbrad  * 1. Redistributions of source code must retain the above copyright
1165850ae2Sbrad  *    notice unmodified, this list of conditions, and the following
1265850ae2Sbrad  *    disclaimer.
1365850ae2Sbrad  * 2. Redistributions in binary form must reproduce the above copyright
1465850ae2Sbrad  *    notice, this list of conditions and the following disclaimer in the
1565850ae2Sbrad  *    documentation and/or other materials provided with the distribution.
1665850ae2Sbrad  *
1765850ae2Sbrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1865850ae2Sbrad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1965850ae2Sbrad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2065850ae2Sbrad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2165850ae2Sbrad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2265850ae2Sbrad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2365850ae2Sbrad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2465850ae2Sbrad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2565850ae2Sbrad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2665850ae2Sbrad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2765850ae2Sbrad  */
2865850ae2Sbrad 
2949393c00Smartynas #include <float.h>
3049393c00Smartynas #include <math.h>
3149393c00Smartynas 
3265850ae2Sbrad #include "math_private.h"
3365850ae2Sbrad 
3465850ae2Sbrad double
round(double x)3565850ae2Sbrad round(double x)
3665850ae2Sbrad {
3765850ae2Sbrad 	double t;
3865850ae2Sbrad 
3965850ae2Sbrad 	if (isinf(x) || isnan(x))
4065850ae2Sbrad 		return (x);
4165850ae2Sbrad 
4265850ae2Sbrad 	if (x >= 0.0) {
4365850ae2Sbrad 		t = floor(x);
4465850ae2Sbrad 		if (t - x <= -0.5)
4565850ae2Sbrad 			t += 1.0;
4665850ae2Sbrad 		return (t);
4765850ae2Sbrad 	} else {
4865850ae2Sbrad 		t = floor(-x);
4965850ae2Sbrad 		if (t + x <= -0.5)
5065850ae2Sbrad 			t += 1.0;
5165850ae2Sbrad 		return (-t);
5265850ae2Sbrad 	}
5365850ae2Sbrad }
54*2f2c0062Sguenther DEF_STD(round);
55*2f2c0062Sguenther LDBL_MAYBE_CLONE(round);
56