Commit | Line | Data |
---|---|---|
b6be2d90 KB |
1 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | |
e2481ef1 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
1c15e888 C |
6 | * provided that: (1) source distributions retain this entire copyright |
7 | * notice and comment, and (2) distributions including binaries display | |
8 | * the following acknowledgement: ``This product includes software | |
9 | * developed by the University of California, Berkeley and its contributors'' | |
10 | * in the documentation or other materials provided with the distribution | |
11 | * and in all advertising materials mentioning features or use of this | |
12 | * software. Neither the name of the University nor the names of its | |
13 | * contributors may be used to endorse or promote products derived | |
a399f6c8 KB |
14 | * from this software without specific prior written permission. |
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
1c15e888 | 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
e2481ef1 KB |
18 | * |
19 | * All recipients should regard themselves as participants in an ongoing | |
20 | * research project and hence should feel obligated to report their | |
21 | * experiences (good or bad) with these elementary function codes, using | |
22 | * the sendbug(8) program, to the authors. | |
b6be2d90 KB |
23 | */ |
24 | ||
25 | #ifndef lint | |
1c15e888 | 26 | static char sccsid[] = "@(#)floor.c 5.6 (Berkeley) 6/1/90"; |
b6be2d90 | 27 | #endif /* not lint */ |
2db37043 | 28 | |
9eda3584 KB |
29 | #include "mathimpl.h" |
30 | ||
31 | vc(L, 4503599627370496.0E0 ,0000,5c00,0000,0000, 55, 1.0) /* 2**55 */ | |
32 | ||
33 | ic(L, 4503599627370496.0E0, 52, 1.0) /* 2**52 */ | |
34 | ||
35 | #ifdef vccast | |
36 | #define L vccast(L) | |
37 | #endif | |
38 | ||
39 | ||
40 | double ceil(); | |
41 | double floor(); | |
e2481ef1 | 42 | |
2db37043 | 43 | /* |
e2481ef1 KB |
44 | * floor(x) := the largest integer no larger than x; |
45 | * ceil(x) := -floor(-x), for all real x. | |
46 | * | |
47 | * Note: Inexact will be signaled if x is not an integer, as is | |
48 | * customary for IEEE 754. No other signal can be emitted. | |
2db37043 | 49 | */ |
2db37043 | 50 | double |
e2481ef1 KB |
51 | floor(x) |
52 | double x; | |
2db37043 | 53 | { |
9eda3584 | 54 | double y; |
2db37043 | 55 | |
e2481ef1 KB |
56 | if ( |
57 | #if !defined(vax)&&!defined(tahoe) | |
58 | x != x || /* NaN */ | |
59 | #endif /* !defined(vax)&&!defined(tahoe) */ | |
60 | x >= L) /* already an even integer */ | |
61 | return x; | |
62 | else if (x < (double)0) | |
63 | return -ceil(-x); | |
64 | else { /* now 0 <= x < L */ | |
65 | y = L+x; /* destructive store must be forced */ | |
66 | y -= L; /* an integer, and |x-y| < 1 */ | |
67 | return x < y ? y-(double)1 : y; | |
68 | } | |
2db37043 ZAL |
69 | } |
70 | ||
71 | double | |
e2481ef1 KB |
72 | ceil(x) |
73 | double x; | |
2db37043 | 74 | { |
9eda3584 | 75 | double y; |
e2481ef1 KB |
76 | |
77 | if ( | |
78 | #if !defined(vax)&&!defined(tahoe) | |
79 | x != x || /* NaN */ | |
80 | #endif /* !defined(vax)&&!defined(tahoe) */ | |
81 | x >= L) /* already an even integer */ | |
82 | return x; | |
83 | else if (x < (double)0) | |
84 | return -floor(-x); | |
85 | else { /* now 0 <= x < L */ | |
86 | y = L+x; /* destructive store must be forced */ | |
87 | y -= L; /* an integer, and |x-y| < 1 */ | |
88 | return x > y ? y+(double)1 : y; | |
89 | } | |
2db37043 ZAL |
90 | } |
91 | ||
859dc438 | 92 | #ifndef national /* rint() is in ./NATIONAL/support.s */ |
2db37043 ZAL |
93 | /* |
94 | * algorithm for rint(x) in pseudo-pascal form ... | |
95 | * | |
96 | * real rint(x): real x; | |
97 | * ... delivers integer nearest x in direction of prevailing rounding | |
98 | * ... mode | |
99 | * const L = (last consecutive integer)/2 | |
100 | * = 2**55; for VAX D | |
101 | * = 2**52; for IEEE 754 Double | |
102 | * real s,t; | |
103 | * begin | |
104 | * if x != x then return x; ... NaN | |
105 | * if |x| >= L then return x; ... already an integer | |
106 | * s := copysign(L,x); | |
107 | * t := x + s; ... = (x+s) rounded to integer | |
108 | * return t - s | |
109 | * end; | |
110 | * | |
111 | * Note: Inexact will be signaled if x is not an integer, as is | |
112 | * customary for IEEE 754. No other signal can be emitted. | |
113 | */ | |
2db37043 ZAL |
114 | double |
115 | rint(x) | |
116 | double x; | |
117 | { | |
9eda3584 KB |
118 | double s,t; |
119 | const double one = 1.0; | |
120 | ||
859dc438 | 121 | #if !defined(vax)&&!defined(tahoe) |
2db37043 ZAL |
122 | if (x != x) /* NaN */ |
123 | return (x); | |
859dc438 | 124 | #endif /* !defined(vax)&&!defined(tahoe) */ |
2db37043 ZAL |
125 | if (copysign(x,one) >= L) /* already an integer */ |
126 | return (x); | |
127 | s = copysign(L,x); | |
128 | t = x + s; /* x+s rounded to integer */ | |
129 | return (t - s); | |
130 | } | |
859dc438 | 131 | #endif /* not national */ |