-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.c
More file actions
85 lines (76 loc) · 2.09 KB
/
float.c
File metadata and controls
85 lines (76 loc) · 2.09 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* float.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stoupin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/24 13:53:21 by stoupin #+# #+# */
/* Updated: 2017/04/24 14:05:52 by stoupin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_int(t_buf *buffer, long long int n,
int ndigits, int printed)
{
int len;
char c;
len = 0;
if (n <= -10 || n >= 10 || printed < ndigits)
len = print_int(buffer, n / 10, ndigits, printed + 1);
if (n >= 0)
c = '0' + (n % 10);
else
c = '0' - (n % 10);
buf_putchar(buffer, c);
return (len + 1);
}
static long int ft_pow(int number, int power)
{
long int result;
int i;
result = 1;
i = 0;
while (i < power)
{
result *= number;
i++;
}
return (result);
}
static int buf_print_float(t_buf *buffer, double n,
int after_point, int no_print)
{
long int ipart;
long int ifpart;
int len;
len = 0;
ipart = (long int)n;
len = ft_intlen(ipart, 10);
if (!no_print)
print_int(buffer, ipart, -1, 1);
if (after_point != 0)
{
ifpart = (long int)(ft_pow(10, after_point) * (n - (double)ipart) + .5);
len += 1 + after_point;
if (!no_print)
{
buf_putchar(buffer, '.');
print_int(buffer, ifpart, after_point, 1);
}
}
return (len);
}
int print_float(t_buf *buffer, double n,
int after_point, int no_print)
{
int len;
len = 0;
if (n < 0)
{
n = -n;
len++;
}
len += buf_print_float(buffer, n, after_point, no_print);
return (len);
}