|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
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
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
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
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.
23: */
24:
25: #ifndef lint
26: static char sccsid[] = "@(#)floor.c 5.6 (Berkeley) 6/1/90";
27: #endif /* not lint */
28:
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();
42:
43: /*
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.
49: */
50: double
51: floor(x)
52: double x;
53: {
54: double y;
55:
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: }
69: }
70:
71: double
72: ceil(x)
73: double x;
74: {
75: double y;
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: }
90: }
91:
92: #ifndef national /* rint() is in ./NATIONAL/support.s */
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: */
114: double
115: rint(x)
116: double x;
117: {
118: double s,t;
119: const double one = 1.0;
120:
121: #if !defined(vax)&&!defined(tahoe)
122: if (x != x) /* NaN */
123: return (x);
124: #endif /* !defined(vax)&&!defined(tahoe) */
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: }
131: #endif /* not national */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.