-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatof.c
89 lines (77 loc) · 1.55 KB
/
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
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
#include <stdio.h>
#include <math.h>
/*
double atof(char *nbr)
{
}
*/
int count_digits_int(int nr)
{
int digits_count;
digits_count = 1 + (nr < 0);
while (nr / 10 != 0)
{
digits_count++;
nr = nr / 10;
}
return (digits_count);
}
int count_digits_fraction(double nr, int precision)
{
int digits_count;
digits_count = 2 + (nr < 0);
if (nr < 0)
nr *= -1;
while (nr / 0.1 > 0 || !precision)
{
printf("%f / 0.1 = %f\n", nr, nr / 0.1);
digits_count++;
precision--;
nr = remainder(nr, 0.1) * 10;
}
return (digits_count);
}
/*
int count_digits_double(double nr)
{
int digits;
digits = 0 + (nr < 0);
while (nr / 10 > 0)
}
*/
int main(void)
{
double db = 0.123456;
/* db = -0.0123;
printf("%f / 0.1 = %f\n", db, db / 0.1);
db = remainder(db, 0.1) * 10;
if (db == 0)
printf("ZERO %f\n", db);
printf("%f / 0.1 = %f\n", db, db / 0.1);
db = remainder(db, 0.1) * 10;
printf("%f / 0.1 = %f\n", db, db / 0.1);
db = remainder(db, 0.1) * 10;
printf("%f / 0.1 = %f\n", db, db / 0.1);
db = remainder(db, 0.1) * 10;
printf("%f / 0.1 = %f\n", db, db / 0.1);
db = remainder(db, 0.1);
if (db == 0)
printf("ZERO %f\n", db);
*/
/* printf("%f %% 0.1 = %f\n", db, remainder(db, 0.1));
return (0);
*/
/* printf("sizeof(double) = %zu\n", sizeof(double));
printf("%f is %.34f\n", db, db); return (0);
*/
printf("%f has %d digits\n", db, count_digits_fraction(db, 10));
return (0);
int n = 0;
printf("%d has %d digits\n", n, count_digits_int(n));
return (0);
double d = 1283.72;
int i = (int)d;
printf("%d\n", i);
printf("%f\n", d - i);
return (0);
}