-
Notifications
You must be signed in to change notification settings - Fork 12
/
ft_atof.c
49 lines (44 loc) · 1.55 KB
/
ft_atof.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atof.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/05 13:29:59 by apuchill #+# #+# */
/* Updated: 2021/02/07 09:27:17 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: <stdlib.h>
** SYNOPSIS: convert ASCII string to double
**
** DESCRIPTION:
** The atof() function converts the initial portion of the string pointed
** to by str to double.
*/
#include "libft.h"
#include <math.h>
#include <stdio.h>
double ft_atof(const char *str)
{
double int_part;
double dec_part;
double sign;
int i;
int_part = 0.0;
dec_part = 0.0;
sign = 1.0;
if (*str == '+' || *str == '-')
if (*str++ == '-')
sign = -1.0;
while (ft_isdigit(*str))
int_part = int_part * 10 + (*str++ - '0');
i = -1;
if (*str == '.' && *str++)
{
while (ft_isdigit(*str))
dec_part += (pow(10, i--) * (*str++ - '0'));
}
return (sign * (int_part + dec_part));
}