Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_printf | ||
4 | ** File description: | ||
5 | ** Flag to print a %g (a double) | ||
6 | */ | ||
7 | |||
8 | #include <stdarg.h> | ||
9 | #include <stddef.h> | ||
10 | #include <stdlib.h> | ||
11 | #include <stdio.h> | ||
12 | #include <stddef.h> | ||
13 | #include "my.h" | ||
14 | #include "myformats.h" | ||
15 | |||
16 | 24 | static int check_precision(int precision, int nb) | |
17 | { | ||
18 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | if (precision - my_strlen(my_str_nbr(ABS(nb))) <= 0) |
19 | ✗ | return 0; | |
20 | else | ||
21 | 24 | return precision - my_strlen(my_str_nbr(ABS(nb))); | |
22 | } | ||
23 | |||
24 | 33 | static int get_nb_zero(double nb) | |
25 | { | ||
26 | 33 | int index = 0; | |
27 | 33 | int temp_nb = 0; | |
28 | 33 | double temp_nb_double = nb; | |
29 | |||
30 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 12 times.
|
33 | if (ABS(nb) < 1) { |
31 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 21 times.
|
96 | while (temp_nb % 10 == 0) { |
32 | 75 | index = index + 1; | |
33 | 75 | temp_nb_double = temp_nb_double * 10; | |
34 | 75 | temp_nb = temp_nb_double; | |
35 | } | ||
36 | 21 | return index - 1; | |
37 | } | ||
38 | 12 | return 0; | |
39 | } | ||
40 | |||
41 | 6 | static void my_get_decimal_part(char *float_nb, double nb, | |
42 | formating_t *formating) | ||
43 | { | ||
44 | 12 | size_t precision_size = my_compute_power_rec_size_t(10, check_precision | |
45 | 6 | (formating->id_nb, nb) + get_nb_zero(nb)); | |
46 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | size_t nb_convert = ABS(nb * precision_size); |
47 | 6 | int i = 0; | |
48 | 6 | char temp[2] = ""; | |
49 | |||
50 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | if (nb_convert % precision_size != 0) |
51 | 5 | my_strcat(float_nb, "."); | |
52 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 6 times.
|
15 | for (i = 0; i < get_nb_zero(nb - (int)nb); i++) |
53 | 9 | my_strcat(float_nb, "0"); | |
54 |
2/2✓ Branch 2 taken 32 times.
✓ Branch 3 taken 6 times.
|
38 | for (int j = 0; j < my_strlen(my_str_nbr_long_long(nb_convert)); j++) { |
55 | 32 | temp[0] = my_str_nbr_long_long(nb_convert % precision_size)[j]; | |
56 | 32 | my_strcat(float_nb, temp); | |
57 | } | ||
58 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
|
6 | nb_convert = ABS(nb * my_compute_power_rec_size_t(10, check_precision |
59 | (formating->id_nb, nb) + get_nb_zero(nb) + 1)); | ||
60 | 6 | my_round_float_str(float_nb, 48 + (nb_convert % 10), | |
61 | 6 | my_strlen(float_nb) - 1, 0); | |
62 | 6 | } | |
63 | |||
64 | 6 | static char *my_get_str_float(double nb, formating_t *formating) | |
65 | { | ||
66 | 6 | char *float_nb = malloc(sizeof(char) * | |
67 | 6 | my_strlen(my_str_nbr(nb)) + 9 + formating->id_nb); | |
68 | 6 | int i = 0; | |
69 | |||
70 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | formating->id_nb = (formating->id_prc != -1) ? (formating->id_nb) : 6; |
71 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (nb < 0) |
72 | 3 | my_strcat(float_nb, "-"); | |
73 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | my_strcat(float_nb, my_str_nbr_long_long(ABS(nb))); |
74 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
6 | if (formating->id_nb > 0 && check_precision(formating->id_nb, nb) != 0) { |
75 | 6 | my_get_decimal_part(float_nb, nb, formating); | |
76 | 6 | i = my_strlen(float_nb) - 1; | |
77 |
4/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
|
21 | for (i; (float_nb[i] == '0' || float_nb[i] == '.') && i != 0; i--) |
78 | 15 | float_nb[i] = '\0'; | |
79 | } | ||
80 | 6 | my_strcat(float_nb, "\0"); | |
81 | 6 | return my_strdup(float_nb); | |
82 | } | ||
83 | |||
84 | 2 | static char *my_get_power(int index_nb) | |
85 | { | ||
86 | char *power; | ||
87 | |||
88 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (ABS(index_nb) >= 100) { |
89 | 1 | power = malloc(sizeof(char) * 5); | |
90 | } else { | ||
91 | 1 | power = malloc(sizeof(char) * 4); | |
92 | } | ||
93 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (index_nb >= 0) { |
94 | 1 | my_strcat(power, "+"); | |
95 | } else { | ||
96 | 1 | my_strcat(power, "-"); | |
97 | } | ||
98 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | if (ABS(index_nb) < 10) |
99 | 1 | my_strcat(power, "0"); | |
100 | 2 | my_strcat(power, my_str_nbr(ABS(index_nb))); | |
101 | 2 | my_strcat(power, "\0"); | |
102 | 2 | return power; | |
103 | } | ||
104 | |||
105 | 2 | static void condition(double nb, int *index_nb, | |
106 | double *temp_nb_double, int *temp_nb) | ||
107 | { | ||
108 |
4/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | if (ABS(nb) > 1) { |
109 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 1 times.
|
7 | for (int i = 0; i < my_strlen(my_str_nbr(ABS(nb))) - 1; i++) { |
110 | 6 | *index_nb = *index_nb + 1; | |
111 | 6 | *temp_nb_double = *temp_nb_double / 10; | |
112 | } | ||
113 | } else { | ||
114 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
|
101 | while (*temp_nb % 10 == 0) { |
115 | 100 | *index_nb = *index_nb - 1; | |
116 | 100 | *temp_nb_double = *temp_nb_double * 10; | |
117 | 100 | *temp_nb = *temp_nb_double; | |
118 | } | ||
119 | } | ||
120 | 2 | } | |
121 | |||
122 | 2 | static char *my_get_str_float_scientific(double nb, formating_t *formating) | |
123 | { | ||
124 | char *float_nb; | ||
125 | 2 | int signe = 0; | |
126 | 2 | int index_nb = 0; | |
127 | 2 | int temp_nb = nb; | |
128 | 2 | double temp_nb_double = nb; | |
129 | |||
130 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (nb < 0) |
131 | 1 | signe = 1; | |
132 | 2 | float_nb = malloc(sizeof(char) * (13 + signe)); | |
133 | 2 | condition(nb, &index_nb, &temp_nb_double, &temp_nb); | |
134 | 2 | my_strcat(float_nb, my_get_str_float(temp_nb_double, formating)); | |
135 | 2 | my_strcat(float_nb, "E"); | |
136 | 2 | my_strcat(float_nb, my_get_power(index_nb)); | |
137 | 2 | return my_strdup(float_nb); | |
138 | } | ||
139 | |||
140 | 14 | static int my_get_index(double nb) | |
141 | { | ||
142 | 14 | int index_nb = 0; | |
143 | 14 | int temp_nb = nb; | |
144 | 14 | double temp_nb_double = nb; | |
145 | |||
146 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (nb == 0.0) |
147 | 2 | return 0; | |
148 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
|
12 | if (ABS(nb) > 1) { |
149 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | return my_strlen(my_str_nbr(ABS(nb))); |
150 | } else { | ||
151 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 6 times.
|
214 | while (temp_nb % 10 == 0) { |
152 | 208 | index_nb = index_nb - 1; | |
153 | 208 | temp_nb_double = temp_nb_double * 10; | |
154 | 208 | temp_nb = temp_nb_double; | |
155 | } | ||
156 | } | ||
157 | 6 | return index_nb; | |
158 | } | ||
159 | |||
160 | 7 | int flag_bigg(va_list list, formating_t *formating) | |
161 | { | ||
162 | 7 | double temp_double = va_arg(list, double); | |
163 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 4 times.
|
7 | int index = ABS(my_get_index(temp_double)); |
164 | char *temp; | ||
165 | |||
166 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
|
7 | if (temp_double == 0.0) { |
167 | 1 | temp = "0"; | |
168 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
6 | } else if (index - 3 < 4 && (index <= formating->id_nb || |
169 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | formating->id_nb == -1)) { |
170 | 4 | temp = my_get_str_float(temp_double, formating); | |
171 | } else { | ||
172 | 2 | temp = my_get_str_float_scientific(temp_double, formating); | |
173 | } | ||
174 | 7 | format_it_double(temp, formating, temp_double); | |
175 | 7 | my_putstrf(formating->fd, temp); | |
176 | 7 | return my_strlen(temp); | |
177 | } | ||
178 |