-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_longdouble_sci.c
118 lines (108 loc) · 3.33 KB
/
ft_putnbr_longdouble_sci.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_putnbr_longdouble_sci.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: bpajot <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/03/05 15:36:35 by bpajot #+# ## ## #+# */
/* Updated: 2018/12/17 14:34:19 by bpajot ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_char_exp_longdouble(int e, t_field *cur)
{
char *exp;
int l;
char *buf;
l = (e * e >= 100 * 100) ? 5 : 4;
l += (e * e >= 1000 * 1000) ? 1 : 0;
exp = ft_strnew(l);
exp[0] = ((cur->type & E_MAJ) || (cur->type & G_MAJ)) ? 'E' : 'e';
exp[1] = (e < 0) ? '-' : '+';
buf = (e >= 0) ? ft_itoa(e) : ft_itoa(-e);
if (e * e >= 10 * 10)
ft_strcpy(&exp[2], buf);
else
{
exp[2] = '0';
ft_strcpy(&exp[3], buf);
}
exp[3] = (e == 0) ? '0' : exp[3];
ft_memdel((void**)&buf);
return (exp);
}
static void ft_char_longdouble_sci2(char *buf, char *exp)
{
ft_memdel((void**)&buf);
ft_memdel((void**)&exp);
}
static char *ft_char_longdouble_sci(t_double *d, t_field *cur)
{
char *res;
char *buf;
int e;
char *exp;
buf = ft_two_pow(d->e - 63);
e = ft_get_exponent(buf);
ft_memdel((void**)&buf);
buf = (e < 0) ? ft_char_longdouble(d, cur->preci - e + 1, cur) :
ft_char_longdouble(d, cur->preci + 1, cur);
e = ft_get_exponent(buf);
res = ft_char_dec_to_sci(buf, cur->preci + 1, e);
buf = ft_round(res, cur->preci);
if (buf[0] == '0' || buf[2] == '.')
{
e += (buf[0] == '0') ? -1 : 1;
buf[1] = (buf[0] == 0) ? buf[2] : '.';
buf[2] = (buf[0] == 0) ? '.' : '0';
buf[cur->preci + 2] = 0;
}
exp = ft_char_exp_longdouble(e, cur);
ft_memdel((void**)&res);
res = ft_strjoin(buf, exp);
ft_char_longdouble_sci2(buf, exp);
return (res);
}
int ft_putnbr_longdouble_sci(t_double *d, t_field *cur)
{
int ret;
char *buf;
ret = 0;
if (d->e == 16385 && d->m == 0)
return ((cur->type & E_MAJ || cur->type & G_MAJ) ? ft_putstr_size("INF")
: ft_putstr_size("inf"));
else if (d->e == 16385)
return ((cur->type & E_MAJ || cur->type & G_MAJ) ? ft_putstr_size("NAN")
: ft_putstr_size("nan"));
else if (d->e == -16384 && d->m == 0)
{
ret += ft_putnbr_size(0);
ret += (cur->preci) ? ft_putchar_size('.') : 0;
ret += (cur->preci) ? ft_putchar_sizel('0', cur->preci) : 0;
buf = ft_char_exp_longdouble(0, cur);
}
else
buf = ft_char_longdouble_sci(d, cur);
ret += ft_putstr_size(buf);
ft_memdel((void**)&buf);
return (ret);
}
int ft_longdoublelen_sci(t_double *d, t_field *cur)
{
int ret;
char *buf;
ret = 0;
if (d->e == 16385)
ret = 3;
else if (d->e == -16384 && d->m == 0)
ret = (cur->preci) ? cur->preci + 6 : 5;
else
{
buf = ft_char_longdouble_sci(d, cur);
ret += ft_strlen(buf);
ft_memdel((void**)&buf);
}
return (ret);
}