difftime.c (15927) | difftime.c (17209) |
---|---|
1/* 2** This file is in the public domain, so clarified as of 3** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov). 4*/ |
|
1 | 5 |
2 | |
3#ifndef lint 4#ifndef NOID | 6#ifndef lint 7#ifndef NOID |
5static char elsieid[] = "@(#)difftime.c 7.4"; | 8static char elsieid[] = "@(#)difftime.c 7.7"; |
6#endif /* !defined NOID */ 7#endif /* !defined lint */ 8 9/*LINTLIBRARY*/ 10 11#include "private.h" 12 13/* --- 26 unchanged lines hidden (view full) --- 40 ** by computing the difference before converting to double. 41 */ 42 delta = time1 - time0; 43 if (delta >= 0) 44 return delta; 45 /* 46 ** Repair delta overflow. 47 */ | 9#endif /* !defined NOID */ 10#endif /* !defined lint */ 11 12/*LINTLIBRARY*/ 13 14#include "private.h" 15 16/* --- 26 unchanged lines hidden (view full) --- 43 ** by computing the difference before converting to double. 44 */ 45 delta = time1 - time0; 46 if (delta >= 0) 47 return delta; 48 /* 49 ** Repair delta overflow. 50 */ |
48 hibit = 1; 49 while ((hibit <<= 1) > 0) 50 continue; | 51 hibit = (~ (time_t) 0) << (TYPE_BIT(time_t) - 1); |
51 /* 52 ** The following expression rounds twice, which means 53 ** the result may not be the closest to the true answer. 54 ** For example, suppose time_t is 64-bit signed int, 55 ** long_double is IEEE 754 double with default rounding, 56 ** time1 = 9223372036854775807 and time0 = -1536. 57 ** Then the true difference is 9223372036854777343, 58 ** which rounds to 9223372036854777856 59 ** with a total error of 513. 60 ** But delta overflows to -9223372036854774273, 61 ** which rounds to -9223372036854774784, and correcting 62 ** this by subtracting 2 * (long_double) hibit 63 ** (i.e. by adding 2**64 = 18446744073709551616) 64 ** yields 9223372036854776832, which 65 ** rounds to 9223372036854775808 66 ** with a total error of 1535 instead. 67 ** This problem occurs only with very large differences. 68 ** It's too painful to fix this portably. 69 ** We are not alone in this problem; | 52 /* 53 ** The following expression rounds twice, which means 54 ** the result may not be the closest to the true answer. 55 ** For example, suppose time_t is 64-bit signed int, 56 ** long_double is IEEE 754 double with default rounding, 57 ** time1 = 9223372036854775807 and time0 = -1536. 58 ** Then the true difference is 9223372036854777343, 59 ** which rounds to 9223372036854777856 60 ** with a total error of 513. 61 ** But delta overflows to -9223372036854774273, 62 ** which rounds to -9223372036854774784, and correcting 63 ** this by subtracting 2 * (long_double) hibit 64 ** (i.e. by adding 2**64 = 18446744073709551616) 65 ** yields 9223372036854776832, which 66 ** rounds to 9223372036854775808 67 ** with a total error of 1535 instead. 68 ** This problem occurs only with very large differences. 69 ** It's too painful to fix this portably. 70 ** We are not alone in this problem; |
70 ** many C compilers round twice when converting | 71 ** some C compilers round twice when converting |
71 ** large unsigned types to small floating types, 72 ** so if time_t is unsigned the "return delta" above | 72 ** large unsigned types to small floating types, 73 ** so if time_t is unsigned the "return delta" above |
73 ** has the same double-rounding problem. | 74 ** has the same double-rounding problem with those compilers. |
74 */ 75 return delta - 2 * (long_double) hibit; 76} | 75 */ 76 return delta - 2 * (long_double) hibit; 77} |