Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/top/dist Be more careful/explicit with FP round...



details:   https://anonhg.NetBSD.org/src/rev/1fdb5862908a
branches:  trunk
changeset: 817629:1fdb5862908a
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sat Aug 27 18:48:30 2016 +0000

description:
Be more careful/explicit with FP rounding when converting floating time
to timeval. Also, don't truncate the seconds part to int for y2038.

I've had this patch sitting around since 2010 and I completely forget
what motivated it.

diffstat:

 external/bsd/top/dist/utils.c |  8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

diffs (25 lines):

diff -r e282a3731c00 -r 1fdb5862908a external/bsd/top/dist/utils.c
--- a/external/bsd/top/dist/utils.c     Sat Aug 27 16:17:16 2016 +0000
+++ b/external/bsd/top/dist/utils.c     Sat Aug 27 18:48:30 2016 +0000
@@ -41,6 +41,7 @@
 
 #include "os.h"
 #include <ctype.h>
+#include <math.h>
 #ifdef HAVE_STDARG_H
 #include <stdarg.h>
 #else
@@ -711,8 +712,11 @@
 void
 double2tv(struct timeval *tv, double d)
 {
-    tv->tv_sec = (int)d;
-    tv->tv_usec = (d - tv->tv_sec) * 1000000;
+    double di;
+
+    di = floor(d);
+    tv->tv_sec = (time_t)di;
+    tv->tv_usec = (int)ceil((d - di) * 1000000.0);
 }
 
 static int debug_on = 0;



Home | Main Index | Thread Index | Old Index