This repository has been archived by the owner on Nov 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_floats.c
142 lines (131 loc) · 3.8 KB
/
ft_floats.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_floats.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 19:50:19 by mchen #+# #+# */
/* Updated: 2017/02/21 19:54:07 by mchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_printf.h"
static long double ft_uld_badround(long double ld, int precision, short base)
{
long double m;
long double c;
int i;
if ((i = precision) != -1)
{
m = ft_uld_get_mantissa(ld, base);
c = ld - m;
while (i-- > 0)
m *= base;
if (ft_uld_get_mantissa(m, base) >= .5)
m = m - ft_uld_get_mantissa(m, base) + 1.1;
else
m = m - ft_uld_get_mantissa(m, base) + .1;
while (++i < precision)
m /= base;
return (c + m);
}
return (ld);
}
static char *ft_uld_mantissatoa(long double ld, int precision,
short base)
{
int i;
char *s;
s = ft_memalloc(sizeof(*s));
while (precision > 0 || (precision < 0 && ld))
{
if (precision)
precision--;
i = ld * base;
s = ft_strjoin_free(s, ft_uitoa_base(i, base, 0, 1));
ld *= base;
ld -= i;
}
return (s);
}
static char *ft_uld_itoa(long double ld, int sigfig, int precision,
short base)
{
long double u;
uintmax_t d;
char *s;
u = 1;
while (u < ld / base)
u *= base;
s = ft_memalloc(sizeof(*s));
while (sigfig && (u >= 1 || !ft_strlen(s)))
{
d = (uintmax_t)(ld / u);
s = ft_strjoin_free(s, ft_uitoa_base(d, base, 0, 1));
if (!(*s == '0' && ft_strlen(s) == 1))
sigfig--;
ld -= d * u;
u /= base;
}
if (sigfig && (precision > 0 || precision == -1) && ld)
{
precision = sigfig > 0 ? sigfig : precision;
s = ft_strjoin_free(s, ft_strdup("."));
s = ft_strjoin_free(s, ft_uld_mantissatoa(ld, precision, base));
}
return (s);
}
static char *ft_printf_ftoa_handler(t_placehold *p, long double ld)
{
long double d;
char *s;
short c;
if (ft_strchr("fF", p->type))
s = ft_uld_itoa(ft_uld_badround(ld, p->prec, p->base), p->sigfig,
p->prec, p->base);
else if ((d = 1))
{
c = 0;
if (ld >= 1)
while (ld / d >= p->exp_base && ++c)
d *= p->exp_base;
else
while (ld && ld / d < 1 && ++c)
d /= p->exp_base;
s = ft_uld_itoa(ft_uld_badround(ld / d, p->prec, p->base), p->sigfig,
p->prec, p->base);
s = ft_strjoin_free(s, ft_strdup(ft_memset(ft_memalloc(2), p->exp_char,
1)));
s = ft_strjoin_free(s, (ld >= 1 || ld == 0) ? ft_strdup("+") :
ft_strdup("-"));
s = ft_strjoin_free(s, ft_uitoa_base(c, 10, 0, p->exp_len));
}
return (s = p->uppercase ? ft_strucase(s) : s);
}
char *ft_printf_ftoa(t_placehold *p, va_list a_list)
{
long double ld;
char *s;
ld = 0;
if (p->length && !ft_strcmp(p->length, "L"))
ld = va_arg(a_list, long double);
else
ld += va_arg(a_list, double);
p->sign = (ld < 0 ? '-' : p->sign);
ld = (ld < 0 ? -ld : ld);
p->prec = (p->prec == -1 && !ft_strchr("aA", p->type) ? 6 : p->prec);
if (ft_strchr("gG", p->type))
{
p->prec = (p->prec == 0 ? 1 : p->prec);
p->sigfig = p->prec;
if ((ld && ld < .00001) || ft_ld_integerpower(p->base, p->prec) <= ld)
p->type -= 2;
else
p->type -= 1;
s = ft_printf_ftoa_handler(p, ld);
s = ft_chrrepl_trailing(ft_chrrepl_trailing(s, '0', 0), '.', 0);
}
else
s = ft_printf_ftoa_handler(p, ld);
return (s);
}