s_roundf.c revision 259065
198944Sobrien/*-
298944Sobrien * Copyright (c) 2003, Steven G. Kargl
398944Sobrien * All rights reserved.
498944Sobrien *
598944Sobrien * Redistribution and use in source and binary forms, with or without
698944Sobrien * modification, are permitted provided that the following conditions
798944Sobrien * are met:
898944Sobrien * 1. Redistributions of source code must retain the above copyright
998944Sobrien *    notice unmodified, this list of conditions, and the following
1098944Sobrien *    disclaimer.
1198944Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1298944Sobrien *    notice, this list of conditions and the following disclaimer in the
1398944Sobrien *    documentation and/or other materials provided with the distribution.
1498944Sobrien *
1598944Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1698944Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1798944Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1898944Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1998944Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2098944Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2198944Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2298944Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23130803Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2498944Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2598944Sobrien */
2698944Sobrien
2798944Sobrien#include <sys/cdefs.h>
2898944Sobrien__FBSDID("$FreeBSD: releng/10.0/lib/msun/src/s_roundf.c 153017 2005-12-02 13:45:06Z bde $");
2998944Sobrien
3098944Sobrien#include <math.h>
3198944Sobrien
3298944Sobrienfloat
3398944Sobrienroundf(float x)
3498944Sobrien{
3598944Sobrien	float t;
3698944Sobrien
3798944Sobrien	if (!isfinite(x))
3898944Sobrien		return (x);
3998944Sobrien
4098944Sobrien	if (x >= 0.0) {
4198944Sobrien		t = floorf(x);
4298944Sobrien		if (t - x <= -0.5)
4398944Sobrien			t += 1.0;
4498944Sobrien		return (t);
4598944Sobrien	} else {
4698944Sobrien		t = floorf(-x);
4798944Sobrien		if (t + x <= -0.5)
4898944Sobrien			t += 1.0;
4998944Sobrien		return (-t);
5098944Sobrien	}
5198944Sobrien}
5298944Sobrien