-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatod.c
138 lines (129 loc) · 3.97 KB
/
atod.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* atod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shongou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 23:32:43 by shongou #+# #+# */
/* Updated: 2023/01/04 23:32:51 by shongou ### ########.fr */
/* */
/* ************************************************************************** */
//#include <stdio.h>
//#include <float.h>
//#include <stdbool.h>
//#include <limits.h>
//#include "libft.h"
//#include <math.h>
//#define __GNU_SOURCE 1
#include "minirt.h"
static double my_atod(char *str)
{
double ret;
double base;
bool dot_flag;
base = 10;
ret = 0;
dot_flag = false;
while (*str && ft_isdigit(*str))
{
if (dot_flag)
ret += base * (*str - '0');
else
ret = ret * base + (*str - '0');
str++;
if (dot_flag)
base *= 0.1;
if (*str == '.' && !dot_flag)
{
str++;
dot_flag = true;
base = 0.1;
}
}
return (ret);
}
/*
void printb(unsigned long long v) {
unsigned long long mask = (long long) 1 << (sizeof(v) * CHAR_BIT - 1);
size_t cnt;
cnt = 0;
do {
putchar(mask & v ? '1' : '0');
cnt += 1;
}while (mask >>=1);
printf("\n");
printf("put %zu\n", cnt);
}*/
static double is_number(double num, int sign)
{
unsigned long long valid_bit1;
unsigned long long valid_bit2;
t_my_double my_dbl;
my_dbl.dnum = num;
valid_bit1 = ((1ull << 11) - 1ull) << 52;
valid_bit2 = (ULONG_MAX - 1) >> 12;
if ((valid_bit1 & my_dbl.ulnum) == valid_bit1)
{
if ((valid_bit2 & my_dbl.ulnum) != 0)
return (NAN);
return (sign * INFINITY);
}
return (sign * num);
}
double atod(char *str)
{
double num;
int sign;
sign = 1;
if (*str == '-' || *str == '+')
sign = 44 - *str++;
num = my_atod(str);
num = is_number(num, sign);
return (num);
}
/*
int main()
{
char *str;
str = "0";
printf("%s: %lf\n",str, atod(str));
str = "0.0";
printf("%s: %lf\n",str, atod(str));
str = "1234";
printf("%s: %lf\n",str, atod(str));
str = "-1234";
printf("%s: %lf\n",str, atod(str));
str = "1234.23";
printf("%s: %lf\n",str, atod(str));
str = "-1234.23";
printf("%s: %lf\n",str, atod(str));
str = "-1234.23.0";
printf("%s: %lf\n",str, atod(str));
str = "-1234.23str";
printf("%s: %lf\n",str, atod(str));
double nan;
nan = NAN;
printf("%lf: %lf\n", nan, is_number(nan, -1));
printf("%lf:%lf\n", INFINITY, is_number(INFINITY, 1));
printf("%lf:%lf\n", -INFINITY, is_number(INFINITY, -1));
printf("%lf:%lf\n", DBL_MAX, is_number(DBL_MAX, 1));
printf("%lf:%lf\n", -DBL_MAX, is_number(DBL_MAX, -1));
printf("%lf:%lf\n", DBL_MAX + 1, is_number(DBL_MAX + 1, 1));
printf("%lf:%lf\n", -DBL_MAX - 1, is_number(DBL_MAX + 1, -1));
//printf("%lf\n", nan - is_number(nan));
//printf("%lf\n", INFINITY - is_number(INFINITY));
//printf("%lf\n", -INFINITY - is_number(-INFINITY));
//printf("%lf\n", DBL_MAX - is_number(DBL_MAX));
//printf("%lf\n", -DBL_MAX - is_number(-DBL_MAX));
//printf("%lf\n", DBL_MAX + 1- is_number(DBL_MAX + 1));
//printf("%lf\n", -DBL_MAX - 1- is_number(-DBL_MAX - 1));
//printf("==================\n\n");
//printf("nan\n%lf\n%lf\n\n", nan, is_number(nan));
//printf("INFINITY\n%lf\n%lf\n\n", INFINITY, is_number(INFINITY));
//printf("-INFINITY\n%lf\n%lf\n\n", -INFINITY, is_number(-INFINITY));
//printf("DBL_MAX\n%lf\n%lf\n\n", DBL_MAX, is_number(DBL_MAX));
//printf("-DBL_MAX\n%lf\n%lf\n\n", -DBL_MAX, is_number(-DBL_MAX));
//printf("DBL_MAX+1\n%lf\n%lf\n\n", DBL_MAX + 1, is_number(DBL_MAX + 1));
//printf("-DBL_MAX-1\n%lf\n%lf\n\n", -DBL_MAX - 1, is_number(-DBL_MAX - 1));
}*/