Browse files

tvdiff_secs(): sub-zero time difference adjustment

Skip a floating point addition operation when integral part of time difference
is zero. This avoids potential floating point addition rounding problems while
preserving decimal part value.
  • Loading branch information...
1 parent 4a4d044 commit 97d7a9260e4f2f335b3632180877c25e0efdc8e3 @yangtse yangtse committed Nov 25, 2011
Showing with 15 additions and 6 deletions.
  1. +5 −2 lib/timeval.c
  2. +5 −2 src/tool_util.c
  3. +5 −2 tests/libtest/testutil.c
View
7 lib/timeval.c
@@ -120,8 +120,11 @@ long curlx_tvdiff(struct timeval newer, struct timeval older)
*/
double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
{
- return (double)(newer.tv_sec-older.tv_sec)+
- (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ if(newer.tv_sec != older.tv_sec)
+ return (double)(newer.tv_sec-older.tv_sec)+
+ (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ else
+ return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
/* return the number of seconds in the given input timeval struct */
View
7 src/tool_util.c
@@ -123,8 +123,11 @@ long tool_tvdiff(struct timeval newer, struct timeval older)
*/
double tool_tvdiff_secs(struct timeval newer, struct timeval older)
{
- return (double)(newer.tv_sec-older.tv_sec)+
- (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ if(newer.tv_sec != older.tv_sec)
+ return (double)(newer.tv_sec-older.tv_sec)+
+ (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ else
+ return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
/* return the number of seconds in the given input timeval struct */
View
7 tests/libtest/testutil.c
@@ -123,8 +123,11 @@ long tutil_tvdiff(struct timeval newer, struct timeval older)
*/
double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
{
- return (double)(newer.tv_sec-older.tv_sec)+
- (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ if(newer.tv_sec != older.tv_sec)
+ return (double)(newer.tv_sec-older.tv_sec)+
+ (double)(newer.tv_usec-older.tv_usec)/1000000.0;
+ else
+ return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
/* return the number of seconds in the given input timeval struct */

0 comments on commit 97d7a92

Please sign in to comment.