File: number_formatting.c

package info (click to toggle)
wcalc 2.3.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,444 kB
  • ctags: 926
  • sloc: ansic: 8,993; objc: 1,946; lex: 798; sh: 766; yacc: 623; makefile: 79
file content (562 lines) | stat: -rw-r--r-- 16,515 bytes parent folder | download
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#if HAVE_CONFIG_H
# include "config.h"
#endif

#if ! defined(HAVE_CONFIG_H) || HAVE_STRING_H
# include <string.h>
#endif
#include <stdio.h>		       /* for snprintf() */
#include <stdlib.h>		       /* for calloc() */

#include "number.h"

#include "calculator.h"
#include "string_manip.h"
#include "number_formatting.h"
#ifdef MEMWATCH
#include "memwatch.h"
#endif

static size_t zero_strip(char *num);
static void add_prefix(char *num, size_t length, int base);
static char *engineering_formatted_number(char *digits, num_exp_t exp,
					  const int precision, const int base,
					  const int prefix,
					  char *truncated_flag);
static char *full_precision_formatted_number(char *digits, num_exp_t exp,
					     const int base,
					     const int prefix);
static char *automatically_formatted_number(char *digits, num_exp_t exp,
					    const int precision,
					    const int base, const int prefix,
					    char *truncated_flag);
static char *precision_formatted_number(char *digits, num_exp_t exp,
					const int precision, const int base,
					const int prefix);

/* this function takes a number (mpfr_t) and prints it.
 * This is a blatant ripoff of mpfr's mpfr_out_str(), because it formats things
 * (sorta) the way I want them formatted, though this prints things out to a
 * string, and does all the fancy presentation stuff we've come to expect from
 * wcalc.
 */
char *num_to_str_complex(const Number num, const int base,
			 const enum engineering_modes engr, const int prec,
			 const int prefix, char *truncated_flag)
{
    char *s, *retstr;
    num_exp_t e;

    Dprintf("num_to_str_complex: base: %i, engr: %i, prec: %i, prefix: %i\n",
	    base, engr, prec, prefix);
    if (num_is_nan(num)) {
	return (char *)strdup("@NaN@");
    }
    if (num_is_inf(num)) {
	if (num_sign(num) > 0) {
	    return (char *)strdup("@Inf@");
	} else {
	    return (char *)strdup("-@Inf@");
	}
    }
    if (num_is_zero(num)) {
	if (num_sign(num) >= 0) {
	    return (char *)strdup("0");
	} else {
	    return (char *)strdup("-0");
	}
    }

    s = num_get_str(NULL, &e, base, 0, num);
    /* s is the string
     * e is the number of integers (the exponent) if positive
     *
     * Now, if there's odd formatting involved, make mpfr do the rounding,
     * so we know it's "correct":
     */
    if (prec > -1) {
	if (engr == never) {
	    size_t significant_figures = 0;

	    Dprintf("prec > -1 && engr == never\n");
	    /*printf("e: %li\n", (long)e);
	     * printf("s: %s\n", s);
	     * printf("prec: %i\n", prec); */
	    significant_figures = ((e > 0) ? e : 0) + prec;
	    if (significant_figures < 2) {	/* why is this the minimum? */
		s = num_get_str(s, &e, base, 2, num);
		if (s[1] > '4') {      /* XXX: LAME! */
		    unsigned foo;

		    foo = s[0] - '0';
		    foo++;
		    snprintf(s, 3, "%u", foo);
		    e++;
		}
	    } else {
		num_free_str(s);
		s = num_get_str(NULL, &e, base, significant_figures, num);
	    }
	} else {
	    int left_digits = 0;
	    Number temp;

	    Dprintf("engr == auto || engr == always\n");
	    /* first, count how many figures to the left of the decimal */
	    num_init_set(temp, num);
	    while (num_get_d(temp) >= 1.0) {
		num_div_ui(temp, temp, base);
		left_digits++;
	    }
	    num_free(temp);
	    if (left_digits == 0) {
		left_digits = 1;
	    }
	    Dprintf("left_digits = %i, asking for %i\n", left_digits,
		    left_digits + prec);
	    s = num_get_str(NULL, &e, base, left_digits + prec, num);
	}
    }
    Dprintf("post-mpfr e: %li s: %s\n", (long int)e, s);
    *truncated_flag = 0;
    if (-2 == prec) {
	retstr = full_precision_formatted_number(s, e, base, prefix);
    } else {
	switch (engr) {
	    case always:
		Dprintf("ALWAYS print engineering\n");
		retstr =
		    engineering_formatted_number(s, e, prec, base, prefix,
						 truncated_flag);
		break;
	    case never:
		Dprintf("NEVER print engineering\n");
		retstr = precision_formatted_number(s, e, prec, base, prefix);
		break;
	    default:
	    case automatic:
		Dprintf("AUTOMATICALLY decide on engineering formatting\n");
		retstr =
		    automatically_formatted_number(s, e, prec, base, prefix,
						   truncated_flag);
	}
    }
    num_free_str(s);
    Dprintf("return string: %s\n", retstr);
    return retstr;
}

char *precision_formatted_number(char *digits, num_exp_t exp,
				 const int precision, const int base,
				 const int prefix)
{
    size_t length;
    size_t full_length;
    size_t decimal_count = 0;
    size_t print_limit;
    char *retstring, *curs, *dcurs = digits;

    length = strlen(digits);
    /* testing against both zero and length because length is unsigned */
    if (exp > 0 && (size_t) exp > length)
	length = exp;
    length += 3;

    if (length < (size_t) (precision + 3)) {	// leading zero, decimal, and null
	length = (size_t) (precision + 3);
    }
    Dprintf("Precision Formatted Number\n");
    Dprintf("digits: %s(%u), exp: %i, base: %i, prefix: %i, precision: %i\n",
	    digits, (unsigned)length, (int)exp, base, prefix, precision);

    // ten extra, 'cuz of the *possible* exponent
    full_length = length + 10;
    curs = retstring = (char *)calloc(full_length, sizeof(char));
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);
    // now, copy the digits into the output string, carefully

    // copy over the negative sign
    if (*dcurs == '-') {
	snprintf(curs++, length--, "%c", *dcurs++);
    }
    // copy in a prefix
    if (prefix) {
	char *nc;

	add_prefix(curs, length, base);
	nc = strchr(curs, '\0');
	length -= nc - curs;
	curs = nc;
    }
    // copy in the integers
    if (exp > 0) {
	snprintf(curs++, length--, "%c", *dcurs++);
	exp--;			       // leading digit
	while (exp > 0 && *dcurs) {
	    snprintf(curs++, length--, "%c", *dcurs++);
	    exp--;
	}
	for (; exp > 0; exp--) {
	    snprintf(curs++, length--, "0");
	}
    } else {
	snprintf(curs++, length--, "0");
    }
    if (precision > 0) {
	// the decimal
	snprintf(curs++, length--, ".");
	Dprintf("the integers: %s\n", retstring);
	Dprintf("l: %lu, fl: %lu, dc: %u, p: %i, e: %i\n", length,
		full_length, (unsigned)decimal_count, (int)precision,
		(int)exp);
	// everything after this is affected by decimalcount
	// copy in the leading zeros
	while (exp < 0 && (ssize_t) decimal_count <= precision) {
	    snprintf(curs++, length--, "0");
	    exp++;
	    decimal_count++;
	}
	Dprintf("l: %lu, fl: %lu, dc: %u, p: %i, e: %i\n", length,
		full_length, (unsigned)decimal_count, (int)precision,
		(int)exp);
	// copy in the rest of the mantissa (the decimals)
	Dprintf("leading zeros: %s\n", retstring);
	// this variable exists because snprintf's return value is unreliable,
	// and can be larger than the number of digits printed
	print_limit =
	    ((length <
	      (precision - decimal_count + 1)) ? length : (precision -
							   decimal_count +
							   1));
	snprintf(curs, print_limit, "%s", dcurs);
    }

    return retstring;
}

char *full_precision_formatted_number(char *digits, num_exp_t exp,
				      const int base, const int prefix)
{
    size_t length;
    size_t full_length;
    size_t decimal_count = 0;
    size_t printed;
    char *retstring, *curs, *dcurs = digits;

    length = strlen(digits);
    /* testing against both zero and length because length is unsigned */
    if (exp > 0 && (size_t) exp > length)
	length = exp;
    length += 3;		       /* the null, the (possible) sign, and the decimal */

    Dprintf("Full Precision Formatted Number\n");
    Dprintf("digits: %s(%u), exp: %i, base: %i, prefix: %i\n", digits,
	    (unsigned)length, (int)exp, base, prefix);
    Dprintf("strlen(digits): %u\n", (unsigned)strlen(digits));

    // ten extra, 'cuz of the *possible* exponent
    full_length = length + 10;
    curs = retstring = (char *)calloc(sizeof(char), full_length);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);

    // now, copy the digits into the output string, carefully

    // copy over the negative sign
    if (*dcurs == '-') {
	snprintf(curs++, length--, "%c", *dcurs++);
    }
    // copy in a prefix
    if (prefix) {
	char *nc;

	add_prefix(curs, length, base);
	nc = strchr(curs, '\0');
	length -= nc - curs;
	curs = nc;
    }
    Dprintf("ready for ints: %s\n", retstring);
    // copy over the integers
    if (exp > 0) {
	snprintf(curs++, length--, "%c", *dcurs++);
	exp--;			       // leading digit
	while (exp > 0 && *dcurs) {
	    snprintf(curs++, length--, "%c", *dcurs++);
	    exp--;
	}
	for (; exp > 0; exp--) {
	    snprintf(curs++, length--, "0");
	}
    } else {
	snprintf(curs++, length--, "0");
    }
    // the decimal
    snprintf(curs++, length--, ".");
    Dprintf("the integers: %s\n", retstring);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);
    // XXX: Currently, this function is not used for decimals, so...

    // the leading decimal zeros
    while (exp < 0) {
	snprintf(curs++, length--, "0");
	exp++;
	decimal_count++;
    }
    // the rest of the mantissa (the decimals)

    // this variable exists because snprintf's return value is unreliable.
    // and can be larger than the number of digits printed
    printed = ((length - 1 < strlen(dcurs)) ? length - 1 : strlen(dcurs));
    snprintf(curs, length, "%s", dcurs);
    length -= printed;
    decimal_count += printed;

    // strip off the trailing 0's
    zero_strip(retstring);

    // copy in an exponent if necessary
    if (exp != 0) {
	curs = strchr(retstring, '\0');
	Dprintf("space left: %lu\n", full_length - (curs - retstring));
	snprintf(curs, full_length - (curs - retstring),
		 (base <= 10 ? "e%ld" : "@%ld"), (long)exp);
    }

    return retstring;
}

char *automatically_formatted_number(char *digits, num_exp_t exp,
				     const int precision, const int base,
				     const int prefix, char *truncated_flag)
{
    size_t length;
    size_t full_length;
    size_t decimal_count = 0;
    size_t printed;
    char *retstring, *curs, *dcurs = digits;

    length = strlen(digits);
    /* testing against both zero and length because length is unsigned */
    if (exp > 0 && (size_t) exp > length)
	length = exp;
    length += 3;		       /* the null, the (possible) sign, and the decimal */

    Dprintf("Automatically Formatted Number\n");
    Dprintf("digits: %s(%u), exp: %i, base: %i, prefix: %i\n", digits,
	    (unsigned)length, (int)exp, base, prefix);
    Dprintf("strlen(digits): %u\n", (unsigned)strlen(digits));

    // ten extra, 'cuz of the *possible* exponent
    full_length = length + 10;
    curs = retstring = (char *)calloc(sizeof(char), full_length);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);

    // now, copy the digits into the output string, carefully

    // copy over the negative sign
    if (*dcurs == '-') {
	snprintf(curs++, length--, "%c", *dcurs++);
    }
    // copy in a prefix
    if (prefix) {
	char *nc;

	add_prefix(curs, length, base);
	nc = strchr(curs, '\0');
	length -= nc - curs;
	curs = nc;
    }
    Dprintf("ready for ints: %s\n", retstring);
    // copy over the integers
    if (exp > 0) {
	snprintf(curs++, length--, "%c", *dcurs++);
	exp--;			       // leading digit
	while (exp > 0 && *dcurs) {
	    snprintf(curs++, length--, "%c", *dcurs++);
	    exp--;
	}
	for (; exp > 0; exp--) {
	    snprintf(curs++, length--, "0");
	}
    } else {
	snprintf(curs++, length--, "0");
    }
    // the decimal
    snprintf(curs++, length--, ".");
    Dprintf("the integers: %s\n", retstring);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);
    // XXX: Currently, this function is not used for decimals, so...

    // the leading decimal zeros
    while (exp < 0) {
	snprintf(curs++, length--, "0");
	exp++;
	decimal_count++;
    }
    // the rest of the mantissa (the decimals)

    // this variable exists because snprintf's return value is unreliable.
    // and can be larger than the number of digits printed
    printed = ((length - 1 < strlen(dcurs)) ? length - 1 : strlen(dcurs));
    snprintf(curs, length, "%s", dcurs);
    length -= printed;
    decimal_count += printed;

    if (precision == -1) {
	char *period;

	// strip off the trailing 0's
	zero_strip(retstring);
	/* XXX: This is a stupid hack; the idea is just to get the mpfr output
	 * to match the double output. */
	period = strchr(retstring, '.');
	Dprintf("retstring: %s\n", retstring);
	Dprintf("period: %s\n", period);
	if (period && strlen(period) > 10) {
	    period[10] = 0;
	    *truncated_flag = 1;
	    zero_strip(retstring);
	}
    } else if (precision >= 0) {
	char *period = strchr(retstring, '.') + 1;

	Dprintf("period: %s\n", period);
	if (period && strlen(period) > (size_t) precision) {
	    Dprintf("truncating down to precision...\n");
	    period[precision] = 0;
	    *truncated_flag = 1;
	}
    }
    // copy in an exponent if necessary
    if (exp != 0) {
	curs = strchr(retstring, '\0');
	Dprintf("space left: %lu\n", full_length - (curs - retstring));
	snprintf(curs, full_length - (curs - retstring),
		 (base <= 10 ? "e%ld" : "@%ld"), (long)exp);
    }

    return retstring;
}

char *engineering_formatted_number(char *digits, num_exp_t exp,
				   const int precision, const int base,
				   const int prefix, char *truncated_flag)
{
    size_t length;
    size_t full_length;
    char *retstring, *curs, *dcurs = digits;

    length = strlen(digits);
    /* testing against both zero and length because length is unsigned */
    if (exp > 0 && (size_t) exp > length)
	length = exp;
    length += 3;		       /* the null, the (possible) sign, and the decimal */

    Dprintf("Engineering Formatted Number\n");
    Dprintf("digits: %s(%u), exp: %i, prec: %i, prefix: %i\n", digits,
	    (unsigned)length, (int)exp, precision, prefix);

    // ten extra, 'cuz of the exponent
    full_length = length + 10;
    curs = retstring = (char *)calloc(sizeof(char), full_length);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);

    // now, copy the digits into the output string, carefully

    // copy over the negative sign
    if (*dcurs == '-') {
	snprintf(curs++, length--, "%c", *dcurs++);
    }
    // copy in a prefix
    if (prefix) {
	char *nc;

	add_prefix(curs, length, base);
	nc = strchr(curs, '\0');
	length -= nc - curs;
	curs = nc;
    }
    // copy over the integer
    snprintf(curs++, length--, "%c", *dcurs++);
    exp--;
    // the decimal
    snprintf(curs++, length--, ".");
    Dprintf("the integers: %s\n", retstring);
    Dprintf("length: %lu, full_length: %lu\n", length, full_length);

    // now, add in the rest of the digits
    // note that the digits are already correctly rounded and I've already
    // allocated enough space, because of how I asked mpfr for the original
    // digit string.
    snprintf(curs, length + 1, "%s", dcurs);
    Dprintf("the decimals: %s\n", retstring);

    // strip off the trailing 0's
    if (-1 == precision) {
	char *period;

	zero_strip(retstring);
	/* XXX: This is a stupid hack; the idea is just to get the mpfr output
	 * to match (roughly) the double output. */
	period = strchr(retstring, '.');
	Dprintf("retstring: %s\n", retstring);
	Dprintf("period: %s\n", period);
	if (period && strlen(period) > 10) {
	    period[10] = 0;
	    *truncated_flag = 1;
	    zero_strip(retstring);
	}
    } else {
	char *period = strchr(retstring, '.') + 1;

	Dprintf("period: %s\n", period);
	if (period && (int)strlen(period) > precision) {
	    Dprintf("truncating down to precision...\n");
	    period[precision] = 0;
	    *truncated_flag = 1;
	}
    }
    // copy in an exponent
    curs = strchr(retstring, '\0');
    Dprintf("space left: %lu\n", full_length - (curs - retstring));
    snprintf(curs, full_length - (curs - retstring),
	     (base <= 10 ? "e%+ld" : "@%+ld"), (long)exp);

    return retstring;
}

/* this function removes zeros and decimals from the back end of a number,
 * and returns how many characters it stripped */
size_t zero_strip(char *num)
{
    size_t curs = strlen(num) - 1;
    size_t count = 0;

    while ('0' == num[curs]) {
	num[curs--] = '\0';
	count++;
    }
    if ('.' == num[curs]) {
	num[curs] = '\0';
	count++;
    }
    return count;
}

/* this function prints a prefix for the specified base into the specified
 * string */
void add_prefix(char *num, size_t length, int base)
{
    switch (base) {
	case 16:
	    snprintf(num, length, "0x");
	    return;
	case 10:
	    return;
	case 8:
	    snprintf(num, length, "0");
	    return;
	case 2:
	    snprintf(num, length, "0b");
	    return;
    }
}