-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_number.c
83 lines (74 loc) · 1.83 KB
/
utils_number.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_number.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khlavaty <khlavaty@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 21:12:02 by fvonsovs #+# #+# */
/* Updated: 2024/09/12 21:06:17 by khlavaty ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
// converts string to float by iterating until we find decimal
// after that divide by 10 and calculate fraction
float str_to_float(char *str)
{
float sum;
float frac;
float div;
float sign;
frac = 0.0;
div = 1.0;
sign = 1.0;
if (str && str[0] == '-')
sign *= -1.0;
sum = (float)ft_atoi(str);
while (*str && *str != '.')
str++;
if (*str++ == '.')
{
while (*str >= '0' && *str <= '9')
{
div *= 10.0;
frac += (*str - '0') / div;
str++;
}
sum += frac * sign;
}
return (sum);
}
int str_to_int_color(char *str)
{
int c;
c = ft_atoi(str);
if (c < 0)
return (0);
if (c > 255)
return (255);
return (c);
}
int is_float(char *str)
{
int i;
i = 0;
while (str[i] && str[i])
{
if (ft_isdigit(str[i]) != 1 && str[i] != '-' && str[i] != '.')
return (0);
i++;
}
return (1);
}
int is_ulong(char *str)
{
int i;
i = 0;
while (str[i] && str[i])
{
if (ft_isdigit(str[i]) != 1)
return (0);
i++;
}
return (1);
}