ulps: Fix long lines
[mathlib.git] / etc / testrand.c
CommitLineData
0b5e8dcd 1#define _XOPEN_SOURCE 600
2
567cacff 3#include <float.h>
07bc03c0 4#include <math.h>
0b5e8dcd 5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
e4b6fe8f 10#include "subr_random.h"
0b5e8dcd 11
0b5e8dcd 12static void usage(const char *progname);
13
14int
15main(int argc, char *argv[])
16{
17 const char *progname, *type;
18 int i, min = -1, max = -1, total = -1;
19 int opt;
567cacff 20 float fx, flower, fupper;
21 double dx, dlower, dupper;
22 long double ldx, ldlower, ldupper;
0b5e8dcd 23
24 /* Save program name for later use */
25 progname = argv[0];
26 type = argv[1];
27
28 optind++;
29
30 /* Parse arguments. */
31 while ((opt = getopt(argc, argv, "m:M:t:")) != -1) {
32 switch (opt) {
33 case 'm':
34 min = atoi(optarg);
35 break;
36 case 'M':
37 max = atoi(optarg);
38 break;
39 case 't':
40 total = atoi(optarg);
41 break;
42 default:
43 usage(progname);
44 /* NEVER REACHED */
45 }
46 }
47
48 /* optind is the argv[] index of the first non-option element */
49 if (optind < argc) {
50 fprintf(stderr, "non-option argv[]-elements: ");
51 while (optind < argc)
52 fprintf(stderr, "%s ", argv[optind++]);
53 fprintf(stderr, "\n");
54 exit(EXIT_FAILURE);
55 }
56
567cacff 57 /* Make sure all arguments were supplied, plus validate input */
58 if (total == -1 || min == -1 || max == -1 || min > max) {
0b5e8dcd 59 usage(progname);
60 /* NEVER REACHED */
61 }
62
63 if (strcmp(type, "float") && strcmp(type, "double")
2372bb8a 64 && (strcmp(type, "ldouble"))) {
0b5e8dcd 65 usage(progname);
66 /* NEVER REACHED */
67 }
68
69 /* Initialize random number generator */
70 init_randgen();
71
72 /* Ready to go */
73 if (!strcmp(type, "float")) {
567cacff 74 if (min == 0 && max == 0) {
75 flower = -FLT_MAX;
76 fupper = FLT_MAX;
77 }
78
0b5e8dcd 79 for (i = 0; i < total; i++) {
80 do {
81 fx = random_float(FP_NORMAL);
567cacff 82 } while (fx < flower || fx > fupper);
1809043c 83 printf("% .8e\n", fx);
0b5e8dcd 84 }
85 }
86
87 if (!strcmp(type, "double")) {
567cacff 88 if (min == 0 && max == 0) {
89 dlower = -DBL_MAX;
90 dupper = DBL_MAX;
91 }
92
0b5e8dcd 93 for (i = 0; i < total; i++) {
94 do {
95 dx = random_double(FP_NORMAL);
567cacff 96 } while(dx < dlower || dx > dupper);
1809043c 97 printf("% .16e\n", dx);
0b5e8dcd 98 }
99 }
100
101 /* XXX: How many significand bits to write ? */
102 if (!strcmp(type, "ldouble")) {
567cacff 103 if (min == 0 && max == 0) {
104 ldlower = -LDBL_MAX;
105 ldupper = LDBL_MAX;
106 }
0b5e8dcd 107 for (i = 0; i < total; i++) {
108 do {
109 ldx = random_long_double(FP_NORMAL);
567cacff 110 } while(ldx < ldlower || ldx > ldupper);
1809043c 111 printf("% .32Le\n", ldx);
0b5e8dcd 112 }
113 }
114
115 return (EXIT_SUCCESS);
116}
117
118static void
119usage(const char *progname)
120{
121 fprintf(stderr,
122 "usage: %s {float, double, ldouble} -t total -m min -M max\n",
123 progname);
567cacff 124 fprintf(stderr,
125 "\tIf min == max, then all possible range is assumed,\n"
126 "\te.g. [-DBL_MAX, DBL_MAX]\n");
0b5e8dcd 127 exit(EXIT_FAILURE);
128}