From e2928bd73364b40a54537e46131cf3ce1eb52320 Mon Sep 17 00:00:00 2001 From: das Date: Mon, 7 Jun 2004 08:05:36 +0000 Subject: [PATCH] Add round(3) and roundf(3) and the associated documentation. PR: 59797 Submitted by: "Steven G. Kargl" Reviewed by: bde (earlier version, last year) --- lib/msun/Makefile | 6 ++-- lib/msun/man/round.3 | 71 +++++++++++++++++++++++++++++++++++++++++ lib/msun/src/math.h | 2 ++ lib/msun/src/s_round.c | 53 ++++++++++++++++++++++++++++++ lib/msun/src/s_roundf.c | 53 ++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 lib/msun/man/round.3 create mode 100644 lib/msun/src/s_round.c create mode 100644 lib/msun/src/s_roundf.c diff --git a/lib/msun/Makefile b/lib/msun/Makefile index e86ce212282a..910638ae363c 100644 --- a/lib/msun/Makefile +++ b/lib/msun/Makefile @@ -85,7 +85,8 @@ COMMON_SRCS= b_exp.c b_log.c b_tgamma.c \ s_floor.c s_floorf.c s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.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_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 \ w_acos.c w_acosf.c w_acosh.c w_acoshf.c w_asin.c w_asinf.c w_atan2.c \ @@ -127,7 +128,7 @@ INCS= fenv.h math.h 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 feclearexcept.3 fegetenv.3 \ fegetround.3 fenv.3 floor.3 fmod.3 hypot.3 ieee.3 \ - ieee_test.3 j0.3 lgamma.3 math.3 rint.3 sin.3 sinh.3 sqrt.3 \ + ieee_test.3 j0.3 lgamma.3 math.3 rint.3 round.3 sin.3 sinh.3 sqrt.3 \ tan.3 tanh.3 MLINKS+=acos.3 acosf.3 @@ -165,6 +166,7 @@ MLINKS+=j0.3 j1.3 j0.3 jn.3 j0.3 y0.3 j0.3 y1.3 j0.3 y1f.3 j0.3 yn.3 MLINKS+=j0.3 j0f.3 j0.3 j1f.3 j0.3 jnf.3 j0.3 y0f.3 j0.3 ynf.3 MLINKS+=lgamma.3 gamma.3 lgamma.3 gammaf.3 lgamma.3 lgammaf.3 lgamma.3 tgamma.3 MLINKS+=rint.3 rintf.3 +MLINKS+=round.3 roundf.3 MLINKS+=sin.3 sinf.3 MLINKS+=sinh.3 sinhf.3 MLINKS+=sqrt.3 cbrt.3 sqrt.3 cbrtf.3 sqrt.3 sqrtf.3 diff --git a/lib/msun/man/round.3 b/lib/msun/man/round.3 new file mode 100644 index 000000000000..66aa6b8a1779 --- /dev/null +++ b/lib/msun/man/round.3 @@ -0,0 +1,71 @@ +.\" 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$ +.\" +.Dd November 29, 2003 +.Dt ROUND 3 +.Os +.Sh NAME +.Nm round , +.Nm roundf +.Nd round to nearest integral value +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn round "double x" +.Ft float +.Fn roundf "float x" +.Sh DESCRIPTION +The +.Fn round +and +.Fn roundf +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 math 3 , +.Xr rint 3 +.Sh STANDARDS +The +.Fn round +function conforms to +.St -isoC-99 . +.Sh HISTORY +The +.Fn round +and +.Fn roundf +functions appeared in +.Fx 5.3 . diff --git a/lib/msun/src/math.h b/lib/msun/src/math.h index 116335b2a44a..7abb7099584f 100644 --- a/lib/msun/src/math.h +++ b/lib/msun/src/math.h @@ -215,6 +215,7 @@ double logb(double) __pure2; double nextafter(double, double); double remainder(double, double); double rint(double) __pure2; +double round(double); #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */ #if __BSD_VISIBLE || __XSI_VISIBLE @@ -292,6 +293,7 @@ float ceilf(float); float fabsf(float); float floorf(float); float fmodf(float, float); +float roundf(float); float erff(float); float erfcf(float) __pure2; diff --git a/lib/msun/src/s_round.c b/lib/msun/src/s_round.c new file mode 100644 index 000000000000..ea764ae2dbc6 --- /dev/null +++ b/lib/msun/src/s_round.c @@ -0,0 +1,53 @@ +/*- + * 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 +__FBSDID("$FreeBSD$"); + +#include + +double +round(double x) +{ + double t; + int i; + + i = fpclassify(x); + if (i == FP_INFINITE || i == FP_NAN) + return (x); + + if (x >= 0.0) { + t = ceil(x); + if (t - x > 0.5) + t -= 1.0; + return (t); + } else { + t = ceil(-x); + if (t + x > 0.5) + t -= 1.0; + return (-t); + } +} diff --git a/lib/msun/src/s_roundf.c b/lib/msun/src/s_roundf.c new file mode 100644 index 000000000000..a040e16aa77f --- /dev/null +++ b/lib/msun/src/s_roundf.c @@ -0,0 +1,53 @@ +/*- + * 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 +__FBSDID("$FreeBSD$"); + +#include + +float +roundf(float x) +{ + float t; + int i; + + i = fpclassify(x); + if (i == FP_INFINITE || i == FP_NAN) + return (x); + + if (x >= 0.0) { + t = ceilf(x); + if (t - x > 0.5) + t -= 1.0; + return (t); + } else { + t = ceilf(-x); + if (t + x > 0.5) + t -= 1.0; + return (-t); + } +}