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*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_round.c,v 1.3 2013/11/11 23:57:34 joerg 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*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
__strong_alias(_roundl,round)38*84d9c625SLionel Sambuc __strong_alias(_roundl, round)
39*84d9c625SLionel Sambuc __weak_alias(roundl, round)
40*84d9c625SLionel Sambuc #endif
41*84d9c625SLionel Sambuc
422fe8fb19SBen Gras double
432fe8fb19SBen Gras round(double x)
442fe8fb19SBen Gras {
452fe8fb19SBen Gras double t;
462fe8fb19SBen Gras int i;
472fe8fb19SBen Gras
482fe8fb19SBen Gras i = fpclassify(x);
492fe8fb19SBen Gras if (i == FP_INFINITE || i == FP_NAN)
502fe8fb19SBen Gras return (x);
512fe8fb19SBen Gras
522fe8fb19SBen Gras if (x >= 0.0) {
532fe8fb19SBen Gras t = floor(x);
542fe8fb19SBen Gras if (x - t >= 0.5)
552fe8fb19SBen Gras t += 1.0;
562fe8fb19SBen Gras return (t);
572fe8fb19SBen Gras } else {
582fe8fb19SBen Gras x = -x;
592fe8fb19SBen Gras t = floor(x);
602fe8fb19SBen Gras if (x - t >= 0.5)
612fe8fb19SBen Gras t += 1.0;
622fe8fb19SBen Gras return (-t);
632fe8fb19SBen Gras }
642fe8fb19SBen Gras }
65