-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.c
More file actions
83 lines (71 loc) · 1.67 KB
/
main.c
File metadata and controls
83 lines (71 loc) · 1.67 KB
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
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "log2fix.h"
#define PRECISION 16
#define XEEOF -1
static int read_number_ (double *x);
int main ()
{
double scale = 1U << PRECISION;
double x;
while (read_number_(&x) != -1) {
if (x < 0) {
fprintf(stderr, "%f is negative\n", x);
continue;
}
if (x >= 1 << (32 - PRECISION)) {
fprintf(stderr, "%f is too big\n", x);
continue;
}
printf(" log(%f) = %f\n", x, log(x));
printf("logfix(%f) = %f\n",
x, logfix(x * scale, PRECISION) / scale);
printf(" log2(%f) = %f\n", x, log2(x));
printf("log2fix(%f) = %f\n",
x, log2fix(x * scale, PRECISION) / scale);
printf(" log10(%f) = %f\n", x, log10(x));
printf("log10fix(%f) = %f\n",
x, log10fix(x * scale, PRECISION) / scale);
}
if (errno == XEEOF) {
return EXIT_SUCCESS;
} else {
perror("read_number_");
return EXIT_FAILURE;
}
}
int read_number_ (double *x)
{
static char s [BUFSIZ] = { 0 };
static char *c = (char *)s + BUFSIZ - 1;
while (isspace((int)*c)) {
c++;
}
if (*c == '\0') {
c = fgets(s, sizeof(s), stdin);
if (!c) {
if (feof(stdin)) {
errno = XEEOF;
}
return -1;
}
}
double d;
char *end;
errno = 0;
d = strtod(c, &end);
if (errno) {
return -1;
}
if (end == c) {
errno = EINVAL;
return -1;
}
*x = d;
c = end;
return 0;
}