+ /* Not a single packet was received successfully. */
+ if (ctx->history_received == 0)
+ return NAN;
+
+ if (percentile <= 0.0)
+ index = 0;
+ else if (percentile >= 100.0)
+ index = ctx->history_received - 1;
+ else
+ {
+ index = (size_t) ceil ((percentile / 100.0) * ((double) ctx->history_received));
+ assert (index > 0);
+ index--;
+ }
+
+ return (ctx->history_by_value[index]);
+} /* }}} double percentile_to_latency */
+
+#if USE_NCURSES
+static double latency_to_ratio (ping_context_t *ctx, /* {{{ */
+ double latency)
+{
+ size_t low;
+ size_t high;
+ size_t index;
+
+ clean_history (ctx);
+
+ /* Not a single packet was received successfully. */
+ if (ctx->history_received == 0)
+ return NAN;
+
+ low = 0;
+ high = ctx->history_received - 1;
+
+ if (latency < ctx->history_by_value[low])
+ return 0.0;
+ else if (latency >= ctx->history_by_value[high])
+ return 100.0;
+
+ /* Do a binary search for the latency. This will work even when the
+ * exact latency is not in the array. If the latency is in the array
+ * multiple times, "low" will be set to the index of the last
+ * occurrence. The value at index "high" will be larger than the
+ * searched for latency (assured by the above "if" block. */
+ while ((high - low) > 1)
+ {
+ index = (high + low) / 2;