Commit | Line | Data |
---|---|---|
ff0114f0 KB |
1 | .\" Copyright (c) 1985, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
8552940a CL |
3 | .\" |
4 | .\" %sccs.include.redist.man% | |
5 | .\" | |
ff0114f0 | 6 | .\" @(#)rint.3 8.1 (Berkeley) %G% |
8552940a CL |
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 | |
19 | The | |
20 | .Fn rint | |
21 | function finds the integer (represented as a double precision number) | |
22 | nearest to | |
23 | .Fa x | |
24 | in the direction of the prevailing rounding mode. | |
25 | .Sh NOTES | |
26 | On a | |
27 | .Tn VAX , | |
28 | .Fn rint x | |
29 | is equivalent to adding half to the magnitude | |
30 | and then rounding towards zero. | |
31 | .Pp | |
32 | In the default rounding mode, to nearest, | |
33 | on a machine that conforms to | |
34 | .Tn IEEE | |
35 | 754, | |
36 | .Fn rint x | |
37 | is the integer nearest | |
38 | .Fa x | |
39 | with the additional stipulation | |
40 | that if | |
41 | .Li |rint(x)\-x|=1/2 | |
42 | then | |
43 | .Fn rint x | |
44 | is even. | |
45 | Other rounding modes can make | |
46 | .Fn rint | |
47 | act like | |
48 | .Fn floor , | |
49 | or like | |
50 | .Fn ceil , | |
51 | or round towards zero. | |
52 | .Pp | |
53 | Another way to obtain an integer near | |
54 | .Fa x | |
55 | is to declare (in C) | |
56 | .Bd -literal -offset indent | |
57 | double x;\0\0\0\0 int k;\0\0\0\0k\0=\0x; | |
58 | .Ed | |
59 | .Pp | |
60 | Most C compilers round | |
61 | .Fa x | |
62 | towards 0 to get the integer | |
63 | .Fa k , | |
64 | but | |
65 | some do otherwise. | |
66 | If in doubt, use | |
67 | .Fn floor , | |
68 | .Fn ceil , | |
69 | or | |
70 | .Fn rint | |
71 | first, whichever you intend. | |
72 | Also note that, if x is larger than | |
73 | .Fa k | |
74 | can accommodate, the value of | |
75 | .Fa k | |
76 | and the presence or absence of an integer overflow are hard to | |
77 | predict. | |
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 | |
86 | A | |
87 | .Fn rint | |
88 | function appeared in | |
89 | .At v6 . |