xref: /minix3/lib/libm/noieee_src/n_round.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
12fe8fb19SBen Gras /*-
22fe8fb19SBen Gras  * Copyright (c) 2003, Steven G. Kargl
32fe8fb19SBen Gras  * All rights reserved.
42fe8fb19SBen Gras  *
52fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
62fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
72fe8fb19SBen Gras  * are met:
82fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
92fe8fb19SBen Gras  *    notice unmodified, this list of conditions, and the following
102fe8fb19SBen Gras  *    disclaimer.
112fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras  *
152fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182fe8fb19SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192fe8fb19SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202fe8fb19SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212fe8fb19SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222fe8fb19SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232fe8fb19SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242fe8fb19SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252fe8fb19SBen Gras  */
262fe8fb19SBen Gras 
272fe8fb19SBen Gras #include <sys/cdefs.h>
282fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
29*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: n_round.c,v 1.2 2014/03/16 10:02:27 martin Exp $");
302fe8fb19SBen Gras #if 0
312fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/msun/src/s_round.c,v 1.1 2004/06/07 08:05:36 das Exp $");
322fe8fb19SBen Gras #endif
332fe8fb19SBen Gras #endif
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <math.h>
362fe8fb19SBen Gras 
37*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
38*0a6a1f1dSLionel Sambuc __weak_alias(roundl, round);
39*0a6a1f1dSLionel Sambuc #endif
40*0a6a1f1dSLionel Sambuc 
412fe8fb19SBen Gras double
round(double x)422fe8fb19SBen Gras round(double x)
432fe8fb19SBen Gras {
442fe8fb19SBen Gras 	double t;
452fe8fb19SBen Gras 
462fe8fb19SBen Gras 	if (x >= 0.0) {
472fe8fb19SBen Gras 		t = ceil(x);
482fe8fb19SBen Gras 		if (t - x > 0.5)
492fe8fb19SBen Gras 			t -= 1.0;
502fe8fb19SBen Gras 		return (t);
512fe8fb19SBen Gras 	} else {
522fe8fb19SBen Gras 		t = ceil(-x);
532fe8fb19SBen Gras 		if (t + x > 0.5)
542fe8fb19SBen Gras 			t -= 1.0;
552fe8fb19SBen Gras 		return (-t);
562fe8fb19SBen Gras 	}
572fe8fb19SBen Gras }
58