-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_floatutil_bonus.c
72 lines (67 loc) · 2.06 KB
/
ft_printf_floatutil_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
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_floatutil_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnakano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/13 15:48:02 by dnakano #+# #+# */
/* Updated: 2020/10/18 12:16:21 by dnakano ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
void ft_putnaninf(int flag_isnan)
{
if (flag_isnan)
ft_putstr_fd("nan", 1);
else
ft_putstr_fd("inf", 1);
}
static int ft_round_check(t_float *iflt, int digit)
{
int i;
if (digit >= 0)
{
if (iflt->int_dec[FLT_INTSIZE - (digit + 1)] % 2)
return (1);
i = 0;
while (i < digit - 1)
{
if (iflt->int_dec[FLT_INTSIZE - i++ - 1] != 0)
return (1);
}
}
if (digit < 0 && iflt->mts_dec[-digit - 1] % 2)
return (1);
i = digit <= 0 ? -digit + 1 : 0;
while (i < FLT_MTSSIZE - 1)
{
if (iflt->mts_dec[i++] != 0)
return (1);
}
return (0);
}
void ft_float_round(t_float *iflt, int digit)
{
if ((digit > FLT_INTSIZE - 1) || (digit < -FLT_MTSSIZE) ||
(!iflt->frac && iflt->exp == (FLT_EXPBIAS + 1)))
return ;
if (digit > 0)
{
if (iflt->int_dec[FLT_INTSIZE - digit] > 5)
ft_float_roundup(iflt, digit);
else if (iflt->int_dec[FLT_INTSIZE - digit] < 5)
return ;
else if (ft_round_check(iflt, digit))
ft_float_roundup(iflt, digit);
}
else
{
if (iflt->mts_dec[-digit] > 5)
ft_float_roundup(iflt, digit);
else if (iflt->mts_dec[-digit] < 5)
return ;
else if (ft_round_check(iflt, digit))
ft_float_roundup(iflt, digit);
}
}