-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_dtoa_f.c
87 lines (80 loc) · 2.23 KB
/
ft_dtoa_f.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_dtoa_f.c :+: :+: */
/* +:+ */
/* By: mraasvel <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/20 20:45:05 by mraasvel #+# #+# */
/* Updated: 2020/11/21 16:28:08 by mraasvel ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
#include "ft_double.h"
static double extract_fraction(double nbr,
char *digits, int precision, int exp)
{
int c;
if ((exp - 1023) > 51)
{
while (precision > 0)
{
*digits = '0';
digits++;
precision--;
}
return (0.0);
}
while (precision > 0)
{
nbr *= 10;
c = (int)nbr;
nbr -= c;
if (c < 0)
c = -c;
*digits = c + '0';
digits++;
precision--;
}
return (nbr);
}
static double extract_integers(double nbr, char *digits, int len)
{
double ten;
int c;
ten = ft_pow(10, len - 1);
while (ten >= 1.0)
{
c = (int)(nbr / ten);
nbr -= ten * (double)c;
ten /= 10;
if (c < 0)
c = -c;
*digits = c + '0';
digits++;
}
return (nbr);
}
char *ft_dtoa_f(double nbr, int precision)
{
t_double d_union;
char *digits;
char rounding_digits[16];
int numlen;
d_union.value = nbr;
numlen = ft_numlen_dbl(nbr);
digits = (char*)malloc((numlen + precision + 2) * sizeof(char));
if (digits == NULL)
return (NULL);
digits[numlen + precision + 1] = '\0';
nbr = extract_integers(nbr, digits, numlen);
digits[numlen] = '.';
if (precision > 0)
nbr = extract_fraction(nbr, digits + numlen + 1,
precision, d_union.bits.exponent);
rounding_digits[15] = '\0';
extract_fraction(nbr, rounding_digits, 15, d_union.bits.exponent);
digits = ft_round_f(digits, rounding_digits);
return (digits);
}