xref: /original-bsd/lib/libm/common_source/rint.3 (revision 703f6d5d)
1.\" Copyright (c) 1985, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)rint.3	5.1 (Berkeley) 05/02/91
7.\"
8.Dd
9.Dt RINT 3
10.Os
11.Sh NAME
12.Nm rint
13.Nd and round-to-closest integer functions
14.Sh SYNOPSIS
15.Fd #include <math.h>
16.Ft double
17.Fn rint "double x"
18.Sh DESCRIPTION
19The
20.Fn rint
21function finds the integer (represented as a double precision number)
22nearest to
23.Fa x
24in the direction of the prevailing rounding mode.
25.Sh NOTES
26On a
27.Tn VAX ,
28.Fn rint x
29is equivalent to adding half to the magnitude
30and then rounding towards zero.
31.Pp
32In the default rounding mode, to nearest,
33on a machine that conforms to
34.Tn IEEE
35754,
36.Fn rint x
37is the integer nearest
38.Fa x
39with the additional stipulation
40that if
41.Li |rint(x)\-x|=1/2
42then
43.Fn rint x
44is even.
45Other rounding modes can make
46.Fn rint
47act like
48.Fn floor ,
49or like
50.Fn ceil ,
51or round towards zero.
52.Pp
53Another way to obtain an integer near
54.Fa x
55is to declare (in C)
56.Bd -literal -offset indent
57double x;\0\0\0\0 int k;\0\0\0\0k\0=\0x;
58.Ed
59.Pp
60Most C compilers round
61.Fa x
62towards 0 to get the integer
63.Fa k ,
64but
65some do otherwise.
66If in doubt, use
67.Fn floor ,
68.Fn ceil ,
69or
70.Fn rint
71first, whichever you intend.
72Also note that, if x is larger than
73.Fa k
74can accommodate, the value of
75.Fa k
76and the presence or absence of an integer overflow are hard to
77predict.
78.Sh SEE ALSO
79.Xr abs 3 ,
80.Xr fabs 3 ,
81.Xr ceil 3 ,
82.Xr floor 3 ,
83.Xr ieee 3 ,
84.Xr math 3
85.Sh HISTORY
86A
87.Fn rint
88function appeared in
89.At v6 .
90