-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_putfloat_g_bonus.c
70 lines (63 loc) · 2.48 KB
/
ft_printf_putfloat_g_bonus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_putfloat_g_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnakano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/15 18:36:42 by dnakano #+# #+# */
/* Updated: 2020/10/18 12:56:21 by dnakano ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include "libftprintf.h"
static int ft_putinstyle_f(t_float iflt, t_printf_flags *flags)
{
int e;
const t_float iflt_o = iflt;
e = ft_printf_putfloat_e_get(iflt, flags);
if (flags->precision >= e + 1)
flags->precision -= (e + 1);
if (flags->flag & FLAG_ALTERNATE)
return (ft_printf_putfloat_f(iflt_o, flags));
if (flags->precision >= FLT_MTSSIZE)
flags->precision = FLT_MTSSIZE - 1;
ft_float_round(&iflt, -flags->precision);
while (flags->precision > 0 && !iflt.mts_dec[flags->precision - 1])
flags->precision--;
return (ft_printf_putfloat_f(iflt_o, flags));
}
static int ft_putinstyle_e(t_float iflt, t_printf_flags *flags, int e)
{
const t_float iflt_o = iflt;
flags->precision--;
if (flags->flag & FLAG_ALTERNATE)
return (ft_printf_putfloat_e(iflt_o, flags));
if (flags->precision - e >= FLT_MTSSIZE)
flags->precision = FLT_MTSSIZE + e - 1;
ft_float_round(&iflt, e - flags->precision);
while (flags->precision > 0)
{
if (flags->precision - e >= 1 && iflt.mts_dec[flags->precision - e - 1])
break ;
else if (flags->precision - e < 1 &&
iflt.int_dec[FLT_INTSIZE + flags->precision - e - 1])
break ;
flags->precision--;
}
return (ft_printf_putfloat_e(iflt_o, flags));
}
int ft_printf_putfloat_g(t_float iflt, t_printf_flags *flags)
{
int e;
const t_float iflt_o = iflt;
if (flags->precision == 0)
flags->precision = 1;
ft_float_round(&iflt, -flags->precision + 1);
e = ft_printf_putfloat_e_get(iflt, flags);
iflt = iflt_o;
if (e < -4 || e >= flags->precision)
return (ft_putinstyle_e(iflt, flags, e));
else
return (ft_putinstyle_f(iflt, flags));
}