From: Guy Harris Date: Mon, 14 Sep 2015 22:12:28 +0000 (-0700) Subject: Use double rather than float. X-Git-Tag: tcpdump-4.8.0~140 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/ff1df423b6a8cda80cef88efa8a3162abcfd0e6e Use double rather than float. That means we can fit a 32-bit integer into a floating-point value without losing precision. Also, put in explicit casts when we convert a floating-point value to an integral value, to suppress compiler warnings. --- diff --git a/print-ntp.c b/print-ntp.c index 908c5360..0ba62194 100644 --- a/print-ntp.c +++ b/print-ntp.c @@ -326,16 +326,16 @@ p_sfix(netdissect_options *ndo, { register int i; register int f; - register float ff; + register double ff; i = EXTRACT_16BITS(&sfp->int_part); f = EXTRACT_16BITS(&sfp->fraction); - ff = f / 65536.0f; /* shift radix point by 16 bits */ - f = ff * 1000000.0f; /* Treat fraction as parts per million */ + ff = f / 65536.0; /* shift radix point by 16 bits */ + f = (int)(ff * 1000000.0); /* Treat fraction as parts per million */ ND_PRINT((ndo, "%d.%06d", i, f)); } -#define FMAXINT (4294967296.0f) /* floating point rep. of MAXINT */ +#define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */ static void p_ntp_time(netdissect_options *ndo, @@ -344,15 +344,15 @@ p_ntp_time(netdissect_options *ndo, register int32_t i; register uint32_t uf; register uint32_t f; - register float ff; + register double ff; i = EXTRACT_32BITS(&lfp->int_part); uf = EXTRACT_32BITS(&lfp->fraction); ff = uf; - if (ff < 0.0f) /* some compilers are buggy */ + if (ff < 0.0) /* some compilers are buggy */ ff += FMAXINT; - ff = ff / FMAXINT; /* shift radix point by 32 bits */ - f = ff * 1000000000.0f; /* treat fraction as parts per billion */ + ff = ff / FMAXINT; /* shift radix point by 32 bits */ + f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */ ND_PRINT((ndo, "%u.%09d", i, f)); #ifdef HAVE_STRFTIME @@ -381,7 +381,7 @@ p_ntp_delta(netdissect_options *ndo, register uint32_t u, uf; register uint32_t ou, ouf; register uint32_t f; - register float ff; + register double ff; int signbit; u = EXTRACT_32BITS(&lfp->int_part); @@ -417,10 +417,10 @@ p_ntp_delta(netdissect_options *ndo, } ff = f; - if (ff < 0.0f) /* some compilers are buggy */ + if (ff < 0.0) /* some compilers are buggy */ ff += FMAXINT; - ff = ff / FMAXINT; /* shift radix point by 32 bits */ - f = ff * 1000000000.0f; /* treat fraction as parts per billion */ + ff = ff / FMAXINT; /* shift radix point by 32 bits */ + f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */ ND_PRINT((ndo, "%s%d.%09d", signbit ? "-" : "+", i, f)); }