forked from kl4yfd/timewave_z3r0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwz-generator.c
474 lines (269 loc) · 8.83 KB
/
twz-generator.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// twz.generator.c
// Original Author: Peter Meyer
// Calculate the value of the timewave at a point.
// Last mod.: 1998-01-05
// http://www.fractal-timewave.com/
// Ported to Linux
// 4 Oct 2009
// John A Phelps
// kl4yfd@gmail.com
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
09 Dec 2012
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
#define NUM_POWERS 64
#define PREC 16 // long double (80 bit) numbers have about 16 significant digits INTEL / AMD / x86_64
//#define PREC 32 // long double (128 bit) numbers have about 32 significant digits (QUAD PRECISION)
#define NUM_SETS 4
#define NUM_DATA_POINTS 384
#define CALC_PREC 1000000 // precision in calculation of wave values
long double powers[NUM_POWERS];
// Powers of (normally) 64.
// Due to the limitations of double precision
// floating point arithmetic these values are
// exact only up to powers[8] for powers of 64.
int64_t wave_factor = 64; // default wave factor
int64_t number_set, stringchar;
char *usage = "\nUsage: twz [dtz] [neg] [step] [wf]."
"\n dtz = days to zero-point"
"\n step = steps in which to decrement time (in minutes)"
"\n wf = wave factor (default 64, range 2-10000)"
"\n\nThis program calculates the running values of the timewave within the given window.\n";
char *set_name[NUM_SETS] =
{ "Kelley", "Watkins", "Sheliak", "Huang Ti" };
char *title = "Days to Zero (DTZ), Kelley, Watkins, Sheliak, Huang Ti";
// The number sets.
int64_t w[NUM_SETS][NUM_DATA_POINTS] =
{
{
#include "DATA/DATA.TW1" // half-twist
},
{
#include "DATA/DATA.TW2" // no half-twist
},
{
#include "DATA/DATA.TW3" // Sheliak
},
{
#include "DATA/DATA.TW4" // HuangTi (no half-twist)
}
};
void inputerror (void);
void get_dtzp (void);
void get_NegBailout (void);
void get_step (void);
void get_wave_factor (void);
void set_powers (void);
long double f (long double x, int64_t number_set);
long double fONE (long double x);
long double fTWO (long double x);
long double fTHREE (long double x);
long double fFOUR (long double x);
long double v (long double y, int64_t number_set);
long double mult_power (long double x, int64_t i);
long double div_power (long double x, int64_t i);
long double dtzp, NegativeBailout, step;
/*-----------------------------*/
int
main (int argc, char *argv[])
{
int64_t i, j, ch;
if (argc != 5 && argc != 1)
{
printf ("%s", usage);
inputerror ();
}
if (argc == 5)
{
dtzp = atof (&argv[1][0]);
NegativeBailout = atof (&argv[2][0]);
NegativeBailout *= -1;
step = atof (&argv[3][0]);
step /= 60; // Convert to 60 minute hours
step /= 24; // Convert to 24 hour days
wave_factor = atoi (&argv[4][0]);
if (wave_factor < 2 || wave_factor > 10000)
{
printf ("%s", usage);
inputerror ();
}
}
if (argc == 1) // If no commandline inputs
{
get_dtzp ();
get_NegBailout();
get_step ();
get_wave_factor ();
}
set_powers ();
//printf("\n\ndtzp: %lfstep: %lfwave_factor: %d",dtzp, step, wave_factor);
printf ("\n%s\n", title);
while (dtzp >= NegativeBailout)
{
printf ("%.*Lf ,", PREC, dtzp);
for (number_set = 0; number_set < NUM_SETS; number_set++)
{
printf ("%.*Lf ,", PREC, f (dtzp, number_set));
}
printf ("\n");
dtzp -= step;
}
}
// wave_factor is a global variable
/*-----------------*/
void
set_powers (void)
{
uint64_t j;
/* put powers[j] = wave_factor^j */
powers[0] = (long double) 1;
for (j = 1; j < NUM_POWERS; j++)
powers[j] = wave_factor * powers[j - 1];
}
/* x is number of days to zero date */
/*--------------*/
long double
f (long double x,
int64_t number_set)
{
uint64_t i;
long double sum = 0.0, last_sum = 0.0;
if (x)
{
for (i = 0; x >= powers[i]; i++)
sum += mult_power (v (div_power (x, i), number_set), i);
i = 0;
do
{
if (++i > CALC_PREC + 2)
break;
last_sum = sum;
sum += div_power (v (mult_power (x, i), number_set), i);
}
while ((sum == 0.0) || (sum > last_sum));
}
/* dividing by 64^3 gives values consistent with the Apple // version
* and provides more convenient y-axis labels
*/
sum = div_power (sum, 3);
return (sum);
}
/*--------------*/
long double
v (long double y,
int64_t number_set)
{
int64_t i = (int64_t) (fmod (y, (long double) NUM_DATA_POINTS));
int64_t j = (i + 1) % NUM_DATA_POINTS;
long double z = y - floor (y);
return (z == 0.0 ? (long double) w[number_set][i] :
(w[number_set][j] - w[number_set][i]) * z + w[number_set][i]);
}
/* in order to speed up the calculation, if wave factor = 64
* then instead of using multiplication or division operation
* we act directly on the floating point representation;
* multiplying by 64^i is accomplished by adding i*0x60
* to the exponent (the last 2 bytes of the 8-byte representation);
* dividing by 64^i is accomplished by subtracting i*0x60
* from the exponent
*/
/*-----------------------*/
long double
mult_power (long double x,
int64_t i)
{
/* Removing this code: Swithing to 64-bit datatypes
int *exponent = (int *) &x + 3;
if (wave_factor == 64)
*exponent += i * 0x60; // measurably faster
else
*/
x *= powers[i];
return (x);
}
/*----------------------*/
long double
div_power (long double x,
int64_t i)
{
/* Removing this code: Swithing to 64-bit datatypes
int *exponent = (int *) &x + 3;
if ((wave_factor == 64) && (*exponent > i * 0x60))
*exponent -= i * 0x60;
else
*/
x /= powers[i];
return (x);
}
void
get_dtzp (void)
{
printf ("Enter the number of days before zero point: ");
int64_t temp = scanf ("%Lf", &dtzp);
}
void
get_NegBailout (void)
{
printf ("Enter the number of days to calculate after zero point: ");
int64_t temp = scanf ("%Lf", &NegativeBailout);
NegativeBailout *= -1; // Set to negative for internal use
}
void
get_step (void)
{
printf ("Enter the time step in minutes ( >= 0 ): ");
int64_t temp = scanf ("%Lf", &step);
step /= 60; // Convert to 60 minute hours
step /= 24; // Convert to fractions of 24-hour days for internal calculations...
if (step < 0)
inputerror ();
}
void
get_wave_factor (void)
{
printf ("Enter the wave factor (2-10000): ");
int64_t temp = scanf ("%ld", &wave_factor);
if ( wave_factor < 2 || wave_factor > 10000 ) inputerror();
}
int64_t
doublecheck (void)
{
char answer;
printf
("\nThe combination you have chosen will create %Lf data points. \nDo you wish to continue? (Y/N) ",
1 + (int) dtzp / step);
answer = getchar ();
}
void
inputerror (void)
{
printf ("\nError: Invalid input, exiting.\n\n");
exit (EXIT_SUCCESS);
}