diff options
author | Brad Smith <brad@cvs.openbsd.org> | 2006-07-12 07:26:09 +0000 |
---|---|---|
committer | Brad Smith <brad@cvs.openbsd.org> | 2006-07-12 07:26:09 +0000 |
commit | 5399afb75ee6826d6ce3b48287de19206aa34372 (patch) | |
tree | 763605756be6d94673eb22c5e8b04571a14e3427 | |
parent | f33bdfeb58fcadc2630c82d595c4d8ddebbac1a4 (diff) |
add some more C99 functions: round(3) and roundf(3).
By Steven G. Kargl <kargl at troutmask dot apl dot washington.edu>
From FreeBSD
-rw-r--r-- | include/math.h | 4 | ||||
-rw-r--r-- | lib/libm/Makefile | 11 | ||||
-rw-r--r-- | lib/libm/man/round.3 | 68 | ||||
-rw-r--r-- | lib/libm/man/trunc.3 | 6 | ||||
-rw-r--r-- | lib/libm/shlib_version | 2 | ||||
-rw-r--r-- | lib/libm/src/s_round.c | 51 | ||||
-rw-r--r-- | lib/libm/src/s_roundf.c | 51 |
7 files changed, 183 insertions, 10 deletions
diff --git a/include/math.h b/include/math.h index e7073e3d445..75518ac78e2 100644 --- a/include/math.h +++ b/include/math.h @@ -1,4 +1,4 @@ -/* $OpenBSD: math.h,v 1.13 2006/07/12 06:43:31 brad Exp $ */ +/* $OpenBSD: math.h,v 1.14 2006/07/12 07:26:07 brad Exp $ */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. @@ -141,6 +141,7 @@ extern double fabs(double); extern double floor(double); extern double fmod(double, double); +extern double round(double); extern double trunc(double); #if __BSD_VISIBLE || __XPG_VISIBLE @@ -236,6 +237,7 @@ extern float fabsf(float); extern float floorf(float); extern float fmodf(float, float); +extern float roundf(float); extern float truncf(float); extern float erff(float); diff --git a/lib/libm/Makefile b/lib/libm/Makefile index cd0a0799563..617cc78cafe 100644 --- a/lib/libm/Makefile +++ b/lib/libm/Makefile @@ -1,5 +1,5 @@ # $NetBSD: Makefile,v 1.28 1995/11/20 22:06:19 jtc Exp $ -# $OpenBSD: Makefile,v 1.39 2006/03/19 00:01:04 kettenis Exp $ +# $OpenBSD: Makefile,v 1.40 2006/07/12 07:26:08 brad Exp $ # # @(#)Makefile 5.1beta 93/09/24 # @@ -116,9 +116,9 @@ COMMON_SRCS = e_acos.c e_acosf.c e_acosh.c e_acoshf.c e_asin.c e_asinf.c \ s_floor.c s_floorf.c s_frexpf.c s_ilogb.c s_ilogbf.c \ s_isinff.c s_isnanf.c s_ldexpf.c s_lib_version.c s_log1p.c \ s_log1pf.c s_logb.c s_logbf.c s_matherr.c s_modff.c \ - s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c s_scalbn.c s_scalbnf.c \ - s_signgam.c s_significand.c s_significandf.c s_sin.c s_sinf.c s_tan.c \ - s_tanf.c s_tanh.c s_tanhf.c s_trunc.c s_truncf.c \ + s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c s_round.c s_roundf.c \ + s_scalbn.c s_scalbnf.c s_signgam.c s_significand.c s_significandf.c \ + s_sin.c s_sinf.c s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_trunc.c s_truncf.c \ w_acos.c w_acosf.c w_acosh.c w_acoshf.c w_asin.c w_asinf.c w_atan2.c \ w_atan2f.c w_atanh.c w_atanhf.c w_cabs.c w_cabsf.c w_cosh.c w_coshf.c \ w_drem.c w_dremf.c w_exp.c w_expf.c w_fmod.c w_fmodf.c w_gamma.c \ @@ -161,7 +161,7 @@ SRCS= ${COMMON_SRCS} MAN+= acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 ceil.3 \ cos.3 cosh.3 erf.3 exp.3 fabs.3 floor.3 fmod.3 hypot.3 ieee.3 \ infnan.3 logb.3 j0.3 lgamma.3 lrint.3 lround.3 math.3 rint.3 \ - sin.3 sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 + round.3 sin.3 sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 MLINKS+=erf.3 erfc.3 MLINKS+=exp.3 expm1.3 exp.3 log.3 exp.3 log10.3 exp.3 log1p.3 exp.3 pow.3 @@ -224,6 +224,7 @@ MLINKS+=lround.3 llround.3 MLINKS+=lround.3 llroundf.3 MLINKS+=math.3 mathf.3 MLINKS+=rint.3 rintf.3 +MLINKS+=round.3 roundf.3 MLINKS+=sin.3 sinf.3 MLINKS+=sinh.3 sinhf.3 MLINKS+=sqrt.3 cbrtf.3 diff --git a/lib/libm/man/round.3 b/lib/libm/man/round.3 new file mode 100644 index 00000000000..632f7a8e628 --- /dev/null +++ b/lib/libm/man/round.3 @@ -0,0 +1,68 @@ +.\" $OpenBSD: round.3,v 1.1 2006/07/12 07:26:08 brad Exp $ +.\" Copyright (c) 2003, Steven G. Kargl +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD: src/lib/msun/man/round.3,v 1.6 2005/06/15 19:04:04 ru Exp $ +.\" +.Dd April 7, 2005 +.Dt ROUND 3 +.Os +.Sh NAME +.Nm round , +.Nm roundf +.\" .Nm roundl +.Nd round to nearest integral value +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn round "double x" +.Ft float +.Fn roundf "float x" +.\" .Ft "long double" +.\" .Fn roundl "long double x" +.Sh DESCRIPTION +The +.Fn round +and +.Fn roundf +.\" .Fn roundl +functions return the nearest integral value to +.Fa x ; +if +.Fa x +lies halfway between two integral values, then these +functions return the integral value with the larger +absolute value (i.e., they round away from zero). +.Sh SEE ALSO +.Xr ceil 3 , +.Xr floor 3 , +.Xr ieee 3 , +.Xr lrint 3 , +.Xr lround 3 , +.Xr math 3 , +.Xr rint 3 , +.Xr trunc 3 +.Sh STANDARDS +These functions conform to +.St -isoC-99 . diff --git a/lib/libm/man/trunc.3 b/lib/libm/man/trunc.3 index bbf3083369e..5e07d715eeb 100644 --- a/lib/libm/man/trunc.3 +++ b/lib/libm/man/trunc.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: trunc.3,v 1.1 2006/03/18 20:13:24 brad Exp $ +.\" $OpenBSD: trunc.3,v 1.2 2006/07/12 07:26:08 brad Exp $ .\" .\" Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.org> .\" All rights reserved. @@ -65,8 +65,8 @@ rounding mode. .Xr floor 3 , .Xr math 3 , .Xr nextafter 3 , -.Xr rint 3 -.\" .Xr round 3 +.Xr rint 3 , +.Xr round 3 .Sh STANDARDS The .Fn trunc diff --git a/lib/libm/shlib_version b/lib/libm/shlib_version index ba5a3fee584..b363be4447c 100644 --- a/lib/libm/shlib_version +++ b/lib/libm/shlib_version @@ -1,2 +1,2 @@ major=2 -minor=2 +minor=3 diff --git a/lib/libm/src/s_round.c b/lib/libm/src/s_round.c new file mode 100644 index 00000000000..cb2db491cbd --- /dev/null +++ b/lib/libm/src/s_round.c @@ -0,0 +1,51 @@ +/* $OpenBSD: s_round.c,v 1.1 2006/07/12 07:26:08 brad Exp $ */ + +/*- + * Copyright (c) 2003, Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math.h" +#include "math_private.h" + +double +round(double x) +{ + double t; + + if (isinf(x) || isnan(x)) + return (x); + + if (x >= 0.0) { + t = floor(x); + if (t - x <= -0.5) + t += 1.0; + return (t); + } else { + t = floor(-x); + if (t + x <= -0.5) + t += 1.0; + return (-t); + } +} diff --git a/lib/libm/src/s_roundf.c b/lib/libm/src/s_roundf.c new file mode 100644 index 00000000000..f56d2187875 --- /dev/null +++ b/lib/libm/src/s_roundf.c @@ -0,0 +1,51 @@ +/* $OpenBSD: s_roundf.c,v 1.1 2006/07/12 07:26:08 brad Exp $ */ + +/*- + * Copyright (c) 2003, Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math.h" +#include "math_private.h" + +float +roundf(float x) +{ + float t; + + if (isinff(x) || isnanf(x)) + return (x); + + if (x >= 0.0) { + t = floorf(x); + if (t - x <= -0.5) + t += 1.0; + return (t); + } else { + t = floorf(-x); + if (t + x <= -0.5) + t += 1.0; + return (-t); + } +} |