fix compilation error and fix stddev calculation

This commit is contained in:
2025-05-02 23:57:11 +02:00
parent e3a2901370
commit b96652f9a4
3 changed files with 8 additions and 8 deletions

View File

@ -1,6 +1,6 @@
NAME = ft_ping NAME = ft_ping
CC=cc CC=cc
FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result -lm FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result
SRCS_DIR = srcs SRCS_DIR = srcs
OBJS_DIR = .objs OBJS_DIR = .objs
@ -18,7 +18,7 @@ OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))
all: $(NAME) all: $(NAME)
$(NAME): $(OBJS_DIR) $(OBJS) $(NAME): $(OBJS_DIR) $(OBJS)
$(CC) $(FLAGS) $(OBJS) -o $@ $(CC) $(FLAGS) $(OBJS) -o $@ -lm
$(OBJS_DIR): $(OBJS_DIR):
mkdir $(OBJS_DIR) mkdir $(OBJS_DIR)

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */ /* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */
/* Updated: 2025/05/02 01:04:29 by tomoron ### ########.fr */ /* Updated: 2025/05/02 23:55:50 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,6 +50,8 @@ void ping_end_print(t_settings *set, char *host, t_stats *stats)
(void)set; (void)set;
stddev = sqrt(stats->sqr_diff / (stats->received - 1)); stddev = sqrt(stats->sqr_diff / (stats->received - 1));
if(isnan(stddev) || isinf(stddev))
stddev = 0.0f;
printf("--- %s ping statistics ---\n", host); printf("--- %s ping statistics ---\n", host);
percent = ((double)stats->sent / 100) * stats->received; percent = ((double)stats->sent / 100) * stats->received;
printf("%lu packets transmitted, %lu packets received, %d%% packet loss\n", stats->sent, stats->received, percent); printf("%lu packets transmitted, %lu packets received, %d%% packet loss\n", stats->sent, stats->received, percent);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/01 17:41:42 by tomoron #+# #+# */ /* Created: 2025/05/01 17:41:42 by tomoron #+# #+# */
/* Updated: 2025/05/02 01:03:15 by tomoron ### ########.fr */ /* Updated: 2025/05/02 23:53:28 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,14 +18,12 @@ void update_stats(t_stats *stats, double time)
stats->received++; stats->received++;
tmp = 0; tmp = 0;
if(stats->prev)
tmp = time - stats->avg; tmp = time - stats->avg;
stats->min = MIN(stats->min, time); stats->min = MIN(stats->min, time);
if(stats->min == 0) if(stats->min == 0)
stats->min = time; stats->min = time;
stats->max = MAX(stats->max, time); stats->max = MAX(stats->max, time);
stats->avg += (time - stats->avg) / stats->received; stats->avg += (time - stats->avg) / stats->received;
if(stats->prev)
stats->sqr_diff += tmp * stats->avg; stats->sqr_diff += tmp * stats->avg;
stats->prev = time; stats->prev = time;
} }