1*2f2c0062Sguenther /* $OpenBSD: s_roundl.c,v 1.3 2016/09/12 19:47:02 guenther Exp $ */
249393c00Smartynas
349393c00Smartynas /*-
449393c00Smartynas * Copyright (c) 2003, Steven G. Kargl
549393c00Smartynas * All rights reserved.
649393c00Smartynas *
749393c00Smartynas * Redistribution and use in source and binary forms, with or without
849393c00Smartynas * modification, are permitted provided that the following conditions
949393c00Smartynas * are met:
1049393c00Smartynas * 1. Redistributions of source code must retain the above copyright
1149393c00Smartynas * notice unmodified, this list of conditions, and the following
1249393c00Smartynas * disclaimer.
1349393c00Smartynas * 2. Redistributions in binary form must reproduce the above copyright
1449393c00Smartynas * notice, this list of conditions and the following disclaimer in the
1549393c00Smartynas * documentation and/or other materials provided with the distribution.
1649393c00Smartynas *
1749393c00Smartynas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1849393c00Smartynas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1949393c00Smartynas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2049393c00Smartynas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2149393c00Smartynas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2249393c00Smartynas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2349393c00Smartynas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2449393c00Smartynas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2549393c00Smartynas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2649393c00Smartynas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2749393c00Smartynas */
2849393c00Smartynas
2949393c00Smartynas #include <math.h>
3049393c00Smartynas
3149393c00Smartynas long double
roundl(long double x)3249393c00Smartynas roundl(long double x)
3349393c00Smartynas {
3449393c00Smartynas long double t;
3549393c00Smartynas
3649393c00Smartynas if (!isfinite(x))
3749393c00Smartynas return (x);
3849393c00Smartynas
3949393c00Smartynas if (x >= 0.0) {
4049393c00Smartynas t = floorl(x);
4149393c00Smartynas if (t - x <= -0.5)
4249393c00Smartynas t += 1.0;
4349393c00Smartynas return (t);
4449393c00Smartynas } else {
4549393c00Smartynas t = floorl(-x);
4649393c00Smartynas if (t + x <= -0.5)
4749393c00Smartynas t += 1.0;
4849393c00Smartynas return (-t);
4949393c00Smartynas }
5049393c00Smartynas }
51*2f2c0062Sguenther DEF_STD(roundl);
52