xref: /netbsd-src/lib/libm/noieee_src/n_roundf.c (revision 42796049d0c7caa9aa8b7eaef4bca4cc1b0e04a6)
1*42796049Sis /*-
2*42796049Sis  * Copyright (c) 2003, Steven G. Kargl
3*42796049Sis  * All rights reserved.
4*42796049Sis  *
5*42796049Sis  * Redistribution and use in source and binary forms, with or without
6*42796049Sis  * modification, are permitted provided that the following conditions
7*42796049Sis  * are met:
8*42796049Sis  * 1. Redistributions of source code must retain the above copyright
9*42796049Sis  *    notice unmodified, this list of conditions, and the following
10*42796049Sis  *    disclaimer.
11*42796049Sis  * 2. Redistributions in binary form must reproduce the above copyright
12*42796049Sis  *    notice, this list of conditions and the following disclaimer in the
13*42796049Sis  *    documentation and/or other materials provided with the distribution.
14*42796049Sis  *
15*42796049Sis  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*42796049Sis  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*42796049Sis  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*42796049Sis  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*42796049Sis  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*42796049Sis  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*42796049Sis  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*42796049Sis  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*42796049Sis  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*42796049Sis  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*42796049Sis  */
26*42796049Sis 
27*42796049Sis #include <sys/cdefs.h>
28*42796049Sis #if defined(LIBM_SCCS) && !defined(lint)
29*42796049Sis __RCSID("$NetBSD: n_roundf.c,v 1.1 2006/01/17 13:16:08 is Exp $");
30*42796049Sis #if 0
31*42796049Sis __FBSDID("$FreeBSD: src/lib/msun/src/s_roundf.c,v 1.1 2004/06/07 08:05:36 das Exp $");
32*42796049Sis #endif
33*42796049Sis #endif
34*42796049Sis 
35*42796049Sis #include <math.h>
36*42796049Sis 
37*42796049Sis float
roundf(float x)38*42796049Sis roundf(float x)
39*42796049Sis {
40*42796049Sis 	float t;
41*42796049Sis 
42*42796049Sis 	if (x >= 0.0) {
43*42796049Sis 		t = ceilf(x);
44*42796049Sis 		if (t - x > 0.5)
45*42796049Sis 			t -= 1.0;
46*42796049Sis 		return (t);
47*42796049Sis 	} else {
48*42796049Sis 		t = ceilf(-x);
49*42796049Sis 		if (t + x > 0.5)
50*42796049Sis 			t -= 1.0;
51*42796049Sis 		return (-t);
52*42796049Sis 	}
53*42796049Sis }
54