# # Use long double instead of double so it won't overflow if ping is run for # a couple of days. # # Reported by: @tifan via twitter # Index: ping.c =================================================================== --- ping.c (revision 229852) +++ ping.c (working copy) @@ -189,10 +189,10 @@ /* timing */ int timing; /* flag to do timing */ -double tmin = 999999999.0; /* minimum round trip time */ -double tmax = 0.0; /* maximum round trip time */ -double tsum = 0.0; /* sum of all times, for doing average */ -double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ +long double tmin = 999999999.0; /* minimum round trip time */ +long double tmax = 0.0; /* maximum round trip time */ +long double tsum = 0.0; /* sum of all times, for doing average */ +long double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ volatile sig_atomic_t siginfo_p; @@ -231,7 +231,7 @@ char *policy_in, *policy_out; #endif struct sockaddr_in *to; - double t; + long double t; u_long alarmtimeout, ultmp; int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno, tos, ttl; @@ -350,8 +350,8 @@ options |= F_MIF; break; case 'i': /* wait between sending packets */ - t = strtod(optarg, &ep) * 1000.0; - if (*ep || ep == optarg || t > (double)INT_MAX) + t = strtold(optarg, &ep) * 1000.0; + if (*ep || ep == optarg || t > (long double)INT_MAX) errx(EX_USAGE, "invalid timing interval: `%s'", optarg); options |= F_INTERVAL; @@ -471,8 +471,8 @@ options |= F_VERBOSE; break; case 'W': /* wait ms for answer */ - t = strtod(optarg, &ep); - if (*ep || ep == optarg || t > (double)INT_MAX) + t = strtold(optarg, &ep); + if (*ep || ep == optarg || t > (long double)INT_MAX) errx(EX_USAGE, "invalid timing interval: `%s'", optarg); options |= F_WAITTIME; @@ -1016,7 +1016,7 @@ struct icmp *icp; struct ip *ip; const void *tp; - double triptime; + long double triptime; int dupflag, hlen, i, j, recv_len, seq; static int old_rrlen; static char old_rr[MAX_IPOPTLEN]; @@ -1056,8 +1056,8 @@ tv1.tv_sec = ntohl(tv32.tv32_sec); tv1.tv_usec = ntohl(tv32.tv32_usec); tvsub(tv, &tv1); - triptime = ((double)tv->tv_sec) * 1000.0 + - ((double)tv->tv_usec) / 1000.0; + triptime = ((long double)tv->tv_sec) * 1000.0 + + ((long double)tv->tv_usec) / 1000.0; tsum += triptime; tsumsq += triptime * triptime; if (triptime < tmin) @@ -1095,7 +1095,7 @@ seq); (void)printf(" ttl=%d", ip->ip_ttl); if (timing) - (void)printf(" time=%.3f ms", triptime); + (void)printf(" time=%.3Lf ms", triptime); if (dupflag) (void)printf(" (DUP!)"); if (options & F_AUDIBLE) @@ -1349,7 +1349,7 @@ nreceived, ntransmitted, ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); if (nreceived && timing) - (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", + (void)fprintf(stderr, " %.3Lf min / %.3Lf avg / %.3Lf max", tmin, tsum / (nreceived + nrepeats), tmax); (void)fprintf(stderr, "\n"); } @@ -1384,12 +1384,12 @@ (void)printf(", %ld packets out of wait time", nrcvtimeout); (void)putchar('\n'); if (nreceived && timing) { - double n = nreceived + nrepeats; - double avg = tsum / n; - double vari = tsumsq / n - avg * avg; + long double n = nreceived + nrepeats; + long double avg = tsum / n; + long double vari = tsumsq / n - avg * avg; (void)printf( - "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", - tmin, avg, tmax, sqrt(vari)); + "round-trip min/avg/max/stddev = %.3Lf/%.3Lf/%.3Lf/%.3Lf ms\n", + tmin, avg, tmax, sqrtl(vari)); } if (nreceived)