161309Sbostic.\" Copyright (c) 1985, 1991, 1993 261309Sbostic.\" The Regents of the University of California. All rights reserved. 348923Scael.\" 448923Scael.\" %sccs.include.redist.man% 548923Scael.\" 6*65099Smckusick.\" @(#)rint.3 8.2 (Berkeley) 12/11/93 748923Scael.\" 848923Scael.Dd 948923Scael.Dt RINT 3 1048923Scael.Os 1148923Scael.Sh NAME 1248923Scael.Nm rint 13*65099Smckusick.Nd round-to-closest integer functions 1448923Scael.Sh SYNOPSIS 1548923Scael.Fd #include <math.h> 1648923Scael.Ft double 1748923Scael.Fn rint "double x" 1848923Scael.Sh DESCRIPTION 1948923ScaelThe 2048923Scael.Fn rint 2148923Scaelfunction finds the integer (represented as a double precision number) 2248923Scaelnearest to 2348923Scael.Fa x 2448923Scaelin the direction of the prevailing rounding mode. 2548923Scael.Sh NOTES 2648923ScaelOn a 2748923Scael.Tn VAX , 2848923Scael.Fn rint x 2948923Scaelis equivalent to adding half to the magnitude 3048923Scaeland then rounding towards zero. 3148923Scael.Pp 3248923ScaelIn the default rounding mode, to nearest, 3348923Scaelon a machine that conforms to 3448923Scael.Tn IEEE 3548923Scael754, 3648923Scael.Fn rint x 3748923Scaelis the integer nearest 3848923Scael.Fa x 3948923Scaelwith the additional stipulation 4048923Scaelthat if 4148923Scael.Li |rint(x)\-x|=1/2 4248923Scaelthen 4348923Scael.Fn rint x 4448923Scaelis even. 4548923ScaelOther rounding modes can make 4648923Scael.Fn rint 4748923Scaelact like 4848923Scael.Fn floor , 4948923Scaelor like 5048923Scael.Fn ceil , 5148923Scaelor round towards zero. 5248923Scael.Pp 5348923ScaelAnother way to obtain an integer near 5448923Scael.Fa x 5548923Scaelis to declare (in C) 5648923Scael.Bd -literal -offset indent 5748923Scaeldouble x;\0\0\0\0 int k;\0\0\0\0k\0=\0x; 5848923Scael.Ed 5948923Scael.Pp 6048923ScaelMost C compilers round 6148923Scael.Fa x 6248923Scaeltowards 0 to get the integer 6348923Scael.Fa k , 6448923Scaelbut 6548923Scaelsome do otherwise. 6648923ScaelIf in doubt, use 6748923Scael.Fn floor , 6848923Scael.Fn ceil , 6948923Scaelor 7048923Scael.Fn rint 7148923Scaelfirst, whichever you intend. 7248923ScaelAlso note that, if x is larger than 7348923Scael.Fa k 7448923Scaelcan accommodate, the value of 7548923Scael.Fa k 7648923Scaeland the presence or absence of an integer overflow are hard to 7748923Scaelpredict. 7848923Scael.Sh SEE ALSO 7948923Scael.Xr abs 3 , 8048923Scael.Xr fabs 3 , 8148923Scael.Xr ceil 3 , 8248923Scael.Xr floor 3 , 8348923Scael.Xr ieee 3 , 8448923Scael.Xr math 3 8548923Scael.Sh HISTORY 8648923ScaelA 8748923Scael.Fn rint 8848923Scaelfunction appeared in 8948923Scael.At v6 . 90